Permitting specific parameter values #839
-
I have the following problem: if post.published?
# params[:post][:state] must be in 'draft'
elsif post.published? && user.admin?
# params[:post][:state] must be in 'draft', 'deleted'
end Rails does not provide such a mechanism to filter for specific values, and I think that pundit doesn't. This is quite complicated, but I can't work around this requirement. I was thinking to add some helper to my Should I try that? Should I keep it out of authorization? This makes policies a bit more complex, but the methods to check the current user permissions are in the policies, so I think it makes more sense. Should Pundit itself provide something like this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You're right, Pundit itself doesn't specifically guide you in how to deal with this fine-grainedness. It covers much more about who can do which verb to which resource. The past few years at Varvet, we've been writing more and more specialised functions/services/commands. I don't know what the most descriptive term is, but it effectively maps the user's intent to a named action that fits within the business domain. Without going too detailed, instead of having a single
These actions tend to map to actions real users take, in the same manner we speak about these actions when talking to non-developers such as product owners or stakeholders. I'm not saying this is the way to do it, but it's an alternative to inspecting the params and trying to figure out authorization rules for each combination. |
Beta Was this translation helpful? Give feedback.
You're right, Pundit itself doesn't specifically guide you in how to deal with this fine-grainedness. It covers much more about who can do which verb to which resource.
The past few years at Varvet, we've been writing more and more specialised functions/services/commands. I don't know what the most descriptive term is, but it effectively maps the user's intent to a named action that fits within the business domain.
Without going too detailed, instead of having a single
update
action and altering thepermitted_parameters
based on authorization rules, we're creating multiple actions/resources (e.g.users/role#update
,docks/opening_hours#update
). This means each action can be much more granu…