Grails security filter all action but except one

3.9k Views Asked by At

I'm gonna create a security filter for my project. I check if !session.user then redirect to action error. Here is my current code:

all(controller: 'accounting|installation|installer|sales|service|serviceOrder|document', action: '*') {
            before = {
                if (!session.user) {
                    redirect(controller: 'installation', action: 'errors')
                    return false
                }
            }
            after = { Map model ->

            }
            afterView = { Exception e ->

            }
        }

However the point is that session.user being created in controller 'installation' and action 'index'. So how can I filter without index action? Any suggestions will be appreciated. Thanks.

3

There are 3 best solutions below

2
Anand Kushwaha On BEST ANSWER

Try this

all(controller: 'accounting|installation|installer|sales|service|serviceOrder|document', action: '*') {
        before = {
         if (!(controllerName == 'installation' && actionName == 'index')) {
            if (!session.user) {
                redirect(controller: 'installation', action: 'errors')
                return false
            }
          }
        }
        after = { Map model ->

        }
        afterView = { Exception e ->

        }
    }
2
Madhu Bose On

Hope I have understood your question ,Since you want to exclude action index then ,try this ..

all(controller: 'accounting|installation|installer|sales|service|serviceOrder|document', action: '*',actionExclude:'index'){....

Regards

0
ABC On

You can use invert:true e.g

def filters = {
    allExceptIndex(controller:"installation",action:"index",invert:true) {
        before = {
        }
        after = { Map model ->
        }
        afterView = { Exception e ->
        }
    }
}

For further reference see Blog