2

我正在使用活动管理员(包括设计),并且我已经使用 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
4

1 回答 1

0

我有一种感觉,你正在组合规则。

文件中更靠后的能力规则将覆盖之前的规则。-能力优先级

作为调试步骤,删除除can :manage, :all.

如果您的角色是互斥的,则应使用elsif而不是单独if的 s。

于 2013-06-02T06:02:50.767 回答