【问题标题】:What is :manage, :all doing in Ruby?什么是 :manage, :all 在 Ruby 中做什么?
【发布时间】:2017-05-26 10:28:49
【问题描述】:

我在 Rails 应用程序中有一个基本授权类,如下所示:

class Ability
  include CanCan::Ability

  def initialize(user)

   if user
     can :access, :rails_admin       # only allow admin users to access Rails Admin
     can :dashboard
     if user.admin?
       can :manage, :all
     else
       can :manage, [Agreement, Attachment, Contact, Deadline, Event, Image, Photo, Project, Submission, Talk]
       can :update, User, id: user.id
     end
   end

   # Current user cannot delete his account
   cannot :destroy, User, id: user.id
  end
end

现在,我在尝试使用简单用户访问仪表板时遇到未经授权的错误,但是一旦我将can :manage, :all 设置为简单的用户条件,它就会莫名其妙地通过并查看仪表板。

什么是:manage, :all:manage, [All_my_tables] 更多,为什么我的用户不允许使用这种方式?

【问题讨论】:

  • 感谢您与我分享 wiki,但我已经浏览过该页面,如果我能在那里找到答案,我不会在这里发布问题
  • “我在尝试访问仪表板时遇到未经授权的错误” -- 这不是因为您写的是can :dashboard 而不是can :read, :dashboard?或者,如果不是,您能否更具体地说明用户未经授权执行的操作? (控制器里有什么?)
  • 只要这适用于管理员,是什么让您认为这是线路失败?我很具体,我用上面的代码说,simple user 无法登录,但是一旦我为简单用户执行can :manage, :all,一切正常
  • :manage:all 具有特殊含义,允许在每个控制器上执行每个操作。因此,管理员当然可以访问仪表板。至于为什么非管理员无法访问,我无法确定(控制器中有什么??)。我的猜测是,为了查看仪表板,您必须拥有:read, :dashboard 权限——您没有授予非管理员用户。

标签: ruby ruby-on-rails-3 authorization cancancan


【解决方案1】:

这是答案,我只需要 :manage, :all 为一个简单的用户,然后覆盖权限。

class Ability
  include CanCan::Ability

  def initialize(user)

     #Check if the user is logged in
     if user
       #Grant access to the dashboard
       can :access, :rails_admin
       can :dashboard
       can :manage, :all

       #Simple user permissions set here
       if !user.admin?
         alias_action :create, :update, :destroy, to: :cud

         can :manage, :all
         cannot :cud, User
         cannot :destroy, [Agreement, Submission]
       end
     end

     can :update, User, id: user.id     #User can edit his/her own account
     cannot :destroy, User, id: user.id #User cannot delete his/her own account
  end
end

感谢投反对票,但在来到这里之前已经对这个问题进行了充分研究

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    相关资源
    最近更新 更多