Search code examples
sessiongrailsgrails-controllergrails-servicesgrails-config

Make user/session info available across the application in Grails


This is a class/flow design question that I am trying to wrestle with:

I am trying to make a user's session information available across the application my Grails application without having (or requiring) to pass HttpSession around between service layer and controller.

Currently, I have a filter, where once a user logs in, all the User related information are mapped into a UserSessionHelper class and I add an instance of that class to session like:

 session.userInfo = new UserSessionHelper(name:"x", isActive: true)

I want to be able to access the session object and it's contents everywhere in my application without explicitly passing it around (as parameter), in the same manner how grailsApplication is available throughout Grails.

Could someone give me some tips on how to accomplish this, in a thread-safe manner?

I am using Grails 2.4.2

Thanks.


Solution

  • There are a lot of ways to do the kinds of thing you are asking about but knowing the specifics would help. For example, it may be that you want a session scoped bean that holds the state in question and then access that bean wherever you need. Then you could have that bean injected into whatever controllers, services, tag libraries, other beans, etc. wherever you need to access that state.