This is specifically about a grails 1.3.7 application, but hopefully the answers will work for newer versions as well. The code below is a simplified version of what is needed. accountService is being injected. The below snippet does what it's supposed to do but clearly is repeated code. This is in a UserFilter class located in grails-app/conf
How do I extract common logic out of my filters and maintain the capability to redirect and check the session? I've tried extracting out a method into the filter class, passing in the session and flash, but the redirect was still giving me issues.
def filters = {
// ... other filters ...
adminAllCheck(controller: 'administration', action: '*') {
before = {
if(!session.isAdmin) {
if(accountService.isAdmin()) {
session.isAdmin = true
} else {
flash.message = 'Non admin'
redirect(controller: 'home', action: 'index')
return false
}
}
true
}
}
userListCheck(controller: 'user', action: 'list') {
before = {
if(!session.isAdmin) {
if(accountService.isAdmin()) {
session.isAdmin = true
} else {
flash.message = 'Non admin'
redirect(controller: 'home', action: 'index')
return false
}
}
true
}
}
}
One way to create a helper method is to create it outside of the
filtersclosure, and pass the instance in. It doesn't work to pass inthisbecause that's not the closure but theUserFiltersinstance. Instead pass in the closure'sdelegatewhich is where therenderandredirectmethods are added, where the attributes likeparamsandcontrollerNameare:You can also use the
|character in thecontrollerandactionarguments to do common work across controllers. It wouldn't work here directly since you use*for the admin controller and only apply to thelistaction in the user controller, but you can do an explicit controller/action name check for that:You could also move the logic to a service and dependency-inject that back in. It's usually not a good idea to mix tiers like that and have HTTP logic in a service, but that would keep the logic in one place. You could use the same trick with the delegate, or just pass in the session/request/response/etc. as needed.