0
votes

I am on Grails 2.1.1 and Spring Security Core Plugin 1.2.7.3.

I am trying to assign Roles to Users in BootStrap.groovy and persist these assignments. I create my Roles and an Admin User in BootStrap.

I found the following blog post: Create User Roles for Spring Security

When I try this approach, I get:

2012-09-16 22:00:56,762 ERROR [org.codehaus.groovy.grails.web.context.GrailsContextLoader] Error executing bootstraps:

groovy.lang.MissingMethodException: No signature of method: User.addToAuthorities() is applicable for argument types: (Role) values: Possible solutions: getAuthorities()

I also tried

class BootStrap {

    def init = { servletContext ->
        ....
        admin.getAuthorities().add(adminRole) 
        admin.save(flush: true)
        ....
    }
} 

However, when I checked the user_role table, it was empty (I checked and entries for Admin User and Admin Role were in respective user and role tables, just not the association).

Could anyone please provide me some guidance or insight on what I am doing wrong? Thank you very much.

1

1 Answers

4
votes

That blog post is 3 years old and wrong for the current plugin in two ways. One, don't encode the password, it's done for you in the User class. And two (the problem you're seeing now) don't call collections methods (addTo, removeFrom) because collections aren't used. Call UserRole.create admin, adminRole as described in the plugin docs: http://grails-plugins.github.com/grails-spring-security-core/docs/manual/index.html (in particular the tutorial in section 23.1).