【问题标题】:Cancan super and admin abilities not workingCancan 超级和管理员功能不起作用
【发布时间】:2014-01-23 17:27:10
【问题描述】:

我有一个有两个角色的应用程序,超级和管理员。超级可以做所有事情,管理员应该可以做除类别之外的所有事情。我已经实现了以下功能,但它仍然允许管理员访问类别:

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

  if user.has_role? :super
    can :manage, :all
  elsif user.has_role? :admin
    cannot :manage, :categories
    can :manage, :all
  end
end

如果我将其更改为以下内容,它会将管理员锁定在一切之外。

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

  if user.has_role? :super
    can :manage, :all
  elsif user.has_role? :admin
    can :manage, :all
    cannot :manage, :categories
  end
end

我的所有控制器中都有load_and_authorize_resource,但仍然无法弄清楚。

【问题讨论】:

    标签: ruby-on-rails devise ruby-on-rails-4 cancan


    【解决方案1】:

    规则最终总结在一个数组中。

    在您的第一个示例中,当您使用cannot 时,此类规则已从数组中删除。但是你定义了can :manage, :all,所以被删除的恢复了。

    在后面的例子中,cannot 被放在最后,所以删除实际上已经生效了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-23
      • 1970-01-01
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      相关资源
      最近更新 更多