我正在使用活动管理员(包括设计),并且我已经使用 cancan 来设置我的角色。我有一个订单模型。我设置了几个角色。我需要某些角色才能查看所有用户的整个订单索引,然后我需要某些用户仅查看他们的订单,而无法访问其他用户的订单。
在我的订单控制器中,我有这个......
scope_to :current_user, :unless => proc{ current_admin_user.admin? }
我从文档中汲取了这一点,但它只显示了管理员订单,而不是索引中的每个订单。
这是我的能力模型
class Ability
include CanCan::Ability
def initialize(user)
return if user.nil? #non logged in user can use this.
if user.admin?
can :manage, :all
end
if user.sales?
can [:create, :update] [Order, Customer]
can :read, :all
cannot :destroy, :all
end
if user.production?
can [:create, :update] [Order, Customer]
can :read, :all
cannot :destroy, :all
end
if user.art?
cannot :create, :all
can :read, :all
can :update, Order
cannot :destroy, :all
end
if user.broker?
can [:index, :create, :edit, :update, :read], Order, :id => user.id
can [:index, :create, :edit, :update, :read], Customer, :id => user.id
cannot :destroy, :all
end
if user.shipping?
cannot :create, :all
can :read, :all
can :update, Order
cannot :destroy, :all
end
end
end