Q:

Grails: Redirect from called method

Good morning Community.
I use Grails for a backend app and therefore want to check if users are allowed to do the requested action.
So I call checkPermission() at the begin and redirect to the index page if user is not allowed. But I'm occuring this error: 
Cannot issue a redirect(..) here. A previous call to redirect(..) has already redirected the response.
Extract from my source:
def test() {
  println("test()")
  checkPermission([TaskXYZ])
  println("AfterCheck")
  redirect(action: "profile", params: [lang: params.lang])
}    

private checkPermission(def tasks) {
  println("checkPermission()")
  if ( ! userService.isAllowed(tasks)) {
    println("NotAllowed")
    flash.error = message(code: "msg.actionIsNotAllowed")
    redirect(action: "index", params: [lang: params.lang])
    return false
  }
}

Output:
test()
checkPermission()
NotAllowed
AfterCheck

So the redirect doesn't work from the private method and return false causes the return to the called method.
What can I do alternatively? I considered beforeInterceptor, but I need to pass custom parameters for every method.
Thanks,
Rob.

A:

The problem you are encountering is that issuing a redirect doesn't stop the execution of the controller method/action. After your redirect within checkPermission your controller code continues to execute after checkPermission and is encountering the next redirect. You need to return from your controller if your checkPermission is to terminate the execution of the controller method/action.
For example:
...
private boolean checkPermission(def tasks) { ... }
...
if (!checkPermission([TaskXYZ])) return
...

