root / spepintegrators / grails / SpepGrailsPlugin.groovy @ ce900dabd54eaaf8d7cebbf37b56ad2fc7871910

View | Annotate | Download (3 KB)

1
import grails.spep.Spep
2
import grails.spep.SpepUser
3
import grails.util.Environment
4
import org.springframework.aop.scope.ScopedProxyFactoryBean
5
import org.codehaus.groovy.grails.web.servlet.WrappedResponseHolder
6
7
class SpepGrailsPlugin {
8
	def version = "1.2"
9
    def grailsVersion = "1.1"   // not sure if this is correct, but the plugin doesn't work with at least 1.2 because of the jetty dependencies
10
	def dependsOn = [:]
11
12
	def author = "Shaun Mangelsdorf"
13
	def authorEmail = "s.mangelsdorf@gmail.com"
14
	def title = "Integrates the SPEP filter with a Grails application"
15
	def description = '''\
16
Integrates the SPEP filter with a Grails application.
17
'''
18
19
	def documentation = "http://esoeproject.qut.edu.au/wiki/esoe/SPEP_Grails_Plugin"
20
21
	final static DEFAULT_CONFIG = [
22
		beanName: "spepUser", 
23
		userClass: SpepUser, 
24
		enabled: false, 
25
		logoutUrl: null
26
	]
27
	
28
	void rationaliseConfig(config) {
29
		if (config.containsKey('spep')) {
30
			DEFAULT_CONFIG.each {
31
				if (!config.containsKey(it.key)) {
32
					config[it.key] = it.value
33
				}
34
			}
35
		} else {
36
			config.spep = DEFAULT_CONFIG.clone()
37
		}
38
	}
39
	
40
	def doWithSpring = {
41
		rationaliseConfig(application.config)
42
43
		if (application.config.spep.enabled && application.config.spep.userClass) {
44
			
45
			spepUserSessionBean(application.config.spep.userClass) {
46
				it.scope = "session"
47
				it.autowire = "byName"
48
			}
49
50
			"${application.config.spep.beanName}"(ScopedProxyFactoryBean) {
51
				targetBeanName = "spepUserSessionBean"
52
				proxyTargetClass = false
53
			}
54
		} else {
55
			log.info("SPEP: enabled is false")
56
		}
57
		
58
		spep(Spep) {
59
			enabled = application.config.spep.enabled
60
			user = ref(application.config.spep.beanName)
61
		}
62
		
63
	}
64
   
65
	def doWithWebDescriptor = { xml ->
66
		if (application.config.spep.enabled) {
67
            // the order of filters in the XML file seems to matter ... and as of tomcat 7, you can't have duplicate definitions (which is what this used to create)
68
            def filters = xml.'filter'
69
			filters[0] + {
70
				'filter' {
71
					'filter-class'("com.qut.middleware.spep.filter.SPEPFilter")
72
					'filter-name'("spep-grails-plugin-filter")
73
					'init-param' {
74
						'param-name'("spep-context")
75
						'param-value'(application.config.spep.contextName ?: "/spep")
76
					}
77
				}
78
			}
79
            def filterMappings = xml.'filter-mapping'
80
			filterMappings[0] + {
81
				'filter-mapping' {
82
					'filter-name'("spep-grails-plugin-filter")
83
					'url-pattern'("/*")
84
				}
85
			}
86
		}
87
	}
88
	
89
	def doWithDynamicMethods = {
90
		if (application.config.spep.enabled) {
91
			application.config.spep.userClass.metaClass.logout = { ->
92
				def response = WrappedResponseHolder.wrappedResponse
93
				def logoutUrl = application.config.spep.logoutUrl
94
95
				if (response) {
96
					if (logoutUrl) {
97
						response.sendRedirect(logoutUrl)
98
					} else {
99
						throw new IllegalStateException("Cannot logout spep user as spep.logoutUrl is not defined in application config")
100
					}
101
				} else {
102
					throw new IllegalStateException("Cannot logout spep user as response is unavailable")
103
				}
104
105
				false
106
			}
107
		}
108
	}
109
}