0
votes

I am new in Grails and Spring Security. I try to make a simple register view. I created the User, Role and UserRole domain classes with the s2-quickstart script from spring-security.

My problem is that only the User will be saved in the database. But I want to save the User and the UserRole.

Thats my controller:

@Secured('permitAll')

class RegisterController {

def index() {
}

def register() {

    def user = new User(params)
    user.save()

    def role = Role.findByAuthority('ROLE_USER') // ROLE_USER will be created in the bootstrap class
    UserRole.create(user, role)

    redirect view: 'index'
}

}

If i put the same code in the bootstrap class the UserRole will be saved too.

Hope someone can help and explain, why this not work.

1

1 Answers

0
votes

update your code:

def register() {

    def user = new User(params)
    user.save(flush: true, failOnError: true)

new UserRole(user: admin, role: Role.findByAuthority('ROLE_USER')).save(flush: true, failOnError: true)

    redirect view: 'index'
}

If there will any issue in during save the UserRole then failOnError: true with throw error, then you can track the issue.