Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify/override filter provided by Grails plugin

Tags:

filter

grails

In my Grails project, I would like to override a filter provided by a plugin so it better fits my needs.

More specifically, the filter is defined so it is applied to all actions on all controllers: filterName(controller:"*", action:"*") { ... }, and I want to restrict it to only certain controllers.

I tried to create a filter class in my project with the same name as the filter I want to override, but the result is both filters are being executed on every request.

So does anyone know how to change/override/(or even deactivate) a filter provided by a plugin? Thanks in advance!

like image 513
Germán Sancho Avatar asked Apr 07 '26 11:04

Germán Sancho


1 Answers

dmahapatro's answer put me in the way to finding a solution: the key concept is getting access to the filterInterceptor bean, which contains the definitions of all filters in the Grails application. It can be accessed e.g. in the BootStrap.groovy file in order to modify available filters at application startup:

class BootStrap {
    def filterInterceptor

    def init = { servletContext ->
        // modify myPluginFilter provided by plugin so it 
        // is only applied to certain requests
        def myPluginFilterHandler = filterInterceptor.handlers.find{ it.filterConfig.name == 'myPluginFilter' }
        myPluginFilterHandler.filterConfig.scope.controller = 'myController'
        myPluginFilterHandler.filterConfig.scope.action = 'myAction'
        myPluginFilterHandler.afterPropertiesSet()

        log.info "myPluginFilter scope modified"
    }

    ...
}

This code is executed once at application startup and it finds the filter myPluginFilter (e.g., defined in a plugin) and changes its scope (the controller and action to which it is applied).

The filter could be destroyed instead of redefined by removing myPluginFilterHandler from the filterInterceptor.handlers collection.

like image 121
Germán Sancho Avatar answered Apr 09 '26 23:04

Germán Sancho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!