Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
如何在grails中使用切换用户spring安全性来覆盖用户权限_Grails_Spring Security - Fatal编程技术网

如何在grails中使用切换用户spring安全性来覆盖用户权限

如何在grails中使用切换用户spring安全性来覆盖用户权限,grails,spring-security,Grails,Spring Security,我试图在我的用户gsp中构建一些管理工具,而不构建整个管理控制器和页面 我的应用程序具有用户和管理员权限。用户创建帐户时,管理员会在用户可以执行某些功能之前批准该帐户。管理员使用switchUser功能在批准帐户之前检查用户的帐户 我只想在其中一个用户帐户页面上添加一个“批准”按钮,该按钮仅对切换用户角色的管理员可见,而不是编写代码,管理员随后必须返回并查找用户,查找他们的记录,然后将帐户更新为已批准。问题是管理员切换到的用户权限无法执行“批准”操作,否则用户可以激活自己的帐户 普惠制就是这样的

我试图在我的用户gsp中构建一些管理工具,而不构建整个管理控制器和页面

我的应用程序具有用户和管理员权限。用户创建帐户时,管理员会在用户可以执行某些功能之前批准该帐户。管理员使用switchUser功能在批准帐户之前检查用户的帐户

我只想在其中一个用户帐户页面上添加一个“批准”按钮,该按钮仅对切换用户角色的管理员可见,而不是编写代码,管理员随后必须返回并查找用户,查找他们的记录,然后将帐户更新为已批准。问题是管理员切换到的用户权限无法执行“批准”操作,否则用户可以激活自己的帐户

普惠制就是这样的:

我的普惠制:

<sec:ifSwitched>
    <p><g:link controller='charge' action='activateProfile'>Approve profile</g:link> - This profile will become live in the system.</p>
</sec:ifSwitched>
我想工作但不存在的内容,因为没有isSwitchedUser()注释:

@Secured("isAuthenticated() and (hasAnyRole('ROLE_ADMIN', 'ROLE_SWITCHED_USER') or isSwitchedUser())
def activateProfile() {
   //update the database and make this user's profile active
}
这当然不起作用,因为作为切换用户,切换帐户没有这两个角色,因此会出现页面未授权错误:

@Secured("isAuthenticated() and (hasAnyRole('ROLE_ADMIN', 'ROLE_SWITCHED_USER'))
def activateProfile() {
   SpringSecurityUtils.isSwitchedUser() {
      //activate the profile
   }
   else {
      //not allowed unless this is a switched user
   }
}

我只是在使用permitAll并检查操作以获得许可吗?它工作正常,安全可靠,但感觉不正常。

您可以使用以下两种方法之一:

自定义许可评估器

您可以在安全批注中使用
hasPermission()
方法,并创建自定义
PermissionEvaluator
。在代码中,如下所示:

@PreAuthorize("hasPermission(#myObject, 'update')")
public void updateSomething(myObject) {
  ..
}
PermissionEvaluator
实现中,您可以对切换用户进行检查

安全注释中的服务调用

在安全表达式中,您可以通过在bean名称前面加上
@
来引用bean

例如:

@PreAuthorize("@mySecurityService.isSwitchedUser() or ...")
public void doSomething(myObject) {
  ..
}
更多详细信息: 这两种方法都需要一些设置。前一段时间,我写了一个更详细的答案,就是关于这两种方法。你可以在这里找到它:

@PreAuthorize("@mySecurityService.isSwitchedUser() or ...")
public void doSomething(myObject) {
  ..
}