Reputation: 10146
I have the following join tables and (relevant models and tables)
For a given user, permission and object, I would like to like to find if user has an associated permission for an object via its roles.
Any help with the relevant query would be much appreciated.
Upvotes: 1
Views: 2262
Reputation: 64363
Try this:
class User
def has_permission?(permission, object)
RoleUser.joins(:permission_roles).where(
:roles_users => {
:user_id => id
},
:permission_roles => {
:permission_id => permission,
:object_type => object.class.base_class.name,
:object_id => object
}
).exists?
end
end
Now you can check the permission as follows:
current_user.has_permission?(@edit_permission, @blog_post)
Upvotes: 0
Reputation: 4398
Given you have the user and the object, how about this:
allowed = user.roles.joins(:permissions => :object).where(:object => { :id => object.id }).any?
Upvotes: 1