Skip to content
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

Add policy_class to permitted_attributes #789

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

hobbypunk90
Copy link

Fixes #742 again 😅

Sorry, i cleaned my github a bit too fast.

hobbypunk90 and others added 4 commits November 1, 2023 09:04
Signed-off-by: Marcel Hoppe <hobbypunk90@users.noreply.github.com>
Signed-off-by: Marcel Hoppe <hobbypunk90@users.noreply.github.com>
Signed-off-by: Marcel Hoppe <hobbypunk90@users.noreply.github.com>
@@ -80,7 +80,8 @@ def authorize(user, possibly_namespaced_record, query, policy_class: nil, cache:
policy = if policy_class
policy_class.new(user, record)
else
cache[possibly_namespaced_record] ||= policy!(user, possibly_namespaced_record)
cache[{ policy_class: policy_class,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always nil here. What's the intention?

@Burgestrand
Copy link
Member

Hi! Thanks for the PR!

I posted a comment in the ol' issue #742 (comment) about what I think we should probably do.

I'd rather avoid having to modify other methods when adding support for this, to avoid breaking existing code.

By adding a policy_class: nil parameter to permitted_attributes and only altering behaviour if that is passed we can probably add this feature without risking altering behaviour of existing code.

def policy(record)
policies[record] ||= Pundit.policy!(pundit_user, record)
def policy(record, policy_class: nil)
policies[{ policy_class: policy_class, record: record }] ||= if policy_class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While policies is technically marked as private, I'm cautious against modifying its behaviour/content.

We maybe should cache when policy_class is passed, but that's a bigger consideration than adding the feature to permitted_attributes.

I believe that the cache behaviour of authorize(..., policy_class: SomePolicy) should be the same for permitted_attributes(..., policy_class: SomePolicy), and today the behaviour is to entirely bypass the cache.

Copy link
Member

@Burgestrand Burgestrand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I posted some comments :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add policy_class parameter to permitted_attributes function
3 participants