gpt4 book ai didi

grails - Grails Spring Security:在双角色之间切换

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

在我的项目中,某些用户具有双重角色,因此,如果有任何这样的用户登录,我如何让他在他拥有的那两个角色之间切换,以便他可以执行该特定角色提供的某些操作。

逐步了解流程,因为我真的是Grails的新手。任何带有示例的在线文献都受到高度赞赏。

更新:-
WorkridersUser loadUserByUsername(String username,String roleName)引发UsernameNotFoundException {
// def conf = SpringSecurityUtils.securityConfig
// Class User = grailsApplication.getDomainClass(“Person”)。clazz

    SchemeUser.withTransaction { status ->

SchemeUser user = SchemeUser.findByUsername(username)
if (!user){ throw new UsernameNotFoundException('User not found', username)}

UserProfile userProfile = UserProfile.findByEmail(user.username)
Representative representative = Representative.findByUser(user)
Organization organization = Organization.get(representative.organization.id)


def authorities = user.authorities.collect {new GrantedAuthorityImpl(it.authority)}

return new WorkridersUser(user.username, user.password, user.enabled,
!user.accountExpired, !user.passwordExpired, !user.accountLocked,
authorities ?: roleName, user.id, organization.companyName,userProfile)
}
}

谢谢

斯里

最佳答案

您需要按如下方法覆盖loadUserByUsername方法:

UserDetails loadUserByUsername(String username, String selectedRole) {
// current selected role is stored in the member variable of the UserDetail class
this.selectedRole = selectedRole
return loadUserByUsername(username)
}

为了在选择框中显示当前用户的所有角色,请编写您自己的标签lib,该标签应类似于:
def authorities = springSecurityService.principal.originalAuthorities
if (authorities.size() > 1) {
out << "<form name='switchRoleForm' action='switchRole' method='post'>"
out << "<select name='roles'>"
Role tmpRole
authorities.each {
tmpRole = Role.findByAuthority(it.authority)
// access your current select role
if (springSecurityService.principal.selectedRole== it.authority)
out << "<option selected value='${it.authority}'>${tmpRole.name}</option>"
else
out << "<option value='${it.authority}'>${tmpRole.name}</option>"
}
out << "</select>"
out << '<input class="switch" type="submit" value="switch"/></span>'
out << "</form>"
}

我在上一篇文章中描述了开关逻辑。

关于grails - Grails Spring Security:在双角色之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772466/

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