Policy for explicitly setting relationship on create
I have a number of resources that are
is_a
other resources. That is:
When these resources are created, they optionally accept an:
I'd like to do two things:
1. Be able to roll the automatic creation of the parent into something like above. Can I pass change
an MFA?
2. Be able to create policies that determine when one is allowed to explicitly set the parent while creating. For example, to specify only users with certain roles should be allowed to explicitly set the parent. Something like:
17 Replies
Note that this second policy needs to not conflict with the fact that the parent will be changed either way by the
changes
hook, so maybe something like has_argument
would be a simpler way to represent this.You can pass change a module that is an
Ash.Resource.Change
with opts, i.e {Module, opts}
And I think your policy might work basically as you've stated it.
although deny_unless never()
is unnecessary
if a policy isn't explicitly authorized, then it is forbiddenThe
deny_unless never()
is to prevent interference by following policies, just a question of ordering.
So doing it as an after_action
hook is the way to keep it from being picked up by the changing_relationship
policy?
I was thinking it might be a before_action
otherwise it might choke on the allow_nil?
or the foreign key constraint.thats not how policies work FYI
all policies that apply to a request must pass, regardless of ordering
I actually meant to do it in a
before_action
sorry 😆But the fact that it's in a hook means it should escape the notice of the
changing_relationship
policy? Right on.Oh, yeah thats true
Yeah that's roughly where I'm at 🙂
If you write a generic change like the one listed above you can really simplify that
change {ManageParent, rel: :parent}
True, but it is pretty much just for this macro 😂
And your point was correct actually you don't want to do it in a
before_action
hook
your choice at the end of the day ¯\_(ツ)_/¯
Because then the policy will never know. Policies are about the changeset before hooks have executed.Right, if it wouldn't be picked up in either, probably makes sense to do it in the
before_action
hook to avoid violating constraints. The parent can exist before the child but not the other way around.it will be picked up with the example I gave
Because
changing_relationship
looks for the structure added by manage_relationship
Ah...ok.
and
manage_relationship
does the bulk of the work in hooks
So its "the best of both worlds"
You can use policies about it, but the work is dispatched for laterWhich one was going to not be noticed by the policy then, though?
using an explicit
before_action
hook and doing the work in that hook
either manually or with manage_relationship
Ah, referring to this one, which doesn't use a hook at all, so it would be picked up.
Righto, fantastic. Thanks a bunch!