使用主动管理员cancan进行范围界定 [英] scoping with active admin cancan

查看:107
本文介绍了使用主动管理员cancan进行范围界定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在活动的管理员&中使用作用域可以可以。
我有管理员用户&那些与机构
有(has_one)关系,机构有很多配置文件
现在,当管理员用户登录时,我要显示具有相同机构的所有配置文件。

How can i use scoping with active admin & cancan. I have admin users & those have (has_one) relation with institution and institution has many profiles Now when admin user login then i want display all profiles which has same institution.

找不到以下链接很有帮助。

Doesn't find following link much helpful.

http://activeadmin.info/docs/2-resource-customization.html#scoping_the_queries

推荐答案

如果您只是这样做,会遇到问题吗?

if you just do simply this, do you get a problem?

# ability.db

def initialize(user)
  case
    # ...
    when user.super_admin?
      can :manage, :all
    when user.admin?
      can :manage, Profile, :institution_id => user.institution.id
    # 
    # ...
end

这将允许: Profile.accessible_by(current_user),此处与 current_user.profiles

class AdminUser
  has_one :institution
  has_many :profiles, :through => :institution
end

ActiveAdmin.register Profile do
  scope_to :current_user #here comes the variable which set in initializer
end

如果您希望超级管理员访问所有帖子,则可以使用:association_method选项

if you want superadmin to access all posts, you can use the :association_method option

ActiveAdmin.register Profile do
  scope_to :current_user, :association_method => :admin_profiles
end 

# in class User
def admin_profiles
  if super_admin?
    Profile.unscoped
  else
    profiles
  end
end

一个棘手的解决方案可以对此进行概括,并使用委托者类作为代理来取消超级管理员所有模型的范围。我可以根据要求拼出。

A tricky solution could generalize this and use a delegator class as proxy to unscope all models for superadmins. i can spell out on request.

这篇关于使用主动管理员cancan进行范围界定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆