Use policies to limit allowed values of Enum in create action
This is a follow-up to my question yesterday. Again, I have this Enum:
I realized what I really wanted to build was an invitational system, where
:superadmin
s can invite people with any role, wheras :admin
s can only invite users with a role of :farmer
and :farmhand
. This is my invitation resource so far:
(Continued in comments)12 Replies
The relevant line is the validation of the
:create
action. Can I conditionally apply the validation given the actor performing the action? Would it instead be more appropriate to define a second action, e.g. :create_admin_invitation
which doesn't have the limitation but is restricted to :superadmin
users with policies?
And, as an aside, does it even make sense to have this as a separate resource, or would I be better off hooking into the token system of ash_auth somehow?You can do it conditionally if you create your own Custom Validation
what exacly are you doing with the invitation? I would probably combine invitations with magic links.
Like you can disable registration for magic link in ash_authentication and then you would just need to create a user send a link and they would just sign in with magic link then.
You don't really control the time between when you send the invitation and when the user does something with it, so having tokens that live that long might not be the best idea
You probably want two policies
If you want admins and super_admins to do anything, use a bypass
I think this should work
@barnabasj What do you think?
expr don't work with creates
oh damn, I forgot
lol
Have to use the SimpleCheck
That would work
Yeah, create a simple check module that takes the role or role as opts. Then you can return true or false
validation vs policy, you could argue both ways I think
Yeah, either can work. I like going with Policies whenever possible because then you can leverage Ash.can? for showing actions in the UI
Solution
Give the SimpleCheck a go and let us know if it helps
Thanks! Walking the dog right now, will report back later 🙂
Alright, so I ended up with a
SimpleCheck
like this:
And I removed the validation from the create action and updated the policies to the following:
Seems to work like it should!
This is a good point. I could probably add an expires_at
that defaults to 72 hours or something, but maybe hooking into the magic link is easier? It is an invite-only service, and invitations should be sent by mail with, yeah, a magic link. When clicking it the user will be prompted to fill out their profile and register.Glad you got it working!