Why does grails-2.0.1 fail to create User, Role and UserRole domain objects in the BootStrap class init method?

懵懂的女人 提交于 2020-01-04 09:30:18

问题


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!


回答1:


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:

new User(username: 'admin', enabled: true, password: '4321').save(failOnError: true)

instead of

new User(username: 'admin', enabled: true, password: '4321').save(flush: true)



回答2:


I came here with the same problem and played with it until it worked.

I am not 100% percent sure which change made it work, but here is what I did:

  1. Changed quotations to be double quotations, so " instead of '. username: 'admin' would be username: "admin"

I thought maybe this was causing some java issues. I am not sure.

Check that your user and role are actually saved! Add fail on error as they recommended above.

Then go to your User, Role, and UserRole classes and make sure that every variable that you have defined is actually being set. Even if you don't have any constraints, if you have a string, just assign it "" the empty string. The reason may be that while it can accept blank strings, it may not accept null strings.

This was what solved it for me. It was very hard to solve because there was no valuable output from the terminal and I had to think of different things.

I hope this solves your issue. It's probably related to saving with constraints on fields of an object not being satisfied.

Let me know if it doesn't work and try to provide more information.

Good luck!



来源:https://stackoverflow.com/questions/9501934/why-does-grails-2-0-1-fail-to-create-user-role-and-userrole-domain-objects-in-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!