A little old, but can come to your hands for all the other groups:
In the User class:
Set<RoleGroup> getAuthorities() { (UserRoleGroup.findAllByUser(this) as List<UserRoleGroup>)*.roleGroup as Set<RoleGroup> } Set<Role> getRoles() { (UserRoleGroup?.findAllByUser(this)?.roleGroup?.authorities.collect{it}?.flatten() ?: oldRoles) as Set<Role> } List<String> getRoleNames() { (UserRoleGroup?.findAllByUser(this)?.roleGroup?.collect{it.authorities.authority}?.flatten()?: oldRoles.authority) as List<String> } //Above will look up from userRoleGroup roleGroup.authorities = UserRole below Set<Role> getOldRoles() { (UserRole.findAllByUser(this) as List<Role>)*.role as Set<Role> }
I used roles, although the groups were enabled and authenticated against the old oldRoles method:
import grails.plugin.springsecurity.userdetails.GormUserDetailsService import grails.transaction.Transactional import org.springframework.security.authentication.UsernamePasswordAuthenticationToken import org.springframework.security.core.Authentication import org.springframework.security.core.GrantedAuthority import org.springframework.security.core.authority.SimpleGrantedAuthority import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.UsernameNotFoundException class MySpringSecurityAuthenticator extends GormUserDetailsService{ UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException { return loadUserByUsername(username) } @Transactional UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
and in my spring / resources.groovy:
userDetailsService(MySpringSecurityAuthenticator){ grailsApplication = ref('grailsApplication') }
What has been done so far, returned to user ROLES and added them to the authentication handler via .. getAuthorities (user.oldRoles)
Now I have changed the above method for reading in group names via getAuthoritiesFromGroup (user.authorities)
It simply analyzes the names of groups that, as a side (effect) of the version used, should also include the word ROLE_GROUPNAME
So if create
@Transactional def grantPermission(User user, String role='ROLE_ADMIN', String group='ROLE_SUPERGROUP') { def adminRole = Role.findByAuthority(role) if (!adminRole) { adminRole = new Role(authority: role).save(flush: true) } UserRole.create user, adminRole, true def adminRoleGroup = RoleGroup.findByName(group) if (!adminRoleGroup) { adminRoleGroup = new RoleGroup(name: group).save(flush: true) } def adminRoleGroupRole = RoleGroupRole.findByRole(adminRole) if (!adminRoleGroupRole) { adminRoleGroupRole = new RoleGroupRole(role: adminRole, roleGroup: adminRoleGroup).save(flush: true) } def alreadyDone = UserRoleGroup.findByUserAndRoleGroup(user,adminRoleGroup) if (!alreadyDone) { new UserRoleGroup(user: user, roleGroup: adminRoleGroup).save(flush: true) } }
I expect authentication against group names, not user roles, so in short, I had to change my controller to
@Secured("hasAnyRole('ROLE_SUPERGROUP')")
Hope this makes sense, has to be straightforward to follow, it just takes time to plunge into it all.
At this moment I am still playing around, and I will not use it as a specific answer, as I would like to crack it to say that my groups were my authorities, and if I wanted me to add another hook To go further each of these groups and each of its actual roles also - the uncertainty that this will produce - at the moment
I want to change it to requestMaps and move it to db so that a lot will change, and will decide whether I should go back or use groups that I know in this way, I can configure less names over controllers and rely on group names ..
In any case, this is under the hood of all this and gives you a clear idea for several years, but it can come in handy for others.
Update So I decided to go with:, ,getAuthorities(user.roles)
Where Set<Role> getRoles() { method inside the specified code bit
The reason is pretty simple:
results?.each { it.user=User.get(it.id) println "-- \n${it.id}:${it.user.roles} \n${it.id}:${it.user.oldRoles}" 7:[Role(authority:ROLE_ADMIN), Role(authority:ROLE_ADMINAHHA)] 7:[Role(authority:ROLE_ADMIN)]
As you can see, I added a new user using getOldRoles. I see only 1 role on getRoles. I get 2 roles. I added a user with two roles.
So, now he will analyze all user roles and add them to the List<GrantedAuthority> , authentication will still be through the actual ROLE names generated earlier.
It just means that when I disconnect the group from the user, it should stop downloading this permission for this user.
this is what the model should do