Skip to content

Commit 0f45542

Browse files
committed
Added security bridge implementation
1 parent 807adac commit 0f45542

File tree

7 files changed

+120
-2
lines changed

7 files changed

+120
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
target/
3+
log/

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Grails Security Bridge
2+
======================
3+
4+
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.
5+
6+
Documentation
7+
-------------
8+
http://bertramdev.github.io/grails-security-bridge

grails-app/services/org/grails/plugin/securitybridge/SharedSecurityService.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class SharedSecurityService implements SecurityBridge {
5858
/**
5959
* Check if the user is currently logged in.
6060
*/
61-
def isLoggedIn() {
61+
boolean isLoggedIn() {
6262
securityBridge.isLoggedIn
6363
}
6464

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

107107
def ifAuthorized(object, action, Closure code) {

src/docs/guide/configuration.gdoc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
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.
2+
3+
The interface is as follows:
4+
5+
{code}
6+
package org.grails.plugin.securitybridge
7+
8+
interface SecurityBridge {
9+
10+
/**
11+
* Returns the current user object if they are logged in
12+
* @return the implementation's user object or null if nobody is logged in
13+
*/
14+
def getCurrentUser()
15+
16+
/**
17+
* Get the user Identifier.
18+
* @return the user identity or null if nobody is logged in
19+
*/
20+
def getUserIdentity()
21+
22+
/**
23+
* Returns the current account object of the logged in user
24+
* @return the implementation's account (for basic auth can just be the user object) object or null if nobody is logged in
25+
*/
26+
def getCurrentAccount()
27+
28+
/**
29+
* Returns the current users account identity. (Useful if multiple users are tied to one account)
30+
* @return the account name or identity, null if nobody is logged in.
31+
*/
32+
def getAccountIdentity()
33+
34+
/**
35+
* Return the current users display name.
36+
*/
37+
def getCurrentUserDisplayName()
38+
39+
/**
40+
* Check if the user is currently logged in.
41+
*/
42+
boolean isLoggedIn()
43+
44+
/**
45+
* Check if the currently logged in user is authorized to perform an action on the passed object
46+
* @param object The object with which we are dealing with.
47+
* @param action The action you would like to perform
48+
*/
49+
boolean isAuthorized(object, action)
50+
51+
/**
52+
* Check if the currently logged in user has the specified role
53+
* @param role
54+
*/
55+
boolean hasRole(role)
56+
57+
/**
58+
* Store the request location for the security service to redirect to upon login success
59+
* @param request The request object
60+
*/
61+
def storeLocation(request)
62+
63+
/**
64+
* Execute code masquerading as the specified user, for the duration of the Closure block
65+
* @return Whatever the closure returns
66+
*/
67+
def withUser(identity, Closure code)
68+
69+
/**
70+
* Create a link to the specified security action
71+
* @param action One of "login", "logout", "signup"
72+
* @return Must return a Map of arguments to pass to g:link to create the link
73+
*/
74+
Map createLink(String action)
75+
}
76+
{code}
77+
78+
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.
79+
80+
{code}
81+
sharedSecurityBridge(com.mycompany.MySecurityBridge) {
82+
//Add any other spring injected references you may need
83+
springSecurityService = ref('springSecurityService')
84+
}
85+
{code}

src/docs/guide/introduction.gdoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
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.
2+
3+
This guide documents how to configure and setup a @sharedSecurityBridge@ for use throughout other plugins.

src/docs/guide/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
introduction: Introduction
2+
configuration: Configuration
3+
usage: Usage

src/docs/guide/usage.gdoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Now that the security bridge is configured, The spring security bridge can be used anywhere throughout your app by simply defining the @sharedSecurityService@.
2+
3+
The @sharedSecurityService@ provides access to all methods created in the bridge, as well as some additional methods to make things a bit easier.
4+
5+
* @getCurrentUser()@
6+
* @getCurrentAccount()@
7+
* @getUserIdentity()@
8+
* @getAccountIdentity()@
9+
* @getCurrentUserDisplayName()@
10+
* @isAuthorized(object,action)@
11+
* @isLoggedIn()@
12+
* @hasAnyRole(role)@
13+
* @ifAuthorized(object,action,Closure code)@
14+
15+
For More methods please take a look at your SecurityBridge interface.
16+

0 commit comments

Comments
 (0)