Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何向Pundit添加全能用户级别_Ruby On Rails_Authorization_Pundit - Fatal编程技术网

Ruby on rails 如何向Pundit添加全能用户级别

Ruby on rails 如何向Pundit添加全能用户级别,ruby-on-rails,authorization,pundit,Ruby On Rails,Authorization,Pundit,我查阅了文档并进行了一些搜索,但我没有看到全能用户(超级用户)级别的选项,也没有看到如何创建全能用户 有没有人看到或创造了这样做的理由?我认为可以连接到核心身份验证系统,但我不确定在哪里进行连接 非常感谢..要做到这一点,唯一的方法是让已指定为“超级用户”的用户或角色的授权检查返回true。因此,它将如下所示: def update? *normal authorization logic* or is_superuser? end def edit? *normal authoriza

我查阅了文档并进行了一些搜索,但我没有看到全能用户(超级用户)级别的选项,也没有看到如何创建全能用户

有没有人看到或创造了这样做的理由?我认为可以连接到核心身份验证系统,但我不确定在哪里进行连接


非常感谢..

要做到这一点,唯一的方法是让已指定为“超级用户”的用户或角色的授权检查返回true。因此,它将如下所示:

def update?
  *normal authorization logic* or is_superuser?
end
def edit?
  *normal authorization logic* or is_superuser?
end
#etc...

private

def is_superuser?
  # configure how to determine who the super users are and return true/false
end

您可以在
ApplicationPolicy
中定义
is\u superuser?
private方法,前提是您从应用程序策略继承了类级策略;否则,您需要在每个策略中定义它。

我找到了一种方法,使用ApplicationPolicy的继承。我别名访问方法,并在调用任何方法之前绑定超级用户测试。若用户是超级用户,我只返回true。我在初始化时这样做,然后才需要将实例方法定义为别名

ALIAS_PREFIX = '__original_'

def initialize(user, record)
 @user = user
 @record = record
 [:index?,:show?,:create?,:new?, :update?, :edit?, :destroy?].each do |access_method|
  alias_name = ALIAS_PREFIX+access_method.to_s
  aliasing_original_method(access_method,alias_name)
  self.class.send(:define_method, access_method) do |*args|
    superuser? ? (return true) : send(alias_name, *args)
  end
 end
end

private
def superuser?
  #whatever you want to define a super user
end
def aliasing_original_method(old_name, new_name)
 self.class.send(:alias_method, new_name, old_name)
 self.class.send(:private, new_name)
end
在[AnyFile]策略中,我会:

def initialize(user, record)
 super(user, record)
end
这将确保子策略中每个方法的真实返回

[更新]

第一个解决方案有点凌乱,我对ruby的了解(以及最后期限)不允许我把它推得更远。不管怎样,我找到了另一种方法。由于我总是转换用户的角色,我在ApplicationPolicy中实现了for_roles方法

def for_roles(*args,&block)
    return true if superuser?
    if args.include?(:all) || (@user.role_symbols & args).any?
      block.call
    else false
    end
end
然后,在任何策略中,您都可以这样做,例如

for_roles(:client_admin,:technician) do
  #any rule computation, DB request you want
end
#or
for_roles(:all) do
  #any rule computation, DB request you want
end

谢谢你。。我觉得这是唯一的办法。我希望我错过了Pundit's self中的一个内置方法。第一次调用效果很好,第二次调用时我有一个无限循环。