(Quick Reference)

23 Controller MetaClass Methods - Reference Documentation

Authors: Burt Beckwith, Beverley Talbott

Version: 2.0.0

23 Controller MetaClass Methods

The plugin registers some convenience methods into all controllers in your application. All are accessor methods, so they can be called as methods or properties. They include:

isLoggedIn

Returns true if there is an authenticated user.

class MyController {

def someAction() { if (isLoggedIn()) { … }

...

if (!isLoggedIn()) { … }

// or

if (loggedIn) { … }

if (!loggedIn) { … } } }

getPrincipal

Retrieves the current authenticated user's Principal (a GrailsUser instance unless you've customized this) or null if not authenticated.

class MyController {

def someAction() { if (isLoggedIn()) { String username = getPrincipal().username … }

// or

if (isLoggedIn()) { String username = principal.username … } } }

getAuthenticatedUser

Loads the user domain class instance from the database that corresponds to the currently authenticated user, or null if not authenticated. This is the equivalent of adding a dependency injection for springSecurityService and calling PersonDomainClassName.get(springSecurityService.principal.id) (the typical way that this is often done).

class MyController {

def someAction() { if (isLoggedIn()) { String email = getAuthenticatedUser().email … }

// or

if (isLoggedIn()) { String email = authenticatedUser.email … } } }