I’m writing a small webapp in Grails, and to make sure all users are authenticated I’m using the following filter:
class LoginFilters { static filters = { loginCheck(controller:'*', action:'*') { before = { if (session.user_id) { request.user = User.get(session.user_id) } else if (!actionName.equals('login')) { redirect(controller: 'login', action: 'login') return false } } } } }
And all controller methods start with reading the user property of the request object:
def actionName = { def user = request.user ... }
The code above works, but I’d rather avoid the duplicate code in the all controller methods. Would it be possible for the filter to bind the user object to a variable named ‘user’ instead of ‘request.user’, that will be accessible from all controllers?
I understand that there might be scoping issues that makes this impossible, but the Grails framework seems to be able to create quite some magic under the hood, so I figured it might be worth asking.
Using the beforeInterceptor in a controller may help: