简体   繁体   English

在Rails 4模型中,如何只允许使用范围来管理具有特定值的用户?

[英]How do allow to use a scope only to admin users with a certain value in Rails 4 model?

In my recent Rails project I have a scope in a model. 在我最近的Rails项目中,我在模型中有一个作用域。 This scope has two possible values: true or false. 该范围有两个可能的值:true或false。

scope :ultima, -> ultima { where(:ultima => ultima ) 

I´m using has_scope gem to allow using this scope in controller so I can do calls like this: 我正在使用has_scope gem,以允许在控制器中使用此范围,因此我可以进行如下调用:

http://localhost:3000/model?ultima=true

or 要么

http://localhost:3000/model?ultima=false

In this project, I have normal users and admin users who have special access to some parts of the application. 在这个项目中,我有普通用户和admin用户,它们对应用程序的某些部分具有特殊访问权限。

How do you allow to use this scope with value 'false' to admin users; 您如何允许将此值为“ false”的范围用于管理员用户; but when value is 'true' is allowed to everyone? 但是当值是“真”时,每个人都可以吗?

I generally use Rolify and Pundit to accomplish this. 我通常使用Rolify和Pundit来完成此任务。 Rolify sets :admin on the model. 在模型上取消设置:admin。 And pundit has a scope where i check if user is :admin. 和Pundit有一个范围,在这里我检查用户是否为:admin。

You can also do this through a namespace and have a separate admin controller that you lock this down with. 您也可以通过名称空间来执行此操作,并使用一个单独的管理控制器来锁定该控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM