我正在为我的应用程序使用cancan
我的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,它可以授权用户并允许他在此控制器中执行某些操作,但我的问题是,当用户以管理员身份登录时,他无法添加新产品,它会给出cancan的访问被拒绝错误。
我的观点是
<% 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 %>它也不显示此链接到管理员,因为有所有的访问权限,但他仍然不能访问此操作?
我还漏掉了什么?
请帮帮忙?
发布于 2011-05-26 15:39:15
你有没有按照cancan维基中的说明操作?https://github.com/ryanb/cancan/wiki/Role-Based-Authorization。
Cancan为每个用户存储角色的默认策略是使用位掩码,但维基在这里提到了一个不同的解决方案:https://github.com/ryanb/cancan/wiki/Separate-Role-Model。
https://stackoverflow.com/questions/6135057
复制相似问题