Having a small policy that doesn't seem to ever be called
I have a policy where I need an actor to be present for three different actions, but the policies don't seem to be called.
The policies:
Code to test:
The code in the
:create_invite
-action assumes that a property on the actor exists, and since the actor is nil
everything explodes in a KeyError
.
Have I done any obvious mistake in the policies?Solution:Jump to solution
yeah, in this context, the policies don't guarantee an actor. you could wrap this in before_action and than you'd know that if you reach the hook that there is an actor beccause of the policy, or you could just handle the actor being nil
case Map.get(context.actor || %{}, :group_id) do
which i would probably do6 Replies
what code?
changes are run before auth, hooks are not
Oh. Got it.
Okay, what I'm really trying to do is to get the policies to return a ForbiddenError when there's no authenticated GraphQL user.
Does changes run before auth for ash_graphql too?
ash_graphql is calling the actions the same way, there is always a changeset constructed first, which runs the changes and than the action is executed, auth happens before execution
what does your change code look like?
Got it. My change code looks like this:
Solution
yeah, in this context, the policies don't guarantee an actor. you could wrap this in before_action and than you'd know that if you reach the hook that there is an actor beccause of the policy, or you could just handle the actor being nil
case Map.get(context.actor || %{}, :group_id) do
which i would probably doThank you. Then I've misunderstood when policies are run and how they work.