使用 cancan 阻止访问控制器

发布于 2024-10-09 18:24:50 字数 537 浏览 5 评论 0原文

我有一个管理控制器,我希望只有定义为管理员的用户才能访问该控制器。

我的能力类别:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜嗑 2024-10-16 18:24:51

这是因为当您使用 load_and_authorize_resource 时,您的控制器必须由名为 Admin 的模型支持(因为您的控制器称为 AdminController)。因此,您需要创建此模型或将 load_and_authorize_resource 替换为:

授权资源:类=>错误

会导致针对您的操作而不是模型进行访问检查。请注意,不幸的是,这会导致通用访问符号(例如 :manage:read)停止工作,要求您直接在 ability.rb 中引用控制器操作>:

可以[:index,:users_list],:admin

其中第一个参数是用户可以访问的控制器操作数组,第二个参数是控制器的短名称

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:

authorize_resource :class => false

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:

can [ :index, :users_list ], :admin

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

半葬歌 2024-10-16 18:24:51

您可以在控制器中添加授权

authorize_resource :class => false

,或者

authorize_resource :class => :controller

然后更改您的 app/models/Ability.rb 文件

can :manage, :controller_name

请参阅 这个

You can put authorization in your controller

authorize_resource :class => false

or

authorize_resource :class => :controller

Then change your app/models/Ability.rb file

can :manage, :controller_name

See this

遥远的她 2024-10-16 18:24:51

检查此项以添加非 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.

空城旧梦 2024-10-16 18:24:51

这是因为当您创建通用控制器时,放入 load_and_authorize_resource ,然后应用程序控制器或管理控制器无法找到它来自的实际类,如本例所示。这会起作用。

class Admin::HomeController < Admin::ApplicationController
    def home    
    end
  end

  class Admin::ApplicationController < ApplicationController
    load_and_authorize_resource :class => self.class
  end

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.

class Admin::HomeController < Admin::ApplicationController
    def home    
    end
  end

  class Admin::ApplicationController < ApplicationController
    load_and_authorize_resource :class => self.class
  end
清醇 2024-10-16 18:24:51

我有同样的问题。我的解决方案是定义一个不能的能力。我不再定义只有管理员可以执行的操作,而是定义非管理员用户不能执行的操作。在您的代码中将是这样的:

管理控制器:

class AdminController < ApplicationController
 authorize_resource :class => AdminController

 def index
 end

 def users_list
 end
end

capability.rb:

class Ability
 include CanCan::Ability

 def initialize(user)
  if user.admin?
   can :manage, :all
  else
   can :read, :all
   cannot [:index, :users_list], AdminController
  end
 end
end

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:

class AdminController < ApplicationController
 authorize_resource :class => AdminController

 def index
 end

 def users_list
 end
end

ability.rb:

class Ability
 include CanCan::Ability

 def initialize(user)
  if user.admin?
   can :manage, :all
  else
   can :read, :all
   cannot [:index, :users_list], AdminController
  end
 end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文