-
Notifications
You must be signed in to change notification settings - Fork 46
/
NimbleGrailsPlugin.groovy
167 lines (133 loc) · 5.41 KB
/
NimbleGrailsPlugin.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Nimble, an extensive application base for Grails
* Copyright (C) 2010 Bradley Beddoes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import grails.util.GrailsUtil
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.springframework.mail.javamail.JavaMailSenderImpl
import org.apache.shiro.authc.credential.Sha256CredentialsMatcher
import org.apache.shiro.SecurityUtils
import grails.plugins.nimble.core.UserBase
class NimbleGrailsPlugin {
// the plugin version
def version = "0.4-SNAPSHOT"
// the version or versions of Grails the plugin is designed for
def grailsVersion = "1.1 > *"
// the other plugins this plugin depends on
def dependsOn = [ shiro: "1.0.1",
mail: "0.6 > *",
]
// resources that are excluded from plugin packaging
def pluginExcludes = [
'grails-app/conf/NimbleConfig.groovy',
'grails-app/conf/NimbleUrlMappings.groovy',
'grails-app/conf/NimbleSecurityFilters.groovy',
'grails-app/conf/NimbleBootStrap.groovy',
]
def author = "Bradley Beddoes and open source contributors"
def authorEmail = "[email protected]"
def title = "Nimble"
def description = '''\\
Nimble is an extensive application base environment for Grails.
'''
// URL to the plugin's documentation
def documentation = "http://sites.google.com/site/nimbledoc/"
def observe = ['controllers']
def doWithSpring = {
loadNimbleConfig(application)
credentialMatcher(Sha256CredentialsMatcher) {
storedCredentialsHexEncoded = true
}
/*
* Ok we have all the config the user has supplied for Nimble,
* recreate any objects from dependent plugins who previously had
* no config
*/
// Redefine mailSender
def mailConfig = application.config.nimble.messaging.mail
mailSender(JavaMailSenderImpl) {
host = mailConfig.host ?: "localhost"
defaultEncoding = mailConfig.encoding ?: "utf-8"
if(mailConfig.port)
port = mailConfig.port
if(mailConfig.username)
username = mailConfig.username
if(mailConfig.password)
password = mailConfig.password
if(mailConfig.protocol)
protocol = mailConfig.protocol
if(mailConfig.props instanceof Map && mailConfig.props)
javaMailProperties = mailConfig.props
}
}
def doWithApplicationContext = { applicationContext ->
}
def doWithWebDescriptor = { xml ->
}
def doWithDynamicMethods = { ctx ->
// Supply functionality to filters
application.filtersClasses.each { filter ->
// Should be used after verified call to 'accessControl'
log.debug("Injecting Nimble methods to Filter ${filter}")
injectAuthn(filter, application)
}
// Supply functionality to controllers
application.controllerClasses?.each { controller ->
log.debug("Injecting Nimble methods to Controller ${controller}")
injectAuthn(controller, application)
}
// Supply functionality to services
application.serviceClasses?.each { service ->
log.debug("Injecting Nimble methods to Service ${service}")
injectAuthn(service, application)
}
}
def onChange = { event ->
doWithDynamicMethods()
}
def onConfigChange = { event ->
}
private void injectAuthn(def clazz, def application) {
clazz.metaClass.getAuthenticatedSubject = {
def subject = SecurityUtils.getSubject()
}
clazz.metaClass.getAuthenticatedUser = {
def principal = SecurityUtils.getSubject()?.getPrincipal()
def authUser
if(application.config?.nimble?.implementation?.user)
authUser = grailsApplication.classLoader.loadClass(application.config.nimble.implementation.user).get(principal)
else
authUser = UserBase.get(principal)
if (!authUser) {
log.error("Authenticated user was not able to be obtained from metaclass")
return null
}
return authUser
}
}
private ConfigObject loadNimbleConfig(GrailsApplication grailsApplication) {
def config = grailsApplication.config
GroovyClassLoader classLoader = new GroovyClassLoader(getClass().classLoader)
// Merging default Nimble config into main application config
config.merge(new ConfigSlurper(GrailsUtil.environment).parse(classLoader.loadClass('DefaultNimbleConfig')))
// Merging user-defined Nimble config into main application config if provided
try {
config.merge(new ConfigSlurper(GrailsUtil.environment).parse(classLoader.loadClass('NimbleConfig')))
} catch (Exception ignored) {
// ignore, just use the defaults
}
return config
}
}