2011-03-13 5 views
2

私のuser-domainには、ログイン後に更新したい(last-login)というフィールドがあります(self-exaplantoy)。 は、Tomcat(DEV ENV)を再起動した後、私は次の例外を取得:ログイン後にユーザーのlastLoginプロパティを更新します。

groovy.lang.MissingMethodException:メソッドのシグネチャなし:静的photoo.user.User.findById()は引数の型に適用されます:(java.langで.LONG)値:[2]考えられる解決策:のfindAll(groovy.lang.Closure)、見つける(groovy.lang.Closure)

私は私のConfig.groovyに次のコードを使用します。

// callback event, after a successful login 
grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx -> 
    def user = User.findById(appCtx.springSecurityService.principal.id) 
    User.withTransaction { 
     if(!user.isAttached()) 
      user.attach() 
     user.lastLogin = new Date() 
     user.save(flush: true) 
    } 
} 

例外を回避するにはどうすればよいのですか? thx

答えて

1

User.get()を使用するだけでよい場合もあります。これは私が持っているものです:

onInteractiveAuthenticationSuccessEvent = { e, appCtx -> 

    // handle AuthenticationSuccessEvent 
    def autservice = appCtx.authenticateService 
    def user = autservice.userDomain() 
    println "user: ${user.id}:${user}}" 
    def request = org.codehaus.groovy.grails.plugins.springsecurity.SecurityRequestHolder.getRequest() 
    def session = request.getSession(false) 
    if (user) { 

     def person = Person.get(user.id) 

     def lang = person.preferredLanguage 
     if (session) { 
      session.lang = lang 
     } 
    } 
} 
3

私は間違いを見つけました。 withTransaction-blockでユーザーを読み込むことが解決策です。

// dont load before 
// def user = User.findById(appCtx.springSecurityService.principal.id) 
User.withTransaction { 
     def user = User.findById(appCtx.springSecurityService.principal.id) 
     if(!user.isAttached()) 
      user.attach() 
     user.lastLogin = new Date() 
     user.save(flush: true, failOnError: true) 
    } 
関連する問題