-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Allow to reset authorization context #265
Comments
I found the answer to the second suggestion :
The method
The first suggestion is the only one remaining 😥 |
This seems related to #217. I do this as a workaround: https://github.com/keygen-sh/keygen-api/blob/master/app/controllers/application_controller.rb#L74 |
@ezekg Thanks, I've already found this issue before posting this one (I should have referenced it 🙇) |
Yeah, we want to support the case when you want to override only a part of the context via the I wonder if we should skip memoization in case an explicit |
That was my first (or second) thought but since it was implemented with memoization and documented as such, It will introduce breaking changes. I also thought about another workaround without using private variables, which is to update the current context: authorize! to: :switch?, account, with: AccountPolicy # context: { user: current_user }
authorization_context[:account] = account
authorize! to: :index?, with: UserPolicy # context: { user: current_user, account: current_account } |
I don't think we have authorization context memoization documented, so it should be rather safe change. |
You're right, memoization is not mentionned.
PR is on the way |
Tell us about your environment
Ruby Version: 3.2.1
Framework Version: Rails 7.0.8.1
Action Policy Version: 0.6.9
Reproduction Script: https://gist.github.com/inkstak/ee0d954288aae6b624ae5d7b68bb8748
What did you do?
I need 2 values in my authorization context :
current_user
¤t_account
But before assigning a
current_account
, I first need to check if I can switch to itSo, in the same controller request, I have to call two policies :
The first one is a authorization checks before assigning the
current_account
.At this time,
current_account
is not yet available in the current context :The second one is a "regular" check.
Like most of authorization checks, it requires a
current_account
to be available in the current context :What did you expect to happen?
I should be able to do a first check without
account
context and perform any subsequent checks withaccount
context.What actually happened?
When doing the first check, ActionPolicy memoizes the context without
account
.When it comes to the second check, the
account
is still missing from the memoized context :What workarounds have you tried?
My current workaround is to reset memoization of the current context after setting
current_account
:.. but it's not ideal as it depends on a private instance methods
I've also tried to explicitely pass the context in the first check :
... but somehow, the explicit context seems to be memoized anyway
What do you suggest?
I have 2 suggestions :
The first and easier one is, like my workaround, to add a public method to reset context :
The second would be to skip memoization when an explicit context is passed.
However, I cannot imagine the downsides or side effects...
The text was updated successfully, but these errors were encountered: