2011-05-26 129 views
0

我使用康康舞我的应用程序惨惨授权问题

我ability.rb类是

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.new # guest user 

    if user.role? :admin 
     can :manage, :all 
    elsif user.role? :operations 
     can :manage, :all 
    elsif user.role? :customer_support 
     can :read, :all 
    else 
     user.role? :marketing 
    can :read, :all 
    end 
end 
end 

,我在user.rb

def role?(role) 
    self.roles.include? role.to_s 
    end 

我还加 load_and_authorize_resource添加方法在我的控制器中说products_controller可以授权用户,并允许他在此控制器中执行某些操作, 但我的问题是当用户获取以管理员身份登录作为角色,他无法添加新产品,因此会导致拒绝访问被拒绝。

我的看法是

<% if can? :create, Product %> 
       <td class="action"><%= link_to 'Show', product %></td> 
       <td class="action"><%= link_to 'Edit', edit_product_path(product) %></td> 
       <td class="action"><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method => :delete %></td> 
      <% end %> 

也未显示此链接到admin作为有所有进入管理但他仍然不能访问这个动作?

我还缺少什么?

plz help?

回答

1

你是否按照cancan wiki中的说明操作? https://github.com/ryanb/cancan/wiki/Role-Based-Authorization

Cancan为每个用户存储角色的默认策略是使用位掩码,但wiki在这里提到了不同的解决方案:https://github.com/ryanb/cancan/wiki/Separate-Role-Model

+0

Greate在https://github.com/ryanb/cancan/wiki/Separate-Role-Model找到答案感谢您使用cancan进行用户和角色之间的多对多关联。感谢帮助 – 2011-05-26 07:55:39