2013-03-28 58 views
1

以下是代碼。CanCan + Devise:管理員用戶無法獲得權限,即使他應該

class WebsitesController < ApplicationController 
    load_and_authorize_resource 

    ... 
end 
class ApplicationController < ActionController::Base 
    ... 

    check_authorization :unless => :do_not_check_authorization? 

    private 
    def do_not_check_authorization? 
     respond_to?(:devise_controller?) 
    end 
end 
class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.new 
    if user.role? == "admin" 
     can :manage, :all 
    elsif user.role? == "developer" 
     can :manage, :websites 
     can :manage, :pages 
     can :manage, :templates 
    elsif user.role? == "editor" 
     can :manage, :pages 
    end 
    end 
end 

從它的外觀,具有管理員角色的用戶應該能夠因爲can :manage, :all網站負責人做任何事情。

但是,當我打的網站/指數,我得到

CanCan::AccessDenied in WebsitesController#index 

You are not authorized to access this page. 

這究竟是爲什麼?

+0

嗨!你是否在用「rolify」來管理用戶的角色? – jvnill 2013-03-28 00:50:16

+0

@jvnill,我解決了我的問題。它在下面的答案中看到了我的愚蠢錯誤。 – 2013-03-28 00:51:25

回答

1

我很笨。

I user.role?返回true但是true == "admin"總是錯誤的。

+0

是的,這也是我的想法(不是啞巴部分:p)。我以爲你可能正在看像'user.role?('admin')'而不是你問題中的代碼:) – jvnill 2013-03-28 00:53:43

相關問題