Skip to content

Commit

Permalink
Added security bridge implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
davydotcom committed Jan 29, 2014
1 parent 807adac commit 0f45542
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store
target/
log/
8 changes: 8 additions & 0 deletions README.md
@@ -0,0 +1,8 @@
Grails Security Bridge
======================

The Grails Security Bridge plugin is used for providing a decoupled, cross-plugin security interface. This allows you to keep the majority of authentication logic in one plugin, while other plugins can reference a public API interface to retrieve the information needed.

Documentation
-------------
http://bertramdev.github.io/grails-security-bridge
Expand Up @@ -58,7 +58,7 @@ class SharedSecurityService implements SecurityBridge {
/**
* Check if the user is currently logged in.
*/
def isLoggedIn() {
boolean isLoggedIn() {
securityBridge.isLoggedIn
}

Expand Down Expand Up @@ -101,7 +101,7 @@ class SharedSecurityService implements SecurityBridge {
* @return Must return a Map of arguments to pass to g:link to create the link
*/
Map createLink(String action) {
securityBridge(failOnError: true).createLink(action)
getSecurityBridge(failOnError: true).createLink(action)
}

def ifAuthorized(object, action, Closure code) {
Expand Down
85 changes: 85 additions & 0 deletions src/docs/guide/configuration.gdoc
@@ -0,0 +1,85 @@
The Security-Bridge is kept relatively simple so as to not overcreep on scope. All that needs to be done is to define a security bridge and register this as a spring bean.

The interface is as follows:

{code}
package org.grails.plugin.securitybridge

interface SecurityBridge {

/**
* Returns the current user object if they are logged in
* @return the implementation's user object or null if nobody is logged in
*/
def getCurrentUser()

/**
* Get the user Identifier.
* @return the user identity or null if nobody is logged in
*/
def getUserIdentity()

/**
* Returns the current account object of the logged in user
* @return the implementation's account (for basic auth can just be the user object) object or null if nobody is logged in
*/
def getCurrentAccount()

/**
* Returns the current users account identity. (Useful if multiple users are tied to one account)
* @return the account name or identity, null if nobody is logged in.
*/
def getAccountIdentity()

/**
* Return the current users display name.
*/
def getCurrentUserDisplayName()

/**
* Check if the user is currently logged in.
*/
boolean isLoggedIn()

/**
* Check if the currently logged in user is authorized to perform an action on the passed object
* @param object The object with which we are dealing with.
* @param action The action you would like to perform
*/
boolean isAuthorized(object, action)

/**
* Check if the currently logged in user has the specified role
* @param role
*/
boolean hasRole(role)

/**
* Store the request location for the security service to redirect to upon login success
* @param request The request object
*/
def storeLocation(request)

/**
* Execute code masquerading as the specified user, for the duration of the Closure block
* @return Whatever the closure returns
*/
def withUser(identity, Closure code)

/**
* Create a link to the specified security action
* @param action One of "login", "logout", "signup"
* @return Must return a Map of arguments to pass to g:link to create the link
*/
Map createLink(String action)
}
{code}

Simply implementing a class that defines all these methods will create a legitimate securityBridge. Next we need to register this bridge with spring. This can be done in your application's @resources.groovy@ file or in a plugins @doWithSpring@ method.

{code}
sharedSecurityBridge(com.mycompany.MySecurityBridge) {
//Add any other spring injected references you may need
springSecurityService = ref('springSecurityService')
}
{code}
3 changes: 3 additions & 0 deletions src/docs/guide/introduction.gdoc
@@ -0,0 +1,3 @@
The Grails Security Bridge plugin is used for providing a decoupled, cross-plugin security interface. This allows you to keep the majority of authentication logic in one plugin, while other plugins can reference a public API interface to retrieve the information needed.

This guide documents how to configure and setup a @sharedSecurityBridge@ for use throughout other plugins.
3 changes: 3 additions & 0 deletions src/docs/guide/toc.yml
@@ -0,0 +1,3 @@
introduction: Introduction
configuration: Configuration
usage: Usage
16 changes: 16 additions & 0 deletions src/docs/guide/usage.gdoc
@@ -0,0 +1,16 @@
Now that the security bridge is configured, The spring security bridge can be used anywhere throughout your app by simply defining the @sharedSecurityService@.

The @sharedSecurityService@ provides access to all methods created in the bridge, as well as some additional methods to make things a bit easier.

* @getCurrentUser()@
* @getCurrentAccount()@
* @getUserIdentity()@
* @getAccountIdentity()@
* @getCurrentUserDisplayName()@
* @isAuthorized(object,action)@
* @isLoggedIn()@
* @hasAnyRole(role)@
* @ifAuthorized(object,action,Closure code)@

For More methods please take a look at your SecurityBridge interface.

0 comments on commit 0f45542

Please sign in to comment.