Grails 3 Interceptors

34 views
Skip to first unread message

Joud Kassem

unread,
Nov 26, 2018, 7:11:50 AM11/26/18
to Grails Dev Discuss
Hi,

How to call a function defined in a controller in its interceptor in grails 3? 

Søren Berg Glasius

unread,
Nov 26, 2018, 7:22:45 AM11/26/18
to grails-de...@googlegroups.com
You could find the bean instance of your controller and call the method defining the function.

Something like this:

MyController controller = applicationContext.getBean(MyController)

controller.callMyFunction()



Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


On Mon, 26 Nov 2018 at 13:11, Joud Kassem <joud.k...@gmail.com> wrote:
Hi,

How to call a function defined in a controller in its interceptor in grails 3? 

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.
To post to this group, send email to grails-de...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grails-dev-discuss/c0ac05a7-feca-49cb-8359-853faebe4dcb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joud Kassem

unread,
Nov 26, 2018, 8:38:08 AM11/26/18
to Grails Dev Discuss
When I do this, I get an error "No bean named 'auth' available"


On Monday, November 26, 2018 at 1:22:45 PM UTC+1, Søren Berg Glasius wrote:
You could find the bean instance of your controller and call the method defining the function.

Something like this:

MyController controller = applicationContext.getBean(MyController)

controller.callMyFunction()



Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


On Mon, 26 Nov 2018 at 13:11, Joud Kassem <joud.k...@gmail.com> wrote:
Hi,

How to call a function defined in a controller in its interceptor in grails 3? 

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-discuss+unsub...@googlegroups.com.

Joud Kassem

unread,
Nov 26, 2018, 9:10:57 AM11/26/18
to Grails Dev Discuss
Is it possible to get a bean instance from a controller? I'm getting that the bean is not available!


On Monday, November 26, 2018 at 1:22:45 PM UTC+1, Søren Berg Glasius wrote:
You could find the bean instance of your controller and call the method defining the function.

Something like this:

MyController controller = applicationContext.getBean(MyController)

controller.callMyFunction()



Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


On Mon, 26 Nov 2018 at 13:11, Joud Kassem <joud.k...@gmail.com> wrote:
Hi,

How to call a function defined in a controller in its interceptor in grails 3? 

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-discuss+unsub...@googlegroups.com.

Søren Berg Glasius

unread,
Nov 27, 2018, 12:41:13 AM11/27/18
to grails-de...@googlegroups.com
What are you actually trying to archive? The question is, if you would be better off having the method you need to call in a service instead, and then inject that service.

Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.

On Mon, 26 Nov 2018 at 15:10, Joud Kassem <joud.k...@gmail.com> wrote:
Is it possible to get a bean instance from a controller? I'm getting that the bean is not available!

On Monday, November 26, 2018 at 1:22:45 PM UTC+1, Søren Berg Glasius wrote:
You could find the bean instance of your controller and call the method defining the function.

Something like this:

MyController controller = applicationContext.getBean(MyController)

controller.callMyFunction()



Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


On Mon, 26 Nov 2018 at 13:11, Joud Kassem <joud.k...@gmail.com> wrote:
Hi,

How to call a function defined in a controller in its interceptor in grails 3? 

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-disc...@googlegroups.com.

To post to this group, send email to grails-de...@googlegroups.com.

Joud Kassem

unread,
Nov 27, 2018, 3:21:34 AM11/27/18
to Grails Dev Discuss
This is actually what I ended up doing yesterday... having the method in a service and calling it in the interceptor


On Tuesday, November 27, 2018 at 6:41:13 AM UTC+1, Søren Berg Glasius wrote:
What are you actually trying to archive? The question is, if you would be better off having the method you need to call in a service instead, and then inject that service.

Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


On Mon, 26 Nov 2018 at 15:10, Joud Kassem <joud.k...@gmail.com> wrote:
Is it possible to get a bean instance from a controller? I'm getting that the bean is not available!

On Monday, November 26, 2018 at 1:22:45 PM UTC+1, Søren Berg Glasius wrote:
You could find the bean instance of your controller and call the method defining the function.

Something like this:

MyController controller = applicationContext.getBean(MyController)

controller.callMyFunction()



Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.


On Mon, 26 Nov 2018 at 13:11, Joud Kassem <joud.k...@gmail.com> wrote:
Hi,

How to call a function defined in a controller in its interceptor in grails 3? 

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-discuss+unsub...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Grails Dev Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-dev-discuss+unsub...@googlegroups.com.

Jeff Scott Brown

unread,
Nov 27, 2018, 6:28:34 PM11/27/18
to grails-de...@googlegroups.com
On 26 Nov 2018, at 6:22, Søren Berg Glasius wrote:

> You could find the bean instance of your controller and call the
> method
> defining the function.
>
> Something like this:
>
> MyController controller = applicationContext.getBean(MyController)
>
> controller.callMyFunction()
>
>

There is really no good reason to ever do that.

It is hard to say exactly what the best solution is without knowing what
you are trying to accomplish but a likely solution is to have that logic
in a Service and inject that service into the interceptor. If the same
behavior is needed from the controller, inject the same service into he
controller.



JSB

--
Jeff Scott Brown
OCI Partner and Principal Software Engineer
OCI Grails and Micronaut Practice Lead

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

Joud Kassem

unread,
Nov 28, 2018, 3:52:33 AM11/28/18
to Grails Dev Discuss
Hi Jeff,

We call some controller functions in the before interceptor to manipulate the passed parameters and check user's permissions ..etc This was fine in grails 2 when the interceptors were defined in the controller itself but in Grails 3, it became a problem. But moving the logic to a service and injecting the service into the interceptor worked fine + I had to move some logic to the interceptor itself... I don't know if this is the best solution but I didn't find another one..

Jeff Scott Brown

unread,
Nov 28, 2018, 8:13:02 AM11/28/18
to Grails Dev Discuss
On 28 Nov 2018, at 2:52, Joud Kassem wrote:

> Hi Jeff,
>
> We call some controller functions in the before interceptor to
> manipulate
> the passed parameters and check user's permissions ..etc This was fine
> in
> grails 2 when the interceptors were defined in the controller itself
> but in
> Grails 3, it became a problem. But moving the logic to a service and
> injecting the service into the interceptor worked fine + I had to move
> some
> logic to the interceptor itself... I don't know if this is the best
> solution but I didn't find another one..
>

Great.

FYI… You should be able to manipulated params and check user
permissions in an interceptor without ever interacting with a
controller. All of the relevant information is available in the
interceptor itself.

I am glad you got a working solution. Well done!

Joud Kassem

unread,
Nov 28, 2018, 2:29:29 PM11/28/18
to Grails Dev Discuss
Thank you! Out of curiosity, is there a guide or a bit of information about how to check user permissions directly in interceptors?

All the best,
Joud

Jeff Scott Brown

unread,
Nov 28, 2018, 4:41:52 PM11/28/18
to Grails Dev Discuss
On 28 Nov 2018, at 13:29, Joud Kassem wrote:

> Thank you! Out of curiosity, is there a guide or a bit of information about
> how to check user permissions directly in interceptors?
>

The way to do that depends on what “check user permissions” really means.

How do you keep track of permissions?
Reply all
Reply to author
Forward
0 new messages