开发者

declarative authorization and has_and_belongs_to_many

开发者 https://www.devze.com 2023-02-11 01:29 出处:网络
I have a little problem with declarative-authorization. I have a User and Role Model with a has_and_belongs_to_many association.

I have a little problem with declarative-authorization. I have a User and Role Model with a has_and_belongs_to_many association.

I've created a Role named :moderator in my authorization_rules.rb

Is it possible that a User with the Role Moderator only gets the Users that have the Moderator Role开发者_如何学运维 assigned to it?? --> User.with_permissions_to(:index)

I thought it would be possible like that:

role :moderator do
  has_permission_on :users, :to => :index do
    if_attribute :roles => contains { ????? }
  end
end

I also created a named_scope in my User Model because I thought it would help...

class User
  has_and_belongs_to_many :roles
  named_scope :by_role, lambda { |role|
    {
      :include => :roles,
      :conditions => {"roles.name" => role}
    }
  }
end

Does anyone knows if it's possible to do this with declarative_authorization?

Thanks for your help!


I did something similar in one of my projects but found dec_auth really confusing at the time. I think this is what you need to do:

authorization_rules.rb:

role :moderator do
  has_permission_on :users, :to => :index
end

User Model:

class User < ActiveRecord::Base
  using_access_control
end

Controller:

@users = User.with_permissions_to(:index)

Let me know if that doesn't work.

0

精彩评论

暂无评论...
验证码 换一张
取 消