【问题标题】:How to do authorization with CanCan when I have a User and a separate Admin当我有一个用户和一个单独的管理员时如何使用 CanCan 进行授权
【发布时间】:2014-01-12 18:28:47
【问题描述】:

我的应用程序使用不同的模型来管理用户和管理员。使用 CanCan 对这些模型中的每一个实施授权的最佳方式是什么?

【问题讨论】:

    标签: ruby-on-rails authorization cancan


    【解决方案1】:

    您将需要在您的 cancan 能力文件中测试用于授权的对象类型。

    类似于以下内容:

    class Ability
      include CanCan::Ability
    
      def initialize(user_or_admin)
    
        case user_or_admin.class.name
        when 'User'
          can :read, :post
        when 'Admin'
          can :manage, :all
        else
          user = User.new # guest user
          # ... other permissions for guests
        end
    
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多