views:

24

answers:

2

Hi everyone,

How I can verify that "at least one of the roles is granted" from a TagLib?

So, I want to use something like:

<sec:ifAnyGranted roles="ROLE_ADMIN,ROLE_SUPERVISOR">
but from the groovy file of my TagLib.

I am using Spring Security Core plugin.

Thanks in advance!

A: 

Use the following code in your taglib:

class MyTagLib {
  def springSecurityService // injected by Spring

  def myTag = { attrs, body ->
      def loggedIn = springSecurityService.loggedIn
  }
}
Stefan
+1  A: 

Use the following code in your taglib:

class MyTagLib {

  def myTag = { attrs, body ->
      if (SpringSecurityUtils.ifAnyGranted("ROLE_ADMIN,ROLE_SUPERVISOR")) {
      // do your stuff...
      }
  }
}
Aaron Saunders
At first I thought I should create an instance for SpringSecurityUtils, but it is a static class.Thanks for the reply. It has been very helpful.
tgarrey