-
Notifications
You must be signed in to change notification settings - Fork 633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Before/after hooks on authorize and policy_scope #474
Comments
Can you give an example of something you'd like to place in the |
Hi @tom-lord @WaKeMaTTa. Sorry for not responding sooner, must have gotten lost in my inbox. It's been 2 months since I posted this issue, and for the life of me I can't remember what I was trying to do and why I was requesting this. Clearly I was able to work around it some other way. Feel free to close/"wont_fix" this issue. |
Hi, let us imagine a case where we have two different roles, a plain user and a supervisor. User is able to access to his own resources, in this sense I am checking on each policy that current_user == resource.owner. However, supervisor only can access to resources from another user, and he CANNOT access to his own resources. To the best of my knowledge it means that I should add in each line as the previous one: current_user == resource.owner && !current_user.supervisor? Am I right? I think that it could be interesting to have a hook, where I could put sth like that:
|
@pacop That example sounds a little contrived; but regardless, you could code that trivially in 1 line, without any additional Pundit extension, by use of the XOR operator:
|
@tom-lord is a real example, I came to this issue because I gonna open a new one but I search first. I can find many examples: a blog where the supervisor cannot create posts but it can accept or refuse posts, a consultant reviewing invoices from another user where he cannot create invoices.... Yeah I know that it solves the problem, but my project has so many policies that add this line to each policy could perfectly by two day of work. When If I were able to have a hook, the problem would be solved in only one line and in one unique place, much faster and elegant |
So a supervisor is not allowed to
So a consultant is not allowed to
If the policy logic is identical, then you could consider using some form of composition/inheritance in the policy class, or a helper method, to remove repetition. For example:
Or even, if you feel the need to take the abstraction further, for example:
Regardless, what you are suggesting as an alternative solution would be to add a
This issue was requesting a policy hook, which - as far as I can tell - is not really what you are suggesting. |
You are completely right, in this example is pretty easy to discern. In real life anything is that easy.
Regarding your first example, I know that perfectly works but in this case I still have to go in each policy adding this chunk of code. This part is which I am trying to avoid since I want to apply this restriction always, so for me it does not make sense having to repeat the same call. What happens if I forget one policy?
Composition is not a option since I still have to go for each policy including the module or doing whatever is required to avoid repetition.
This very example is what I am trying to do. Now I see that you know what I am trying to achieve. But If you use a controller you would be mixing your policies with controllers which it is not a good practice. I just was trying to do this but with ApplicationPolicy.
For me this solution is much more elegant don't you think? Let me know your thoughts in that case, BTW thanks for being open to discuss. |
I get what you're trying to achieve, but I still feel it would be relatively easy to achieve with vanilla ruby patterns. For example:
Or if you want to write even less code, how about using
Given this is a relatively unusual requirement, I'm not personally convinced it warrants additional functionality to the Pundit library, which prides itself in being as minimal as possible. |
If anyone is interested in solving this problem, I have implemented a solution in pundit-before gem. |
As there is one policy file per model (and an application_policy) file, there is currently no easy way to set up standard setup/teardown behavior in all policy files if the individual methods are overwritten. I'd like to propose adding before/after hooks that are called as part of the normal life cycle of evaluating a policy. E.g. very simplistically it would be something like
I'd suggest having multiple hooks so that there is a top level before/after (as shown above) and then auto_generated before_[query] methods that can also be implemented.
The text was updated successfully, but these errors were encountered: