Skip to content

Commit 8ab0d1b

Browse files
author
David Estes
committed
enhance gradle s2-quickstart to handle running in a web-plugin profile
1 parent ae5942a commit 8ab0d1b

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

plugin-core/docs/src/docs/introduction/gettingStarted.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ After installation, execute the `s2-quickstart` initialization script. This sets
4040
./gradlew runCommand -Pargs="s2-quickstart com.yourapp User Role"
4141
----
4242

43+
If you are installing into a Grails plugin instead of an application, you must make sure you are using the `web-plugin` profile. Otherwise dependncies will not be met.
44+
Running the same command will inject the spring beans into your `GrailsPlugin` classes `doWithSpring` method.
45+
4346
=== Plugin Configuration and Setup
4447

4548
The Spring Security plugin streamlines configuration and setup through a combination of steps:
@@ -52,4 +55,4 @@ The Spring Security plugin streamlines configuration and setup through a combina
5255

5356
The plugin configures Spring beans within the application context to implement various functionality components. Dependency management automatically handles the selection of appropriate jar files.
5457

55-
By following these steps, your Grails application will be ready to leverage the Spring Security plugin for enhanced security. While in-depth knowledge of Spring Security isn't mandatory, having a basic understanding of its underlying implementation can be helpful. For more details, refer to the [Spring Security documentation](https://{htmlsingle}).
58+
By following these steps, your Grails application will be ready to leverage the Spring Security plugin for enhanced security. While in-depth knowledge of Spring Security isn't mandatory, having a basic understanding of its underlying implementation can be helpful. For more details, refer to the [Spring Security documentation](https://{htmlsingle}).

plugin-core/plugin/grails-app/commands/grails.plugin.springsecurity/S2QuickstartCommand.groovy

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,16 @@ Example: ./grailsw s2-quickstart --uiOnly
205205
List<Map<String, String>> beans = []
206206
beans.add([import : "import ${userModel.packageName}.${userModel.simpleName}PasswordEncoderListener".toString(),
207207
definition: "${userModel.propertyName}PasswordEncoderListener(${userModel.simpleName}PasswordEncoderListener)".toString()])
208-
addBeans(beans, 'grails-app/conf/spring/resources.groovy')
208+
209+
if(new File('grails-app/conf/spring/resources.groovy').exists()) {
210+
addBeans(beans, 'grails-app/conf/spring/resources.groovy')
211+
} else {
212+
//we could be generating this in a plugin... we should look for a Plugin class
213+
File pluginClassFile = findPluginClass()
214+
if(pluginClassFile != null) {
215+
addBeansToPlugin(beans, pluginClassFile)
216+
}
217+
}
209218

210219

211220
generateFile('Authority', roleModel.packagePath, roleModel.simpleName)
@@ -301,5 +310,39 @@ Example: ./grailsw s2-quickstart --uiOnly
301310
}
302311
}
303312

313+
private void addBeansToPlugin(List<Map<String, String>> beans, File pluginClassFile) {
314+
List<String> lines = []
315+
if (pluginClassFile.exists()) {
316+
pluginClassFile.eachLine { line, nb ->
317+
lines << line
318+
if(line.contains('package')) {
319+
beans.forEach(bean -> lines.add(bean.import))
320+
}
321+
if (line.contains('doWithSpring()')) {
322+
beans.each { Map bean ->
323+
lines << ' ' + bean.definition
324+
}
325+
}
326+
}
327+
}
328+
329+
pluginClassFile.withWriter('UTF-8') { writer ->
330+
lines.each { String line ->
331+
writer.write "${line}${System.lineSeparator()}"
332+
}
333+
}
334+
}
335+
336+
337+
private File findPluginClass() {
338+
File pluginClass = null
339+
new File("src/main/groovy").eachFileRecurse { fl ->
340+
if (fl.isFile() && fl.name.endsWith("GrailsPlugin.groovy")) {
341+
pluginClass = fl
342+
}
343+
}
344+
return pluginClass
345+
}
346+
304347
}
305348

0 commit comments

Comments
 (0)