什么是:管理,:都用Ruby做?
我在Rails应用程序中有一个基本授权类,如下所示:什么是:管理,:都用Ruby做?,ruby,ruby-on-rails-3,authorization,cancancan,Ruby,Ruby On Rails 3,Authorization,Cancancan,我在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, :al
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,[所有我的表]
,为什么不允许我的用户使用这种方式?以下是答案,我只需要对一个简单的用户: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
谢谢你的否决票,但是这个问题在来这里之前已经被仔细研究过了谢谢你分享我的维基,但是我已经浏览了这个页面,如果我能在那里找到我的答案,我不会在这里发布问题“我在尝试访问仪表板时遇到未经授权的错误”--这不是因为您编写了
can:dashboard
,而不是can:read,:dashboard
?或者,如果没有,您能否更具体地说明用户未经授权的行为?(控制器中有什么?)?我非常具体,我说通过使用上面的代码,简单用户不能登录,但是一旦我登录,可以:管理,:all
对于一个简单用户,所有的工作:manage
和:all
都有一个特殊的含义,允许每个控制器上的每个操作。因此,管理员当然可以访问仪表板。至于为什么非管理员不能访问,我不能确定(控制器中有什么??)。我的猜测是,为了查看dashbaord,您必须拥有:read,:dashboard
权限——您没有将该权限授予非管理员用户。