首页 > 解决方案 > 如何在 rails_admin 中隐藏特定类型的用户?

问题描述

我在我的 Rails 应用程序中使用 Rails Admin gem 作为管理仪表板,它运行良好并显示了我的所有模型。我正在使用设计进行身份验证并有两种类型的用户,注册和设计访客用户的用户(用户被保存直到他们注册或使用示例电子邮件作为访客用户登录)。在 rails admin Users 部分,它显示了所有用户,甚至是访客用户。我想从那里隐藏这个来宾用户。

我尝试了什么: user.rb

scope :verified, -> { where(guest: false) }

rails_admin.rb

config.model User do
    list do
      scopes [:verified]
    end
  end

它不起作用...有什么方法可以在这里使用永久过滤器,因为我的普通用户有很多字段,例如位置和用户名...所以我想做一些事情,比如只显示有用户名的用户。

标签: ruby-on-railsdeviserails-admin

解决方案


您尝试的将过滤列表中的来宾用户,而不是对列表本身的访问。Rails 管理员推荐的解决此问题的方法是使用授权框架,请参阅 cancancan 文档以使其正常工作。

https://github.com/sferik/rails_admin/wiki/Cancancan

我成功地为每个用户配置了这个。

它如何工作的示例:

class Ability
  include CanCan::Ability
  def initialize(user)
    return unless user && user.admin?
    can :access, :rails_admin       # only allow admin users to access Rails Admin
    can :read, :dashboard           # allow access to dashboard
    if user.role? :superadmin
      can :manage, :all             # allow superadmins to do anything
    elsif user.role? :manager
      can :manage, [User, Product]  # allow managers to do anything to products and users
    elsif user.role? :sales
      can :update, Product, hidden: false  # allow sales to only update visible products
    end
  end
end

推荐阅读