Hi, I am new to the Grails platform…
I could create the User, Role and UserRole domain objects in the BootStrap class init method in the grails-1.3.7 version with the Spring Security Core Plugin … But can not do the same in the grails-2.0.1 version..
Following is the code that I’m trying to execute …
import com.beshto.Role
import com.beshto.User
import com.beshto.UserRole
import grails.util.Environment
class BootStrap {
def init = { servletContext ->
switch (Environment.current) {
case Environment.DEVELOPMENT:
createInitialUsersIfRequired()
break;
case Environment.PRODUCTION:
println "No special config or Bootstrapping required for the Production version..."
break;
}
}
def destroy = {
}
void createInitialUsersIfRequired() {
println "Creating special config / Bootstrapping for Development version admin/4321 and user/1234..."
if(Role.list().size() == 0) {
new Role(authority: 'ROLE_ADMIN').save(flush: true)
new Role(authority: 'ROLE_USER').save(flush: true)
}
if(User.count() == 0){
def newAdmin = new User(username: 'admin', enabled: true, password: '4321').save(flush: true)
def newUser = new User(username: 'user', enabled: true, password: '1234').save(flush: true)
UserRole.create newAdmin, Role.findByAuthority('ROLE_ADMIN'), true
UserRole.create newUser, Role.findByAuthority('ROLE_USER'), true
assert User.count() == 2
assert Role.count() == 2
}
}
}
Any HELP will be highly appreciated!
Check role creation method. Try to change:
new Role(authority: 'ROLE_ADMIN').save(flush: true)for
new Role(authority: 'ROLE_USER').save(failOnError: true)And the same case for the users. For example:
instead of