Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails with SpringSecurity, check if the current user can access controller / action

I'm currently developing a menu for my application that should be able to display only the controllers that the current user can access (requestmap defined in the database).

How can I check if the current user has access to a specific controller and action?

like image 372
Jan Avatar asked Feb 21 '10 16:02

Jan


2 Answers

To check roles in view : Spring security plugin provides ifAllGranted, ifAnyGranted, ifNoneGranted etc., tags to check roles

For example, to check Admin Role of logged in User :

<sec:ifLoggedIn>
    <sec:ifAllGranted roles="ROLE_ADMIN">
        Admin resource
     </sec:ifAllGranted>
</sec:ifLoggedIn>

(tested in grails-2.2.2 and springSecurityCorePlugin-1.2.7.3)

like image 63
Thamme Gowda Avatar answered Sep 19 '22 14:09

Thamme Gowda


org.grails.plugins.springsecurity.service.AuthenticateService authenticateService = new org.grails.plugins.springsecurity.service.AuthenticateService()
def isAdmin = authenticateService.ifAllGranted('ROLE_ADMIN')

if(isAdmin) {
   println 'I am Admin'
}
like image 34
user762266 Avatar answered Sep 20 '22 14:09

user762266