使用 cancan 阻止访问控制器
我有一个管理控制器,我希望只有定义为管理员的用户才能访问该控制器。
我的能力类别:
class Ability
include CanCan::Ability
def initialize(user)
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
我的管理控制器:
class AdminController < ApplicationController
load_and_authorize_resource
def index
end
def users_list
end
end
当我尝试访问 /admin/users_list
(无论是否有管理员用户)时,我收到以下错误:未初始化的常量 Admin
什么我做错了吗?这是限制对控制器的访问的正确方法吗?
I have an admin controller and I want that only users that are defined as admin would have access to that controller.
my ability class:
class Ability
include CanCan::Ability
def initialize(user)
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
my admin controller:
class AdminController < ApplicationController
load_and_authorize_resource
def index
end
def users_list
end
end
when i try to access /admin/users_list
(either with an admin user or without) i get the following error: uninitialized constant Admin
What am I doing wrong? Is that the right way to restrict access to a controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是因为当您使用 load_and_authorize_resource 时,您的控制器必须由名为 Admin 的模型支持(因为您的控制器称为 AdminController)。因此,您需要创建此模型或将 load_and_authorize_resource 替换为:
会导致针对您的操作而不是模型进行访问检查。请注意,不幸的是,这会导致通用访问符号(例如 :manage 和 :read)停止工作,要求您直接在 ability.rb 中引用控制器操作>:
其中第一个参数是用户可以访问的控制器操作数组,第二个参数是控制器的短名称
This is because when you are using load_and_authorize_resource your controller must be backed by a model named Admin (since your controller is called AdminController). Thus you need to either create this model or replace load_and_authorize_resource with:
which causes the access checks to be made against your actions rather than the model. Note this unfortunately causes the generic access symbols such as :manage and :read to stop working requiring you to refernce the controller actions directly in ability.rb:
where the first argument is a array of controller actions the user can access and the second argument is the short name of the controller
您可以在控制器中添加授权
,或者
然后更改您的 app/models/Ability.rb 文件
请参阅 这个
You can put authorization in your controller
or
Then change your app/models/Ability.rb file
See this
检查此项以添加非 RESTful 控制器的授权规则:
https://github。 com/ryanb/cancan/wiki/Non-RESTful-Controllers
希望这会有所帮助。
Check this to add authorization rules for non-restful-controllers:
https://github.com/ryanb/cancan/wiki/Non-RESTful-Controllers
Hope this helps.
这是因为当您创建通用控制器时,放入 load_and_authorize_resource ,然后应用程序控制器或管理控制器无法找到它来自的实际类,如本例所示。这会起作用。
This is because when you make a generic controller, put load_and_authorize_resource then the application controller or admin controller could not find the actual class which it come from like this example. This will work.
我有同样的问题。我的解决方案是定义一个不能的能力。我不再定义只有管理员可以执行的操作,而是定义非管理员用户不能执行的操作。在您的代码中将是这样的:
管理控制器:
capability.rb:
I had the same issue. My solution was define a cannot ability. Insted of define what only the Admin can do, I'm defining what the no admin user can not do. In your code would be something like this:
Admin controller:
ability.rb: