Skip to content

Commit

Permalink
Re-write plugin scripts as ApplicationCommand (#920)
Browse files Browse the repository at this point in the history
* Rewrite s2-quickstart as the ApplicationCommand

* Rewrite s2-quickstart as the ApplicationCommand

* Update S2QuickstartCommand.groovy

* Rewrite s2-create-persistent-token as ApplicationCommand

* Re-write s2-create-role-hierarchy-entry as ApplicationCommand

* Update Gradle Enterprise publishAlwaysIf condition
  • Loading branch information
puneetbehl authored Aug 24, 2023
1 parent 68e3a01 commit 2d6d5b6
Show file tree
Hide file tree
Showing 8 changed files with 494 additions and 372 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package grails.plugin.springsecurity

import grails.dev.commands.ExecutionContext
import io.micronaut.core.naming.NameUtils
import org.grails.build.parsing.CommandLine

trait CommandLineHelper {

static final boolean SUCCESS = true
static final boolean FAILURE = false

abstract ExecutionContext getExecutionContext()

boolean isFlagPresent(String name) {
final CommandLine commandLine = executionContext.commandLine
if (commandLine.hasOption(name)) {
return commandLine.optionValue(name) ? true : false
} else {
def value = commandLine?.undeclaredOptions?.get(name)
return value ? true : false
}
}

String flagValue(String name) {
final CommandLine commandLine = executionContext.commandLine
if (commandLine.hasOption(name)) {
return commandLine.optionValue(name)
} else {
def value = commandLine?.undeclaredOptions?.get(name)
return value
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2023 Puneet Behl.
*
* 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.
*/
package grails.plugin.springsecurity

import grails.build.logging.ConsoleLogger
import grails.build.logging.GrailsConsole
import grails.codegen.model.Model
import grails.dev.commands.GrailsApplicationCommand
import groovy.transform.CompileStatic

/**
* Creates a persistent token domain class for the Spring Security Core plugin.
* Usage: <code>./gradlew runCommand "-Pargs=s2-create-persistent-token [DOMAIN CLASS NAME]"</code>
*
* For example: <code>./gradlew runCommand "-Pargs=s2-create-persistent-token com.yourapp.PersistentLogin"</code>
*
* @author Puneet Behl
* @since 6.0.0
*/
@CompileStatic
class S2CreatePersistentTokenCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap {

@Delegate
ConsoleLogger consoleLogger = GrailsConsole.getInstance()

private final static USAGE_MESSAGE = '''
./gradlew runCommand "-Pargs=s2-create-persistent-token [DOMAIN CLASS NAME]"
For example: ./gradlew runCommand "-Pargs=s2-create-persistent-token com.yourapp.PersistentLogin"
'''

@Override
boolean handle() {

if (args.size() == 0) {
consoleLogger.error("Usage: " + USAGE_MESSAGE)
return FAILURE
}

final String domainClass = args[0]
final Model domainModel = model(domainClass)
consoleLogger.addStatus ("\nCreating persistent token class $domainClass")
render(template: template("PersistentLogin.groovy.template"),
destination: file("grails-app/domain/$domainModel.packagePath/${domainModel.simpleName}.groovy"),
model: domainModel,
overrite: false
)
file('grails-app/conf/application.groovy').withWriterAppend { BufferedWriter writer ->
writer.newLine()
writer.writeLine 'grails.plugin.springsecurity.rememberMe.persistent = true'
writer.writeLine "grails.plugin.springsecurity.rememberMe.persistentToken.domainClassName = '$domainClass'"
writer.newLine()
}
return SUCCESS
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2023 Puneet Behl.
*
* 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.
*/
package grails.plugin.springsecurity

import grails.build.logging.ConsoleLogger
import grails.build.logging.GrailsConsole
import grails.codegen.model.Model
import grails.dev.commands.GrailsApplicationCommand
import groovy.transform.CompileStatic

/**
* Creates a domain class for a persistent role hierarchy for the Spring Security Core plugin
* Usage: <code>./gradlew runCommand "-Pargs=s2-create-role-hierarchy-entry [DOMAIN CLASS NAME]"
* For example: <code>./gradlew runCommand "-Pargs=s2-create-role-hierarchy-entry com.yourapp.RoleHierarchyEntry"</code>
*
* @author Puneet Behl
* @since 6.0.0
*/
@CompileStatic
class S2CreateRoleHierarchyEntryCommand implements GrailsApplicationCommand, CommandLineHelper, SkipBootstrap {

private static final String USAGE_MESSAGE = '''
./gradlew runCommand "-Pargs=s2-create-role-hierarchy-entry [DOMAIN CLASS NAME]"
For example: ./gradlew runCommand "-Pargs=s2-create-role-hierarchy-entry com.yourapp.RoleHierarchyEntry"
'''

@Delegate
ConsoleLogger consoleLogger = GrailsConsole.getInstance()

@Override
boolean handle() {

if (args.size() == 0) {
consoleLogger.error("Usage: " + USAGE_MESSAGE)
return FAILURE
}

final String domainClass = args[0]
final Model domainModel = model(domainClass)

consoleLogger.addStatus("\nCreating role hierarchy entry class $domainClass")
render(template: template('RoleHierarchyEntry.groovy.template'),
destination: file("grails-app/domain/$domainModel.packagePath/${domainModel.simpleName}.groovy"),
model: domainModel, overwrite: false)

file('grails-app/conf/application.groovy').withWriterAppend { BufferedWriter writer ->
writer.newLine()
writer.writeLine "grails.plugin.springsecurity.roleHierarchyEntryClassName = '$domainClass'"
writer.newLine()
}

return SUCCESS
}
}
Loading

1 comment on commit 2d6d5b6

@tircnf
Copy link

@tircnf tircnf commented on 2d6d5b6 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puneetbehl -- the grails 6 gradle.properties file does not contain a gorm version.

Line 193 of the s2-quickstart command is looking for the gorm version to determine which version of User to use. (injectable service or not).

I don't know the appropriate way to determine gorm version now. Either the default gradle.properties needs to be fixed, or some other mechanism needs to figured out.

when I build an app and use s2-quickstart, I get the old version of User.groovy with the injected spring security service instead of the one with the Interceptor.

Please sign in to comment.