You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I'd like to ask you for help for my Master thesis.
I have User model with arrays groups and permissions and I'd like to create a rule that allows current_user to show a user_resource if the current_user's permissions overlap with user_resource's groups, i.e. (current_user.permissions & user_resource.groups).any? is true.
How do I write a rule in ability.rb for that?
I've tried this:
def initialize(user)
user ||= User.new
can :read, User, groups: user.permissions
end
But it doesn't work. I guess that it would work only if the whole array of groups would match the permissions, but I want it to work if at least one of the elements are the same.
ChatGPT recommended to use a lambda, but it doesn't seem to work for me either. I guess can only accepts a hash. Do you by any chance know if that is the case? It didn't give me an error, just the authorization has failed.
can :read, User do |user|
user.groups.include?(g)
end
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, I'd like to ask you for help for my Master thesis.
I have User model with arrays
groups
andpermissions
and I'd like to create a rule that allows current_user to show a user_resource if the current_user's permissions overlap with user_resource's groups, i.e.(current_user.permissions & user_resource.groups).any?
is true.How do I write a rule in ability.rb for that?
I've tried this:
But it doesn't work. I guess that it would work only if the whole array of groups would match the permissions, but I want it to work if at least one of the elements are the same.
ChatGPT recommended to use a lambda, but it doesn't seem to work for me either. I guess
can
only accepts a hash. Do you by any chance know if that is the case? It didn't give me an error, just the authorization has failed.The groups could be something like this:
and the permissions of a given user to be something like this:
Is there some other (simpler) way? I've briefly looked into https://github.com/martinrehfeld/role_model but it doesn't seem to solve my issue.
Thank you
Beta Was this translation helpful? Give feedback.
All reactions