ruby-on-rails - 如何在 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
它不起作用...有什么方法可以在这里使用永久过滤器,因为我的普通用户有很多字段,例如位置和用户名...所以我想做一些事情,比如只显示有用户名的用户。
解决方案
您尝试的将过滤列表中的来宾用户,而不是对列表本身的访问。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
推荐阅读
- java - RecyclerView 在滚动到最后一个 Item 时崩溃
- r - R 计算结果不超过阈值
- featherlight.js - Featherlight Image Count Index/Total [索引错误]
- ios - iOS accessibility: what are the pros/cons for hardcoding "double tap to activate" as hint?
- dataframe - 在 R 中编写 for 循环与 Stata 有何不同?
- android - 如何处理对依赖于 Firebase 身份验证状态的 LiveData 的过早观察以进行身份验证?
- asp.net-core - 为什么在集成测试期间我从 NLog 收到此错误:找不到 LayoutRenderer
- ios - 即使我们不像其他 ViewController 生命周期方法那样在 viewcontroller 中覆盖它,是否也会调用 loadView?
- reactjs - How to design State for Multiple objects using react-redux?
- javascript - 单元测试应该“通过”?为什么会失败?