Question: How to inject a session property in a GORM/Hibernate Event?

View: New views
3 Messages — Rating Filter:   Alert me  

Question: How to inject a session property in a GORM/Hibernate Event?

by hartsock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In Grails, how do I inject an http session property into a Hibernate Event?

The hibernate.org examples show use of session specific interceptors that require you to create a new interceptor per session and inject the user of that session into the new interceptor (whose life cycle is one session). How would I do this in Grails? Creating a new interceptor for each session means I have to be able to pass an interceptor as a parameter to the hibernate "openSession" call. I've looked inside the GrailsHibernatePlugin.groovy file

I had a need for Audit Logging in my current project. I was able to find enough information about Hibernate Events to do simple audit logging. I can now audit log what changed but not who changed it.

I'd like to find a way to allow the end user to write a closure (a closure to return the actor/user) that closure would be somehow be injected into the event. That closure would allow the use of Acegi, CAS, JSecurity, or which ever security system they wanted. It may simply not be possible to be that generic but I'd like to try.

At least I'd like to be able to inject a session property from the controller into my audit log.

Re: Question: How to inject a session property in a GORM/Hibernate Event?

by pftravis :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think what you are looking for is:

org.springframework.web.context.request.RequestContextHolder

and

RequestContextHolder.requestAttributes.userPrincipal.name should return the userId of the current user.


hartsock wrote:
In Grails, how do I inject an http session property into a Hibernate Event?

The hibernate.org examples show use of session specific interceptors that require you to create a new interceptor per session and inject the user of that session into the new interceptor (whose life cycle is one session). How would I do this in Grails? Creating a new interceptor for each session means I have to be able to pass an interceptor as a parameter to the hibernate "openSession" call. I've looked inside the GrailsHibernatePlugin.groovy file

I had a need for Audit Logging in my current project. I was able to find enough information about Hibernate Events to do simple audit logging. I can now audit log what changed but not who changed it.

I'd like to find a way to allow the end user to write a closure (a closure to return the actor/user) that closure would be somehow be injected into the event. That closure would allow the use of Acegi, CAS, JSecurity, or which ever security system they wanted. It may simply not be possible to be that generic but I'd like to try.

At least I'd like to be able to inject a session property from the controller into my audit log.

Re: Question: How to inject a session property in a GORM/Hibernate Event?

by hartsock :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey! Nice! Yes that does have some very sweet info in it. I'm going to poke around with that some... interesting tho' when I ask that object for an undefined attribute... my exceptions come from the object...

org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest

... very interesting. Thanks for the pointer, I think this might work!



I think what you are looking for is:

org.springframework.web.context.request.RequestContextHolder

and

RequestContextHolder.requestAttributes.userPrincipal.name should return the userId of the current user.


hartsock wrote:
In Grails, how do I inject an http session property into a Hibernate Event?

The hibernate.org examples show use of session specific interceptors that require you to create a new interceptor per session and inject the user of that session into the new interceptor (whose life cycle is one session). How would I do this in Grails? Creating a new interceptor for each session means I have to be able to pass an interceptor as a parameter to the hibernate "openSession" call. I've looked inside the GrailsHibernatePlugin.groovy file

I had a need for Audit Logging in my current project. I was able to find enough information about Hibernate Events to do simple audit logging. I can now audit log what changed but not who changed it.

I'd like to find a way to allow the end user to write a closure (a closure to return the actor/user) that closure would be somehow be injected into the event. That closure would allow the use of Acegi, CAS, JSecurity, or which ever security system they wanted. It may simply not be possible to be that generic but I'd like to try.

At least I'd like to be able to inject a session property from the controller into my audit log.