Skip to content

Commit f7cea30

Browse files
authored
Merge pull request #1177 from apache/patch-quickstart-plugin
enhance gradle s2-quickstart to handle running in a web-plugin profile
2 parents ae1acf9 + 7ecf9c5 commit f7cea30

File tree

2 files changed

+127
-4
lines changed

2 files changed

+127
-4
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 dependencies 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: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ Example: ./grailsw s2-quickstart --uiOnly
7777
initializeTemplateAttributes()
7878
createDomains(userModel, roleModel, requestmapModel, roleGroupModel)
7979
}
80-
81-
updateConfig(userModel?.simpleName, roleModel?.simpleName, requestmapModel?.simpleName, userModel?.packageName, roleGroupModel != null)
80+
if (new File('grails-app/conf/application.yml').exists()) {
81+
consoleLogger.addStatus('\nConfiguring Spring Security; Updating YAML Application Config')
82+
updateYmlConfig(userModel?.simpleName, roleModel?.simpleName, requestmapModel?.simpleName, userModel?.packageName, roleGroupModel != null)
83+
} else {
84+
updateConfig(userModel?.simpleName, roleModel?.simpleName, requestmapModel?.simpleName, userModel?.packageName, roleGroupModel != null)
85+
}
8286
logStatus()
8387
return SUCCESS
8488
}
@@ -205,7 +209,16 @@ Example: ./grailsw s2-quickstart --uiOnly
205209
List<Map<String, String>> beans = []
206210
beans.add([import : "import ${userModel.packageName}.${userModel.simpleName}PasswordEncoderListener".toString(),
207211
definition: "${userModel.propertyName}PasswordEncoderListener(${userModel.simpleName}PasswordEncoderListener)".toString()])
208-
addBeans(beans, 'grails-app/conf/spring/resources.groovy')
212+
213+
if(new File('grails-app/conf/spring/resources.groovy').exists()) {
214+
addBeans(beans, 'grails-app/conf/spring/resources.groovy')
215+
} else {
216+
//we could be generating this in a plugin... we should look for a Plugin class
217+
File pluginClassFile = findPluginClass()
218+
if(pluginClassFile != null) {
219+
addBeansToPlugin(beans, pluginClassFile)
220+
}
221+
}
209222

210223

211224
generateFile('Authority', roleModel.packagePath, roleModel.simpleName)
@@ -267,6 +280,79 @@ Example: ./grailsw s2-quickstart --uiOnly
267280
}
268281
}
269282

283+
private void updateYmlConfig(String userClassName, String roleClassName, String requestmapClassName, String packageName, boolean useRoleGroups) {
284+
//I mean, we could use SNAKE YAML TO GENERATE THIS BUT I DIGRESS
285+
file('grails-app/conf/application.yml').withWriterAppend { BufferedWriter writer ->
286+
writer.newLine()
287+
writer.writeLine('---')
288+
writer.writeLine('# Added by the Spring Security Core plugin:')
289+
writer.writeLine('grails:')
290+
writer.writeLine(' plugin:')
291+
writer.writeLine(' springsecurity:')
292+
if (!uiOnly) {
293+
writer.writeLine(' userLookup:')
294+
writer.writeLine(" userDomainClassName: '${packageName}.$userClassName'")
295+
writer.writeLine(" authorityJoinClassName: '${packageName}.$userClassName$roleClassName'")
296+
}
297+
if(!uiOnly || useRoleGroups) {
298+
writer.writeLine(' authority:')
299+
if(!uiOnly) {
300+
writer.writeLine(" className: '${packageName}.$roleClassName'")
301+
}
302+
if(useRoleGroups) {
303+
writer.writeLine(" groupAuthorityNameField: authorities")
304+
}
305+
}
306+
307+
if (useRoleGroups) {
308+
writer.writeLine(' useRoleGroups: true')
309+
}
310+
if (requestmapClassName) {
311+
writer.writeLine(" requestMap.className: '${packageName}.$requestmapClassName'")
312+
writer.writeLine(" securityConfigType: Requestmap")
313+
}
314+
writer.writeLine(' controllerAnnotations:')
315+
writer.writeLine(' staticRules:')
316+
writer.writeLine(' - pattern: /')
317+
writer.writeLine(' access: [\'permitAll\']')
318+
writer.writeLine(' - pattern: /error')
319+
writer.writeLine(' access: [\'permitAll\']')
320+
writer.writeLine(' - pattern: /index')
321+
writer.writeLine(' access: [\'permitAll\']')
322+
writer.writeLine(' - pattern: /index.gsp')
323+
writer.writeLine(' access: [\'permitAll\']')
324+
writer.writeLine(' - pattern: /shutdown')
325+
writer.writeLine(' access: [\'permitAll\']')
326+
writer.writeLine(' - pattern: /assets/**')
327+
writer.writeLine(' access: [\'permitAll\']')
328+
writer.writeLine(' - pattern: /**/js/**')
329+
writer.writeLine(' access: [\'permitAll\']')
330+
writer.writeLine(' - pattern: /**/css/**')
331+
writer.writeLine(' access: [\'permitAll\']')
332+
writer.writeLine(' - pattern: /**/images/**')
333+
writer.writeLine(' access: [\'permitAll\']')
334+
writer.writeLine(' - pattern: /**/favicon.ico')
335+
writer.writeLine(' access: [\'permitAll\']')
336+
writer.newLine()
337+
338+
writer.writeLine(' filterChain:')
339+
writer.writeLine(' chainMap:')
340+
writer.writeLine(' - pattern: /assets/**')
341+
writer.writeLine(' filters: none')
342+
writer.writeLine(' - pattern: /**/js/**')
343+
writer.writeLine(' filters: none')
344+
writer.writeLine(' - pattern: /**/css/**')
345+
writer.writeLine(' filters: none')
346+
writer.writeLine(' - pattern: /**/images/**')
347+
writer.writeLine(' filters: none')
348+
writer.writeLine(' - pattern: /**/favicon.ico')
349+
writer.writeLine(' filters: none')
350+
writer.writeLine(' - pattern: /**')
351+
writer.writeLine(' filters: JOINED_FILTERS')
352+
writer.newLine()
353+
}
354+
}
355+
270356
private void generateFile(String templateName, String packagePath, String className, String fileName = null, String folder = 'grails-app/domain') {
271357
render template(templateName + '.groovy.template'),
272358
file("${folder}/$packagePath/${fileName ?: className}.groovy"),
@@ -301,5 +387,39 @@ Example: ./grailsw s2-quickstart --uiOnly
301387
}
302388
}
303389

390+
private void addBeansToPlugin(List<Map<String, String>> beans, File pluginClassFile) {
391+
List<String> lines = []
392+
if (pluginClassFile.exists()) {
393+
pluginClassFile.eachLine { line, nb ->
394+
lines << line
395+
if(line.trim().startsWith('package ')) {
396+
beans.forEach(bean -> lines.add(bean.import))
397+
}
398+
if (line.contains('doWithSpring()')) {
399+
beans.each { Map bean ->
400+
lines << ' ' + bean.definition
401+
}
402+
}
403+
}
404+
}
405+
406+
pluginClassFile.withWriter('UTF-8') { writer ->
407+
lines.each { String line ->
408+
writer.write "${line}${System.lineSeparator()}"
409+
}
410+
}
411+
}
412+
413+
414+
private File findPluginClass() {
415+
File pluginClass = null
416+
new File("src/main/groovy").eachFileRecurse { fl ->
417+
if (fl.isFile() && fl.name.endsWith("GrailsPlugin.groovy")) {
418+
pluginClass = fl
419+
}
420+
}
421+
return pluginClass
422+
}
423+
304424
}
305425

0 commit comments

Comments
 (0)