2012-02-21 57 views
1

我正在使用CanCan設計。我正在使用我的用戶模型。 我的用戶索引頁是localhost:3000/users(它僅適用於:管理員角色)。Rails 3 - 設計:用戶路線

問題是,CanCan(或設計)不檢查此路線上的授權。所有其他路由(即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" } 

我ability.rb是下述(I haven't定義是,它是允許所有):

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 

我該如何解決這個問題? 謝謝!

+1

顯示您的用戶的路線和能力模型,請 – 2012-02-21 19:17:37

+0

貼.... 我認爲這個問題是在我路線,你怎麼看? – Tony 2012-02-21 19:32:39

+1

好的,你能告訴你如何驗證控制器中的用戶? – 2012-02-21 19:35:06

回答

2

我的問題是以下行:

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

我必須包括:指數