为什么grails-2.0.1无法在BootStrap类的init方法中创建User,Role和UserRole域对象? [英] Why does grails-2.0.1 fail to create User, Role and UserRole domain objects in the BootStrap class init method?

查看:84
本文介绍了为什么grails-2.0.1无法在BootStrap类的init方法中创建User,Role和UserRole域对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可以创建User,Role和UserRole域对象BootStrap类的init方法在grails-1.3.7版本中使用Spring Security Core Plugin ...但在grails-2.0.1版本中不能这样做。



以下是我试图执行的代码...

  import com.beshto.Role 
import com.beshto.User
import com.beshto.UserRole
import grails.util.Environment
$ b $ class BootStrap {

def init = {servletContext - >
switch(Environment.current){
case Environment.DEVELOPMENT:
createInitialUsersIfRequired()
break;
案例Environment.PRODUCTION:
println生产版本不需要特殊配置或引导程序...
break;



def destroy = {
}

void createInitialUsersIfRequired(){
println创建特殊的配置/ Bootstrapping for Development版本admin / 4321和user / 1234 ...

if(Role.list()。size()== 0){
新角色(权限:'ROLE_ADMIN ').save(flush:true)
新角色(权限:'ROLE_USER')。save(flush:true)
}

if(User.count()= = 0){
def newAdmin = new User(用户名:'admin',启用:true,密码:'4321')。保存(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
}
}
}

任何HELP y赞赏!

解决方案

检查角色创建方法。尝试更改:
$ b

新角色(权限:'ROLE_ADMIN')。save(flush:true)



for


$ b

新角色(权限:'ROLE_USER')。save(failOnError:true )



和用户的情况一样。例如:

 新用户(用户名:'admin',启用:true,密码:'4321')。save(failOnError :true)

而不是

<$ p $保存(flush:true)
新用户(用户名:'admin',启用:true,密码:'4321' p>

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:

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

instead of

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

这篇关于为什么grails-2.0.1无法在BootStrap类的init方法中创建User,Role和UserRole域对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆