AE
Ash Elixir•2y ago
Blibs

Policy for action stopped being called

In my User resource, I have this action:
update :confirm_phone_number do
alias Actions.ConfirmPhoneNumber.Changes

accept []

argument :code, :string, allow_nil?: false

change Changes.VerifyCode

change set_attribute(:confirmed_at, DateTime.utc_now())

change Changes.SendEmail
end
update :confirm_phone_number do
alias Actions.ConfirmPhoneNumber.Changes

accept []

argument :code, :string, allow_nil?: false

change Changes.VerifyCode

change set_attribute(:confirmed_at, DateTime.utc_now())

change Changes.SendEmail
end
And also this policy for it:
alias Marketplace.Accounts.User.Checks

bypass AshAuthentication.Checks.AshAuthenticationInteraction do
authorize_if always()
end

policy action(:confirm_phone_number) do
forbid_unless Checks.NotConfirmedYet
authorize_if always()
end
alias Marketplace.Accounts.User.Checks

bypass AshAuthentication.Checks.AshAuthenticationInteraction do
authorize_if always()
end

policy action(:confirm_phone_number) do
forbid_unless Checks.NotConfirmedYet
authorize_if always()
end
I expect that the NotConfirmedYet policy will be called when running User.confirm_phone_number(user, %{code: "1234"}), but it isn't. I'm pretty sure this was working before, so I'm not sure if it is a bug in the new versions of Ash (I'm using the latest 2.9.11) or something else I messed up.
1 Reply
Blibs
BlibsOP•2y ago
Here is the check implementation:
defmodule Marketplace.Accounts.User.Checks.NotConfirmedYet do
@moduledoc false

use Ash.Policy.SimpleCheck

def describe(_), do: "actor account is not confirmed yet"

def match?(%{confirmed_at: nil}, _, _), do: true
def match?(%{confirmed_at: _}, _, _), do: false
end
defmodule Marketplace.Accounts.User.Checks.NotConfirmedYet do
@moduledoc false

use Ash.Policy.SimpleCheck

def describe(_), do: "actor account is not confirmed yet"

def match?(%{confirmed_at: nil}, _, _), do: true
def match?(%{confirmed_at: _}, _, _), do: false
end
Ah, turns out it was something stupid from my side, I totally forgot to add the actor 😅

Did you find this page helpful?