Illep
Illep

Reputation: 16851

Find the user role of the currently logged in user

I want to know the current logged in Users User Role.

I have 2 roles in my application.

USER_ROLE and ADMIN_ROLE

How can i know if the current logged in user belongs to one of the above roles.

I tried the following code and i get an error:

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'
}

The error is :

Groovy:unable to resolve class 

org.grails.plugins.springsecurity.service.AuthenticateService

- Groovy:unable to resolve class 

I am using grails 2.2.4

SPring security ----> compile ':spring-security-core:2.0-RC2'

Upvotes: 0

Views: 660

Answers (2)

tim_yates
tim_yates

Reputation: 171154

Not sure where you got that from, but you need:

boolean isAdmin = grails.plugin.springsecurity.SpringSecurityUtils.ifAllGranted('ROLE_ADMIN')

Upvotes: 2

Ian Roberts
Ian Roberts

Reputation: 122414

You can find this information via static methods on the SpringSecurityUtils class

if(SpringSecurityUtils.ifAllGranted("ROLE_ADMIN")) ....

Upvotes: 2

Related Questions