Grails Spring Security Plugin - Linking ONE-TO-ONE to a user's domain

I am using Spring's security module to manage membership and authentication in my Grails application.

I am trying to associate a user domain class with a profile domain through a one-to-one association.

I added this line to User.groovy:

static hasOne = [userProfile:UserProfile]
static constraints = {
//...
             userProfile unique:true
}

and UserProfile.groovy:

User user

Alas, I had an error when calling UseRole.create (user, role).

There are several examples of how to get the same functionality that I am looking for. In particular, I want to associate any user with one profile object in order to expand it.

I also want to add a one-to-many relationship with messages and another table ...

Thanks best regards

PS: I get this error:

Spring ... 2011-03-08 12: 18: 51,179 [main] ERROR context.GrailsContextLoader - bootstraps: null java.lang.NullPointerException    $Proxy19.save( )   at com.dromedian.xxxxx.security.UserRole.create(UserRole.groovy: 32)   at com.dromedian.xxxxx.security.UserRole $create.call( )    BootStrap $_closure1.doCall(BootStrap.groovy: 20)   at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251)   at grails.util.Environment.executeForEnvironment(Environment.java:244)   at grails.util.Environment.executeForCurrentEnvironment(Environment.java:220)   at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy: 212)   at grails.web.container.EmbeddableServer $start.call( )    _GrailsRun_groovy $_run_closure5_closure12.doCall(_GrailsRun_groovy: 158)    _GrailsRun_groovy $_run_closure5_closure12.doCall(_GrailsRun_groovy)    _GrailsSettings_groovy $_run_closure10.doCall(_GrailsSettings_groovy: 280)    _GrailsSettings_groovy $_run_closure10.call(_GrailsSettings_groovy)    _GrailsRun_groovy $_run_closure5.doCall(_GrailsRun_groovy: 149)    _GrailsRun_groovy $_run_closure5.call(_GrailsRun_groovy)    _GrailsRun_groovy.runInline(_GrailsRun_groovy: 116)    _GrailsRun_groovy.this $4 $runInline (_GrailsRun_groovy)    _GrailsRun_groovy $_run_closure1.doCall(_GrailsRun_groovy: 59)    RunApp $_run_closure1.doCall(RunApp.groovy: 33)    gant.Gant $_dispatch_closure5.doCall(Gant.groovy: 381)    gant.Gant $_dispatch_closure7.doCall(Gant.groovy: 415)    gant.Gant $_dispatch_closure7.doCall(Gant.groovy)    gant.Gant.withBuildListeners(Gant.groovy: 427)    gant.Gant.this $2 $withBuildListeners (Gant.groovy)    gant.Gant $ $2 $BuildListeners.callCurrent( )    gant.Gant.dispatch(Gant.groovy: 415)    gant.Gant.this $2 $ (Gant.groovy)    gant.Gant.invokeMethod(Gant.groovy)    gant.Gant.executeTargets(Gant.groovy: 590)    gant.Gant.executeTargets(Gant.groovy: 589) ...

:

User.groovy( , Spring)

    static hasOne = [userDetail:UserDetail]

static constraints = {
    username blank: false, unique: true
    password blank: false
            userDetail unique:true
}

UserDetail.groovy

static hasOne = [user:User]
static belongsTo = User

BootStrap.groovy

    //TODO temporary added - no for production or persistent db
    def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
    def userRole = new Role(authority: 'ROLE_USER').save(flush: true)

    String password = springSecurityService.encodePassword('password')
    def testUser = new User(username: 'me', enabled: true, password: password)
    testUser.save(flush: true)
    if(testUser != null){
        UserRole.create testUser, adminRole, true
    }

        UserRole.create testUser, adminRole, true

. , , .

+3
3

, , . , . :

def ensureSave(domainObject) {
 if(!domainObject.save(flush:true)) {
  throw new Exception("not saved successfully: $domainObject");
 }
 domainObject
}

:

ensureSave(testUser)

testUser = ensureSave(new User(...));

+1

, UserProfile . , save() , NullPointerException.

UserDetail profile = new UserDetail()
def testUser = new User(username: 'me', enabled: true, password: password, userdetail: profile)
assert testUser.save()

.save() , , - , , , . grails , , , . , , .

0

... :

null... :

static constraints = {
   ...
   userDetail unique:true, nullable: true
   ...
}

, ( , UserRole on null )

0

Source: https://habr.com/ru/post/1795764/


All Articles