toske
toske

Reputation: 1754

Grails - Write method available in any controller

I'm quite new into groovy / grails world, so forgive for asking simple question. I'm pretty sure this can be done by meta-programming, that is injecting method into definition of grails controller class or so. Can anyone point me where should this injection be written (Bootstrap.groovy?). I'm trying to create currentUser() method with Spring-security-core plugin that I could use in any controller.

Upvotes: 0

Views: 1012

Answers (1)

David Tinker
David Tinker

Reputation: 9634

Your question is answered in these two posts:

However there are a couple of other approaches you might consider ...

You could just inject a "SecurityService" into controllers that need to know the current user and put "getCurrentUser" on that.

You could use a Grails filter to add "currentUser" to the params map available to all controllers. I like that approach as I usually use a filter to implement security anyway. The filter can also add it to all models prior to view rendering so all your views have access to it.

Upvotes: 2

Related Questions