views:

104

answers:

1

Hi,

I'm using the Acegi (AKA Sprign Security) plugin in my Grails app. InSecurityConfig.groovy I have added the line

userName = 'email'

such that the email field is used as the username. I find that if I change the email field and save the object, e.g.

user.email = '[email protected]'
user.save(failOnError: true)  

The save completes without error, but the email field is not actually updated. My guess is that the Acegi plugin prohibits changing the username field, but I'd be grateful if someone could confirm.

Thanks, Don

+2  A: 

The domain object used by acegi is cached. As a matter of enormous coincidence I had the same problem this week and wrote up the solution yesterday!

In summary you have two options:

Turn off caching of the domain object by adding cacheUsers = false to your SecurityConfig.groovy

Refresh the domain object by replacing it in the SecurityContextHolder

private def refreshUserPrincipal(user) {
    GrantedAuthority[] auths = user.authorities.collect {
        new GrantedAuthorityImpl(it.authority)
    }
    def grailsUser = new GrailsUserImpl(
        user.username
            "",
            true,
            true,
            true,
            true,
            auths,
            user);
    def authToken = new UsernamePasswordAuthenticationToken(grailsUser, "", auths)
    SecurityContextHolder.context.authentication = authToken
}

(Check the source of GrailsUserImpl to see what all those true values mean!)

Dave