You want to use the grails filter. Using the filter, you can specify which controllers and methods (using wildcards) you want to intercept using the methods before / after and after View.
This makes it easy to populate a new variable in the model, so it is available in the view. Here is an example that uses the acegi authenticateService plugin:
class SecurityFilters { def authenticateService def filters = { all(controller:'*', action:'*') { after = { model -> def principal = authenticateService.principal() if (principal != null && principal != 'anonymousUser') { model?.loggedInUser = principal?.domainClass log.debug("SecurityFilter: adding current user to model = $model") } else { log.debug("SecurityFilter: anonymous user, model = $model") } } } } }
source share