AE
Ash Elixir•2y ago
moxley

Parameterized custom policy checks?

Is there a way to pass a parameter to a SimpleCheck implementation module? Across most resources in my app, several checks need to be performed. Many of these checks are invariable, and can be put into a SimpleCheck module, but but one checks is variable, depending on a case-by-case basis. I would like to define one SimpleCheck module that does all these checks, and pass a parameter to it to handle that one variable case. Otherwise, I have to define repetitive checks in each of the resource modules. Is there a solution for this?
4 Replies
moxley
moxleyOP•2y ago
The my policies can look like right now is this:
policies do
policy always() do
forbid_unless ActiveMemberPolicy
authorize_if expr("events" in ^actor(:roles) or "super" in ^actor(:roles))
end
end
policies do
policy always() do
forbid_unless ActiveMemberPolicy
authorize_if expr("events" in ^actor(:roles) or "super" in ^actor(:roles))
end
end
But I'd like to do something like this:
policies do
policy always() do
authorize_if CanPerformAsRolePolicy, role: :events
end
end
policies do
policy always() do
authorize_if CanPerformAsRolePolicy, role: :events
end
end
I don't want to repeat the check for the :super role and active member status. I only want one check that takes a parameter.
ZachDaniel
ZachDaniel•2y ago
{Check, opts} Then you should get opts as the third argument to match?/3 🙂
moxley
moxleyOP•2y ago
Like this?
policies do
policy always() do
authorize_if {CanPerformAsRolePolicy, role: :events}
end
end
policies do
policy always() do
authorize_if {CanPerformAsRolePolicy, role: :events}
end
end
ZachDaniel
ZachDaniel•2y ago
yep!

Did you find this page helpful?