代码之家  ›  专栏  ›  技术社区  ›  Jason Kim

CanCan+Devise:管理员用户无法获得权限,尽管他应该

  •  1
  • Jason Kim  · 技术社区  · 11 年前

    这是代码。

    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.
    

    为什么会发生这种情况?

    1 回复  |  直到 11 年前
        1
  •  1
  •   Jason Kim    11 年前

    我太笨了。

    user.role? 退货 true 但是 true == "admin" 总是错误的。