Created
November 9, 2012 23:21
-
-
Save sergiomichels/4048972 to your computer and use it in GitHub Desktop.
Grails - Custom UserDetailsService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserDetailsService | |
| import org.springframework.security.core.userdetails.UserDetails | |
| import org.springframework.security.core.userdetails.UsernameNotFoundException | |
| import User | |
| class CustomUserDetailsService implements GrailsUserDetailsService { | |
| //this creates a List of GrantedAuthorityImpl | |
| MyAuthoritiesBuilder myAuthoritiesBuilder | |
| UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException { | |
| return loadUserByUsername(username) | |
| } | |
| UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | |
| User user = User.get(username.toUpperCase()) | |
| if (!user) { | |
| throw new UsernameNotFoundException('User not found', username) | |
| } | |
| InsoftGrailsUser grailsUser = new InsoftGrailsUser(user.id, user.passwrod, true, !user.accountExpired, | |
| !user.passwordExpired, !user.accountLocked, myAuthoritiesBuilder.getAuthorities(user), user.id) | |
| grailsUser.uniqueIdentifier = myAuthoritiesBuilder.uniqueIdentifier | |
| return grailsUser | |
| } | |
| } | |
| import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils | |
| import org.springframework.security.core.authority.GrantedAuthorityImpl | |
| import br.com.insoft.seguranca.SegurancaService | |
| import br.com.insoft4.seguranca.UsuarioPonto | |
| class MyAuthoritiesBuilder { | |
| static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)] | |
| def grailsApplication | |
| public List<GrantedAuthorityImpl> getAuthorities(User user) { | |
| def roles = [] | |
| SecurityService securityService = getSecurityService() | |
| if(securityService.userCanAccess(user, "FORMS_CODE1")) { | |
| roles << new GrantedAuthorityImpl("ROLE_FORMS_CODE1") | |
| } | |
| if(securityService.userCanAccess(user, "FORMS_CODE2")) { | |
| roles << new GrantedAuthorityImpl("ROLE_FORMS_CODE2") | |
| } | |
| if(roles.empty) { | |
| roles = NO_ROLES | |
| } | |
| return roles | |
| } | |
| public Long getUniqueIdentifier() { | |
| return getUtilService().getNextSequenceValue() //sequence to identify uniquely the user session | |
| } | |
| private def getUtilService() { | |
| return grailsApplication.mainContext.getBean("utilService") | |
| } | |
| private def getSecurityService() { | |
| return grailsApplication.mainContext.getBean("securityService") | |
| } | |
| } | |
| //beans declaration in resources.groovy | |
| myAuthoritiesBuilder(MyAuthoritiesBuilder) { | |
| grailsApplication = ref('grailsApplication') | |
| } | |
| userDetailsService(CustomUserDetailsService) { | |
| myAuthoritiesBuilder = ref('myAuthoritiesBuilder') | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment