【问题标题】:scoping with active admin cancan使用活动管理员进行范围界定 cancan
【发布时间】:2012-05-30 07:31:38
【问题描述】:

我如何在活动管理员和 cancan 中使用范围。 我有管理员用户并且那些与机构有(has_one)关系 和机构有很多简介 现在,当管理员用户登录时,我想显示所有具有相同机构的个人资料。

没有发现以下链接很有帮助。

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

【问题讨论】:

    标签: ruby-on-rails cancan activeadmin


    【解决方案1】:

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

    # 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 选项

    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
    

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

    【讨论】:

    • 因此需要通过机构在管理员用户和配置文件之间建立虚拟关系。非常感谢 !!您的回答清除了有关活动管理员和 cancan 的许多事情
    • 如果用户是超级管理员,我如何取消范围?例如。他应该看到所有的个人资料?
    • 嗨@viktor tron,也许你能帮我解决这个问题stackoverflow.com/questions/11096400/…
    猜你喜欢
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多