forked from bradleybeddoes/nimble
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NimbleGrailsPlugin.groovy
151 lines (121 loc) · 4.92 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
/*
* Nimble, an extensive application base for Grails
* Copyright (C) 2009 Intient Pty Ltd
*
* 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 intient.nimble.domain.User
class NimbleGrailsPlugin {
// the plugin version
def version = "0.2"
// 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-SNAPSHOT",
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 = "Intient Pty Ltd + 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://intient.com/products/nimble"
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 controllers
application.controllerClasses?.each { controller ->
controller.metaClass.getAuthenticatedUser = {
def authUser = User.get(SecurityUtils.getSubject()?.getPrincipal())
if (!authUser) {
log.error("Authenticated user was not able to be obtained from metaclass")
return null
}
return authUser
}
}
// Supply functionality to services
application.serviceClasses?.each { service ->
service.metaClass.getAuthenticatedUser = {
def authUser = User.get(SecurityUtils.getSubject()?.getPrincipal())
if (!authUser) {
log.error("Authenticated user was not able to be obtained from metaclass")
return null
}
return authUser
}
}
}
def onChange = { event ->
doWithDynamicMethods()
}
def onConfigChange = { event ->
}
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
}
}