Bruno Morgado
Bruno Morgado

Reputation: 507

create users with spring security plug-in for Grails

I'm using the spring security core plug-in in a Grails app. I managed to install the plug-in and provide a default user in the bootstrap which is working fine. However I want to provide the option for create a new user (signup) in the application. What is the best way to accomplish this? I tried the following code in the UserController:

 def save = {
    def userRole = SecRole.findByAuthority("ROLE_USER")?: new SecRole(authority:"ROLE_USER").save(failOnError:true)
    def adminRole = SecRole.findByAuthority("ROLE_ADMIN") ?: new SecRole(authority:"ROLE_ADMIN").save(failOnError:true)

    def newUser = new User(
                        username: params.username,
                        password: springSecurityService.encodePassword(params.password),
                        enabled: true)

    SecUserSecRole.create newUser, userRole
}

but it doesn't work and throws the java.lang.NullPointerException. Any ideas? Thanks in advance.

Upvotes: 0

Views: 527

Answers (2)

hasan
hasan

Reputation: 553

I tried to load not existing Roles. I tested it with the above comment (save the user and not so save after fixing my bug and both versions worked). So SecUserSecRole.create were sufficient for me.

Upvotes: 0

Burt Beckwith
Burt Beckwith

Reputation: 75671

You need to call save() on the User.

Upvotes: 2

Related Questions