Skip to content

Commit dac675c

Browse files
authored
Merge pull request #729 from oivoodoo/fix-type-bug
Fix type issue if the user model has attribute with `data` name
2 parents 4245b05 + 03c9d33 commit dac675c

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v1.0.38 (TBA)
4+
5+
### Bug fixes
6+
7+
* [`Pow.Ecto.Schema.Changeset`] Fixed issue with schemas having a `:data` field not being handled correctly in changeset functions
8+
39
## v1.0.37 (2024-03-03)
410

511
### Bug fixes

lib/pow/ecto/schema/changeset.ex

+13-4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ defmodule Pow.Ecto.Schema.Changeset do
111111

112112
do_confirm_password_changeset(user_or_changeset, params)
113113
end
114+
114115
def confirm_password_changeset(user_or_changeset, %{"confirm_password" => password_confirmation} = params, _config) do
115116
params =
116117
params
@@ -119,6 +120,7 @@ defmodule Pow.Ecto.Schema.Changeset do
119120

120121
convert_confirm_password_param(user_or_changeset, params)
121122
end
123+
122124
def confirm_password_changeset(user_or_changeset, params, _config),
123125
do: do_confirm_password_changeset(user_or_changeset, params)
124126

@@ -161,9 +163,10 @@ defmodule Pow.Ecto.Schema.Changeset do
161163
|> Changeset.prepare_changes(&Changeset.delete_change(&1, :current_password))
162164
end
163165

164-
defp reset_current_password_field(%{data: user} = changeset) do
166+
defp reset_current_password_field(%Changeset{data: user} = changeset) do
165167
%{changeset | data: reset_current_password_field(user)}
166168
end
169+
167170
defp reset_current_password_field(user) do
168171
%{user | current_password: nil}
169172
end
@@ -181,8 +184,9 @@ defmodule Pow.Ecto.Schema.Changeset do
181184
end
182185
defp maybe_validate_email_format(changeset, _type, _config), do: changeset
183186

184-
defp maybe_validate_current_password(%{data: %{password_hash: nil}} = changeset, _config),
187+
defp maybe_validate_current_password(%Changeset{data: %{password_hash: nil}} = changeset, _config),
185188
do: changeset
189+
186190
defp maybe_validate_current_password(changeset, config) do
187191
changeset = Changeset.validate_required(changeset, [:current_password])
188192

@@ -192,7 +196,7 @@ defmodule Pow.Ecto.Schema.Changeset do
192196
end
193197
end
194198

195-
defp validate_current_password(%{data: user, changes: %{current_password: password}} = changeset, config) do
199+
defp validate_current_password(%Changeset{data: user, changes: %{current_password: password}} = changeset, config) do
196200
user
197201
|> verify_password(password, config)
198202
|> case do
@@ -222,13 +226,15 @@ defmodule Pow.Ecto.Schema.Changeset do
222226

223227
false
224228
end
229+
225230
def verify_password(%{password_hash: password_hash}, password, config) do
226231
apply_password_verify_function(config, [password, password_hash])
227232
end
228233

229-
defp maybe_require_password(%{data: %{password_hash: nil}} = changeset) do
234+
defp maybe_require_password(%Changeset{data: %{password_hash: nil}} = changeset) do
230235
Changeset.validate_required(changeset, [:password])
231236
end
237+
232238
defp maybe_require_password(changeset), do: changeset
233239

234240
defp maybe_validate_password(changeset, config) do
@@ -250,11 +256,13 @@ defmodule Pow.Ecto.Schema.Changeset do
250256
defp maybe_put_password_hash(%Changeset{valid?: true, changes: %{password: password}} = changeset, config) do
251257
Changeset.put_change(changeset, :password_hash, hash_password(password, config))
252258
end
259+
253260
defp maybe_put_password_hash(changeset, _config), do: changeset
254261

255262
defp maybe_validate_password_hash(%Changeset{valid?: true} = changeset) do
256263
Changeset.validate_required(changeset, [:password_hash])
257264
end
265+
258266
defp maybe_validate_password_hash(changeset), do: changeset
259267

260268
defp hash_password(password, config) do
@@ -413,6 +421,7 @@ defmodule Pow.Ecto.Schema.Changeset do
413421
_label, error -> {:halt, error}
414422
end)
415423
end
424+
416425
defp validate_dns_labels({:error, error}), do: {:error, error}
417426

418427
defp validate_dns_label(label) do

test/pow/ecto/schema/changeset_test.exs

+30
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,34 @@ defmodule Pow.Ecto.Schema.ChangesetTest do
418418
assert Changeset.validate_email("john.doe@#{String.duplicate("x", 64)}.com") == {:error, "dns label too long"}
419419
assert Changeset.validate_email("john.doe@#{String.duplicate("x", 64)}.example.com") == {:error, "dns label too long"}
420420
end
421+
422+
defmodule UserDataAttribute do
423+
@moduledoc false
424+
use Ecto.Schema
425+
use Pow.Ecto.Schema
426+
427+
@ecto_derive_inspect_for_redacted_fields false
428+
429+
schema "users" do
430+
pow_user_fields()
431+
432+
field :data, :string
433+
end
434+
end
435+
436+
describe "User struct with data field" do
437+
@password "password"
438+
439+
test "pow_current_password_changeset/2" do
440+
password_hash = Password.pbkdf2_hash(@password)
441+
user = %UserDataAttribute{password_hash: password_hash}
442+
443+
assert UserDataAttribute.changeset(user, %{})
444+
assert UserDataAttribute.pow_changeset(user, %{})
445+
assert UserDataAttribute.pow_verify_password(user, @password)
446+
assert UserDataAttribute.pow_user_id_field_changeset(user, %{})
447+
assert UserDataAttribute.pow_password_changeset(user, %{})
448+
assert UserDataAttribute.pow_current_password_changeset(user, %{"current_password" => @password})
449+
end
450+
end
421451
end

0 commit comments

Comments
 (0)