Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing last login date in Grails with Spring Security

In my Config.groovy I used:

grails.plugins.springsecurity.useSecurityEventListener = true

grails.plugins.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
    User.withTransaction {
        def user = User.findById(appCtx.springSecurityService.principal.id)
        if(!user.isAttached())
            user.attach()
        user.lastLoginDate = new Date()
        user.save(flush: true, failOnError: true)
    }
}

My User domain has this field and I don't get any errors while logging in, however, the field is not updated.

I tried debugging everything from org.hibernate but I couldn't find any updateor other relevant statements.

Is there anything else I need to add anywhere in order to get this working?

For the record:

  • Grails 1.3.7
  • Spring Security Core 1.1.2
  • Hibernate 1.3.7
like image 492
slhck Avatar asked May 03 '11 10:05

slhck


1 Answers

Ok, the answer was plain simple. I had forgot the package name so it wouldn't find User.

So this one worked if your User object is in the package org.test:

org.test.User.withTransaction {
      def user = org.test.User.get(appCtx.springSecurityService.principal.id)
      user.lastLoginDate = new Date()
      user.save(failOnError: true)
}
like image 123
slhck Avatar answered Oct 04 '22 15:10

slhck