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 update
or other relevant statements.
Is there anything else I need to add anywhere in order to get this working?
For the record:
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With