2011-03-02 88 views
3

我使用Spring Security Plugin在我的Grails應用程序中管理成員資格和身份驗證。Grails Spring Security Plugin - 與用戶域的一對一關聯

我想通過一對一的關聯將用戶域類與配置文件域相關聯。

我加在User.groovy這些行:

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

,並UserProfile.groovy:

User user 

唉,調用UseRole.create(用戶,角色),當我有錯誤。

有關如何獲得我正在尋找的相同功能的一些最佳做法。特別是,我想將任何用戶與一個配置文件對象相關聯以擴展它。

我想你還需要增加一個一對多的發佈訊息和其他表的關係......

感謝 問候

PS: 我得到這個錯誤:

配置Spring安全UI ... 2011-03-08 12:18:51,179 [main] ERROR context.GrailsContextLoader - 執行bootstraps時出錯:null java.lang.NullPointerException at $ Proxy19.save(Unknown Source) a t com.dromedian.xxxxx.security.UserRole.create(UserRole.groovy:32) at com.dromedian.xxxxx.security.UserRole $ create.call(Unknown Source) at BootStrap $ _closure1.doCall(BootStrap.groovy: 20) 在grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251) 在grails.util.Environment.executeForEnvironment(Environment.java:244) 在grails.util.Environment.executeForCurrentEnvironment(Environment.java:220)在_GrailsRun_groovy 在org.grails.tomcat.TomcatServer.start(TomcatServer.groovy::212) 在grails.web.container.EmbeddableServer $ start.call(未知來源) 在_GrailsRun_groovy $ _run_closure5_closure12.doCall(158 _GrailsRun_groovy) $ _run_closure5_closure12.doCall(_GrailsRun_groovy) 在_GrailsS​​ettings_groovy $ _run_closure10.doCall(_GrailsS​​ettings_groovy:280) 在_GrailsS​​ettings_groovy $ _run_closure10.call(_GrailsS​​ettings_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) at gant.Gant $ _dispatch_closure7.doCall(Gant.groovy:415) at gant.Gant $ _dispatch_closure7.doCal l(Gant.groovy) at gant.Gant.withBuildListeners(Gant.groovy:427) at gant.Gant.this $ 2 $ withBuildListeners(Gant.groovy) at gant.Gant $ this $ 2 $ withBuildListeners.callCurrent(Unknown Source ) 在gant.Gant.dispatch(Gant.groovy:415) 在gant.Gant.this $ 2 $訊(Gant.groovy) 在gant.Gant.invokeMethod(Gant.groovy) 在gant.Gant.executeTargets(甘特。常規:590) 在gant.Gant.executeTargets(Gant.groovy:589) 應用程序上下文關停......

的配置是:

User.groovy(域類彈簧安全插件創建)

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 

沒有錯誤。我試圖調試,但我可以理解錯誤在哪裏。

+0

您需要向我們顯示錯誤消息,因爲我們沒有魔力來查看您的屏幕。 ;) – Gregg 2011-03-02 16:40:05

+0

您必須更具體地瞭解您收到的錯誤消息的位置和地址。 – jjczopek 2011-03-03 00:06:41

+0

對不起,我現在發佈了錯誤消息:) – ryuujin 2011-03-08 11:23:40

回答

0

我想你必須在構造函數中設置UserProfile。因爲你沒有提供,所以save()失敗,因此給你NullPointerException

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

斷言在.save()調用當你在你的領域類改變一些代碼已被證明非常有用,往往你,如果你忘記更改它們的構造函數不會在引導文件的工作。由於grails的處理相當優雅,你會得到奇怪的錯誤,而不是獲取最能幫助你的信息。通過放置斷言,它會直接停止問題出現的地方。

1

如前所述,您的測試用戶不會保存,因爲需要用戶配置文件。用戶的save方法將返回null,但是,您不檢查該方法。 我通常把一個方法沿着這些線路在:

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

,然後參考,如下所示:

ensureSave(testUser) 

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

HTH

0

我只是跑成類似的問題......並推斷出這一點:

你的鏈接類必須明確地允許爲空...那就是:

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

如果你不這樣做,構造函數調用的類失敗(正如其他人所指出的,並試圖在null上創建UserRole失敗)

相關問題