Tuesday, June 16, 2009

Acegi security pluing tag with Grails

Here are the most common tags to use with the Acegi and Grails:

g:ifallgranted role="ROLE_ADMIN,ROLE_SUPERVISOR">
g:ifanygranted role="ROLE_ADMIN,ROLE_SUPERVISOR">
g:ifnotgranted role="ROLE_USER">
g:isloggedin>
g:isnotloggedin>

Also you can get some extra info about the logged in user/

g:loggedinuserinfo field="username"> // as a gtag
${loggedInUserInfo(field:'username')} // as a GString-expression

Getting the loggedin or current User
If you have the Grails/Acegi plugin installed

def authenticateService

def list = {
def user = authenticateService.principal()
def username = user?.getUsername()


If you don't have Acegi or Jsecurity plugin installed

Object obj =
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String username;
if ( obj instanceof UserDetails ) {
username= ( (UserDetails)obj ).getUsername();
} else {
}

No comments: