【问题标题】:manage roles in rails管理 Rails 中的角色
【发布时间】:2016-09-07 06:39:17
【问题描述】:

我想在我的项目中创建角色。每个用户可以是:管理员、注册或演示。每个角色看到不同的东西。 我怎样才能做到这一点?扮演角色的最佳宝石是什么?

这是我想要的“糟糕编程”中的一个例子:

  def index
    if current_user.role[:name] == 'admin'
      @installations = Installation.all
    elsif current_user.role[:name] == 'registered'
      @installations = current_user.installations
    elsif current_user.role[:name] == 'demo'
      @installations = current_user.installations.first
    else
    end
  end

【问题讨论】:

    标签: ruby-on-rails ruby roles


    【解决方案1】:

    有很多解决方案可供您使用。

    从宝石开始:

    https://github.com/RolifyCommunity/rolify

    https://github.com/martinrehfeld/role_model

    通过使用 Devise 架构(如果你使用它):

    https://github.com/plataformatec/devise/wiki/How-To:-Add-a-default-role-to-a-User

    通过在 rails 4 中使用枚举:

    class AddRolesToUser < ActiveRecord::Migration
      #add_column 'role', :integer, default: 0 to the users table
    end
    
    class User < ActiveRecord::Base
      enum role: [:demo, :admin, :registered]
    end
    

    这将启用角色方法。

    user = User.find(1)
    user.role #:demo
    user.admin? #false
    user.registered? #false
    

    因此:

    if user.admin?
      #somethig
    elsif user.registered?
      #another something
    else
      #another another something.
    

    最后但同样重要的是,您搜索的不是管理角色解决方案,而是管理权限解决方案:

    https://github.com/ryanb/cancan

    【讨论】:

      【解决方案2】:

      您可能会感兴趣的一些宝石:

      如果您决定自己实现它,那么在某个页面中您可能想要更改内容,为此您可能想要执行以下操作:

      使用迁移向用户模型添加角色:

      class AddRoleToUsers < ActiveRecord::Migration
        def change
          add_column :users, :role, :string, default: :demo
        end
      end
      

      然后在您的应用中您可以按如下方式使用它:

      def index
        case current_user.role
          when :admin
            @installations = Installation.all
          when :registered
            @installations = current_user.installations
          else 
            @installations = current_user.installations.first
        end
      end 
      

      例如,您也可以简单地创建一个布尔值admin

      您可能还想做的是在模型中创建一些方法,以便您可以调用 current_user.admin?current_user.registered? 。您可以这样做(如果您选择使用字符串来存储角色):

      class User < ActiveRecord::Base
        def admin?
          self.role == "admin"
        end
      
        def registered?
          self.role == "registered"
        end
      end
      

      我看到将角色存储在字符串中的一个优点是,例如,如果您有 5 个角色,那么您就没有 4 个布尔值(就像您将 admin 存储在布尔值中一样),而只有一个字符串。从长远来看,您可能希望实际存储 role_id 而不是字符串,并拥有一个单独的 role 模型。

      Jorge de Los Santos(另一个答案)指出的一个很好的替代方法是使用 enum :

      class User < ActiveRecord::Base
        enum role: [:demo, :admin, :registered]
      end
      

      这是一个很好的替代方案,因为它会自动添加上述方法,例如current_user.admin?,而无需对其进行硬编码。

      使用您的角色,您可能想要进行一些授权(管理员可以访问特定页面,演示用户仅限于页面的子集等)。为此,您可以使用名为 cancancan 的 gem。您可以查看this railscast 以了解更多信息。另外,您可以在这里获得一些信息:How to use cancancan?

      【讨论】:

        【解决方案3】:

        在您的用户模型中添加一个布尔值 :admin

        class AddAdminToUsers < ActiveRecord::Migration
          def change
            add_column :users, :admin, :boolean, deafult: false
          end
        end
        

        为注册用户创建一种方法,将他们与演示用户区分开来,例如验证他们的电子邮件、提供家庭地址和电话号码、填写个人资料等。不过,这取决于您,首先您需要决定注册用户和演示用户应该有何不同。

        【讨论】:

          【解决方案4】:

          CanCan gem 将授权添加到您的项目中,如果您想实现具有不同能力的多个角色,这尤其有用。当与devise 之类的身份验证系统一起使用时,您将获得适用于您网站的一整套功能。

          您可以完全控制要定义的角色以及他们拥有的能力。 CanCan 管理角色的跟踪、分配和查询,然后让您轻松构建所需的内容。

          您可以在 Github 中找到 CanCan gem:https://github.com/ryanb/cancan

          使用简单,文档简单易懂。

          【讨论】:

            猜你喜欢
            • 2011-09-26
            • 1970-01-01
            • 2015-06-13
            • 2011-09-09
            • 1970-01-01
            • 1970-01-01
            • 2017-02-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多