Rails 3 - 设计:用户路线

发布于 2025-01-07 22:27:30 字数 1698 浏览 4 评论 0原文

我正在使用带有 CanCan 的设备。我正在使用我的用户模型。 我的用户索引页面是 localhost:3000/users (仅针对 :administrator 角色启用)。

问题是 CanCan(或 Devise)没有检查该路由的授权。正在检查所有其他路由(即 localhost:3000/tasks)。即,如果我注销系统并输入用户索引页面,它将显示其内容。如果我输入任务路线,它会将我重定向到登录屏幕(正确的行为)。

我认为这是因为 Devise 的路线而发生的。
我的简化用户模型是:

class User < ActiveRecord::Base
   has_and_belongs_to_many :roles

   # Include default devise modules. Others available are:
   # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and           :omniauthable
   devise :database_authenticatable,
      :recoverable, :rememberable, :trackable, :validatable

    # Setup accessible (or protected) attributes for your model
    attr_accessible :name, :role_ids, :role, :email, :password, :password_confirmation, :remember_me

    def role?(role_check)
      self.roles.each do |role|
        return true if (role.name.eql? role_check.to_s.humanize )
      end    

      return false
    end


     def role=(role_id)
        self.roles.clear
        self.roles << Role.find(role_id)
     end

     def role
         self.roles.first unless self.roles.length == 0
     end

   end

我的路线如下:

devise_for :users
resources :users
devise_for :users,  :controllers => { :registrations => "users/registrations" }

我的能力.rb 如下(我尚未定义,它允许所有):

class Ability
  include CanCan::Ability

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

   if user.role? :administrator
       can :manage, :all

   elsif user.role? :department_header
      can :manage, :all
   elsif user.role? :staff
      can :manage, :all
   end
 end
end

我该如何解决这个问题? 谢谢!

I am using devise with CanCan. I am using my user model.
My user index page is localhost:3000/users (It is enabled just for :administrator role).

The problem is that CanCan (or Devise) is not checking for authorization on this route. All other routes (i.e. localhost:3000/tasks) are being checked. i.e. If I logout of the system and type the users index page it displays its content. If I type the tasks route it redirects me to the login screen (correct behaviour).

I think that this is happening because of Devise's routes.
My simplified User model is:

class User < ActiveRecord::Base
   has_and_belongs_to_many :roles

   # Include default devise modules. Others available are:
   # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and           :omniauthable
   devise :database_authenticatable,
      :recoverable, :rememberable, :trackable, :validatable

    # Setup accessible (or protected) attributes for your model
    attr_accessible :name, :role_ids, :role, :email, :password, :password_confirmation, :remember_me

    def role?(role_check)
      self.roles.each do |role|
        return true if (role.name.eql? role_check.to_s.humanize )
      end    

      return false
    end


     def role=(role_id)
        self.roles.clear
        self.roles << Role.find(role_id)
     end

     def role
         self.roles.first unless self.roles.length == 0
     end

   end

My Routes are the following:

devise_for :users
resources :users
devise_for :users,  :controllers => { :registrations => "users/registrations" }

My ability.rb is the following (I haven´t defined yet, it is allowing all):

class Ability
  include CanCan::Ability

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

   if user.role? :administrator
       can :manage, :all

   elsif user.role? :department_header
      can :manage, :all
   elsif user.role? :staff
      can :manage, :all
   end
 end
end

How can I fix this?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无尽的现实 2025-01-14 22:27:30

我的问题出在以下几行:

load_and_authorize_resource :only => [:show,:new,:destroy,:edit,:update]

我必须包括:index

My problem was on the following line:

load_and_authorize_resource :only => [:show,:new,:destroy,:edit,:update]

I have to include :index

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文