gpt4 book ai didi

grails - 如何用另一个替换用户的角色集合?

转载 作者:行者123 更新时间:2023-12-02 14:59:53 25 4
gpt4 key购买 nike

我想知道如何更新用户角色,我尝试了以下方法:
(注意:User = Personne,UserRole = PersonneRole )
清除列表并用新列表替换:

    @Transactional
def myaction(Personne personne,List<String> roles){ // roles is a list of authorities
def role
personne.personneRoles*.delete()// tried PersonneRoles.findByPersonne(personne)*.delete() as well
personne.personneRoles.clear()
roles.each { role=Role.findByAuthority(it);personne.personneRoles.add(new PersonneRole(personne,role)) }
if(!personne.save(flush:true))
return false
....
我也遵循 this tutorial,但是没有用:
@Transactional
def action(Personne personne,List<String> roles){
def role
def temp = []
temp += personne.personneRoles
temp.each{personneRole ->
personne.removeFromPersonneRoles(personneRole)
personneRole.delete()
}
roles.each { role=Role.findByAuthority(it);personne.personneRoles.add(new PersonneRole(personne,role)) }
if(!personne.save(flush:true))
return false
...
在两种情况下,我都会收到一条错误消息,告诉我“....拒绝的值[ROLE_ADMIN_APP]
这是一个有效的角色,但是已经对这个人追了,这意味着该集合没有被清除,请问有什么解决办法吗?谢谢

最佳答案

在spring安全UI插件https://github.com/grails-plugins/grails-spring-security-ui/blob/master/grails-app/services/grails/plugin/springsecurity/ui/SpringSecurityUiService.groovy中查看grails.plugin.springsecurity.ui.SpringSecurityUiService,看看他是如何做到的。它将删除所有关联的角色并重新添加它们(请参见void updateUser)

    UserRole.removeAll user
addRoles user, roleNames
removeUserFromCache user

关于grails - 如何用另一个替换用户的角色集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37188242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com