|
| 1 | +/* Copyright 2006-2016 the original author or authors. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +import grails.codegen.model.Model |
| 17 | +import groovy.transform.Field |
| 18 | + |
| 19 | +@Field String usageMessage = ''' |
| 20 | + grails s2-quickstart <domain-class-package> <user-class-name> <role-class-name> [requestmap-class-name] [--groupClassName=group-class-name] |
| 21 | +or grails s2-quickstart --uiOnly |
| 22 | +
|
| 23 | +Example: grails s2-quickstart com.yourapp User Role |
| 24 | +Example: grails s2-quickstart com.yourapp User Role --groupClassName=RoleGroup |
| 25 | +Example: grails s2-quickstart com.yourapp Person Authority Requestmap |
| 26 | +Example: grails s2-quickstart --uiOnly |
| 27 | +''' |
| 28 | + |
| 29 | +@Field Map templateAttributes |
| 30 | +@Field boolean uiOnly |
| 31 | +@Field boolean salt |
| 32 | + |
| 33 | +description 'Creates domain classes and updates config settings for the Spring Security plugin', { |
| 34 | + |
| 35 | + usage usageMessage |
| 36 | + |
| 37 | + argument name: 'Domain class package', description: 'The package to use for the domain classes', required: false |
| 38 | + argument name: 'User class name', description: 'The name of the User/Person class', required: false |
| 39 | + argument name: 'Role class name', description: 'The name of the Role class', required: false |
| 40 | + argument name: 'Requestmap class name', description: 'The name of the Requestmap class', required: false |
| 41 | + |
| 42 | + flag name: 'groupClassName', description: 'If specified, role/group classes will also be generated using the flag value as the role-group name' |
| 43 | + flag name: 'uiOnly', description: 'If specified, no domain classes are created but the plugin settings are initialized (useful with LDAP, Mock, Shibboleth, etc.)' |
| 44 | +} |
| 45 | + |
| 46 | +Model userModel |
| 47 | +Model roleModel |
| 48 | +Model requestmapModel |
| 49 | +Model groupModel |
| 50 | +uiOnly = flag('uiOnly') |
| 51 | +salt = flag('salt') |
| 52 | +if (uiOnly) { |
| 53 | + addStatus '\nConfiguring Spring Security; not generating domain classes' |
| 54 | +} |
| 55 | +else { |
| 56 | + |
| 57 | + if (args.size() < 3) { |
| 58 | + error 'Usage:' + usageMessage |
| 59 | + return false |
| 60 | + } |
| 61 | + |
| 62 | + String packageName = args[0] |
| 63 | + String groupClassName = flag('groupClassName') |
| 64 | + String groupClassNameMessage = '' |
| 65 | + if (groupClassName) { |
| 66 | + groupModel = model(packageName + '.' + groupClassName) |
| 67 | + groupClassNameMessage = ", and role/group classes for '" + groupModel.simpleName + "'" |
| 68 | + } |
| 69 | + |
| 70 | + userModel = model(packageName + '.' + args[1]) |
| 71 | + roleModel = model(packageName + '.' + args[2]) |
| 72 | + |
| 73 | + String message = "Creating User class '" + userModel.simpleName + "'" |
| 74 | + if (4 == args.size()) { |
| 75 | + requestmapModel = model(packageName + '.' + args[3]) |
| 76 | + message += ", Role class '" + roleModel.simpleName + "', and Requestmap class '" + requestmapModel.simpleName + "'" + groupClassNameMessage |
| 77 | + } |
| 78 | + else { |
| 79 | + message += " and Role class '" + roleModel.simpleName + "'" + groupClassNameMessage |
| 80 | + } |
| 81 | + message += " in package '" + packageName + "'" |
| 82 | + addStatus message |
| 83 | + |
| 84 | + templateAttributes = [ |
| 85 | + packageName: userModel.packageName, |
| 86 | + userClassName: userModel.simpleName, |
| 87 | + userClassProperty: userModel.modelName, |
| 88 | + roleClassName: roleModel.simpleName, |
| 89 | + roleClassProperty: roleModel.modelName, |
| 90 | + requestmapClassName: requestmapModel?.simpleName, |
| 91 | + groupClassName: groupModel?.simpleName, |
| 92 | + groupClassProperty: groupModel?.modelName] |
| 93 | + |
| 94 | + createDomains userModel, roleModel, requestmapModel, groupModel |
| 95 | +} |
| 96 | + |
| 97 | +updateConfig userModel?.simpleName, roleModel?.simpleName, requestmapModel?.simpleName, userModel?.packageName, groupModel != null |
| 98 | + |
| 99 | +if (uiOnly) { |
| 100 | + addStatus ''' |
| 101 | +************************************************************ |
| 102 | +* Your grails-app/conf/application.groovy has been updated * |
| 103 | +* with security settings; please verify that the * |
| 104 | +* values are correct. * |
| 105 | +************************************************************ |
| 106 | +''' |
| 107 | +} |
| 108 | +else { |
| 109 | + addStatus ''' |
| 110 | +************************************************************ |
| 111 | +* Created security-related domain classes. Your * |
| 112 | +* grails-app/conf/application.groovy has been updated with * |
| 113 | +* the class names of the configured domain classes; * |
| 114 | +* please verify that the values are correct. * |
| 115 | +************************************************************ |
| 116 | +''' |
| 117 | +} |
| 118 | + |
| 119 | +private Map extractVersion(String versionString) { |
| 120 | + def arr = versionString.split('\\.') |
| 121 | + def v = [mayor: 0, minor: 0, bug: 0] |
| 122 | + try { |
| 123 | + if ( arr.size() >= 1) { |
| 124 | + v.mayor = arr[0].toInteger() |
| 125 | + } |
| 126 | + if ( arr.size() >= 2) { |
| 127 | + v.minor = arr[1].toInteger() |
| 128 | + } |
| 129 | + if ( arr.size() >= 3) { |
| 130 | + v.bug = arr[2].toInteger() |
| 131 | + } |
| 132 | + } catch ( Exception e ) { |
| 133 | + v = [mayor: 0, minor: 0, bug: 0] |
| 134 | + } |
| 135 | + v |
| 136 | +} |
| 137 | + |
| 138 | +private boolean versionAfterOrEqualsToThreshold(String threshold, String value) { |
| 139 | + if ( value == null ) { |
| 140 | + return false |
| 141 | + } |
| 142 | + if ( value.startsWith(threshold) ) { |
| 143 | + return true |
| 144 | + } |
| 145 | + |
| 146 | + def va = extractVersion(value) |
| 147 | + def vb = extractVersion(threshold) |
| 148 | + def l = [va, vb] |
| 149 | + l.sort { Map a, Map b -> |
| 150 | + def compare = a.mayor <=> b.mayor |
| 151 | + if ( compare != 0 ) { |
| 152 | + return compare |
| 153 | + } |
| 154 | + compare = a.minor <=> b.minor |
| 155 | + if ( compare != 0 ) { |
| 156 | + return compare |
| 157 | + } |
| 158 | + a.bug <=> b.bug |
| 159 | + } |
| 160 | + def sortedValue = l[0].collect { k, v -> v }.join('.') |
| 161 | + threshold.startsWith(sortedValue) |
| 162 | +} |
| 163 | + |
| 164 | +private void createDomains(Model userModel, Model roleModel, Model requestmapModel, Model groupModel) { |
| 165 | + |
| 166 | + def props = new Properties() |
| 167 | + file("gradle.properties")?.withInputStream { props.load(it) } |
| 168 | + |
| 169 | + final threshold = '6.0.10' |
| 170 | + |
| 171 | + boolean gormVersionAfterThreshold = versionAfterOrEqualsToThreshold(threshold, props.gormVersion ?: props.getProperty("gorm.version")) |
| 172 | + |
| 173 | + if ( gormVersionAfterThreshold ) { |
| 174 | + generateFile 'PersonWithoutInjection', userModel.packagePath, userModel.simpleName |
| 175 | + if ( salt ) { |
| 176 | + generateFile 'PersonPasswordEncoderListenerWithSalt', userModel.packagePath, userModel.simpleName, "${userModel.simpleName}PasswordEncoderListener", 'src/main/groovy' |
| 177 | + } else { |
| 178 | + generateFile 'PersonPasswordEncoderListener', userModel.packagePath, userModel.simpleName, "${userModel.simpleName}PasswordEncoderListener", 'src/main/groovy' |
| 179 | + } |
| 180 | + def beansList = [[import: "import ${userModel.packageName}.${userModel.simpleName}PasswordEncoderListener", definition: "${userModel.propertyName}PasswordEncoderListener(${userModel.simpleName}PasswordEncoderListener)"]] |
| 181 | + addBeans(beansList, 'grails-app/conf/spring/resources.groovy') |
| 182 | + |
| 183 | + } else { |
| 184 | + if ( salt ) { |
| 185 | + generateFile 'PersonWithSalt', userModel.packagePath, userModel.simpleName |
| 186 | + } else { |
| 187 | + generateFile 'Person', userModel.packagePath, userModel.simpleName |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + generateFile 'Authority', roleModel.packagePath, roleModel.simpleName |
| 192 | + generateFile 'PersonAuthority', roleModel.packagePath, userModel.simpleName + roleModel.simpleName |
| 193 | + |
| 194 | + if (requestmapModel) { |
| 195 | + generateFile 'Requestmap', requestmapModel.packagePath, requestmapModel.simpleName |
| 196 | + } |
| 197 | + |
| 198 | + if (groupModel) { |
| 199 | + generateFile 'AuthorityGroup', groupModel.packagePath, groupModel.simpleName |
| 200 | + generateFile 'PersonAuthorityGroup', groupModel.packagePath, userModel.simpleName + groupModel.simpleName |
| 201 | + generateFile 'AuthorityGroupAuthority', groupModel.packagePath, groupModel.simpleName + roleModel.simpleName |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +private void updateConfig(String userClassName, String roleClassName, String requestmapClassName, String packageName, boolean useRoleGroups) { |
| 206 | + |
| 207 | + file('grails-app/conf/application.groovy').withWriterAppend { BufferedWriter writer -> |
| 208 | + writer.newLine() |
| 209 | + writer.newLine() |
| 210 | + writer.writeLine '// Added by the Spring Security Core plugin:' |
| 211 | + if (!uiOnly) { |
| 212 | + writer.writeLine "grails.plugin.springsecurity.userLookup.userDomainClassName = '${packageName}.$userClassName'" |
| 213 | + writer.writeLine "grails.plugin.springsecurity.userLookup.authorityJoinClassName = '${packageName}.$userClassName$roleClassName'" |
| 214 | + writer.writeLine "grails.plugin.springsecurity.authority.className = '${packageName}.$roleClassName'" |
| 215 | + } |
| 216 | + if (useRoleGroups) { |
| 217 | + writer.writeLine "grails.plugin.springsecurity.authority.groupAuthorityNameField = 'authorities'" |
| 218 | + writer.writeLine 'grails.plugin.springsecurity.useRoleGroups = true' |
| 219 | + } |
| 220 | + if (requestmapClassName) { |
| 221 | + writer.writeLine "grails.plugin.springsecurity.requestMap.className = '${packageName}.$requestmapClassName'" |
| 222 | + writer.writeLine "grails.plugin.springsecurity.securityConfigType = 'Requestmap'" |
| 223 | + } |
| 224 | + writer.writeLine 'grails.plugin.springsecurity.controllerAnnotations.staticRules = [' |
| 225 | + writer.writeLine "\t[pattern: '/', access: ['permitAll']]," |
| 226 | + writer.writeLine "\t[pattern: '/error', access: ['permitAll']]," |
| 227 | + writer.writeLine "\t[pattern: '/index', access: ['permitAll']]," |
| 228 | + writer.writeLine "\t[pattern: '/index.gsp', access: ['permitAll']]," |
| 229 | + writer.writeLine "\t[pattern: '/shutdown', access: ['permitAll']]," |
| 230 | + writer.writeLine "\t[pattern: '/assets/**', access: ['permitAll']]," |
| 231 | + writer.writeLine "\t[pattern: '/**/js/**', access: ['permitAll']]," |
| 232 | + writer.writeLine "\t[pattern: '/**/css/**', access: ['permitAll']]," |
| 233 | + writer.writeLine "\t[pattern: '/**/images/**', access: ['permitAll']]," |
| 234 | + writer.writeLine "\t[pattern: '/**/favicon.ico', access: ['permitAll']]" |
| 235 | + writer.writeLine ']' |
| 236 | + writer.newLine() |
| 237 | + |
| 238 | + writer.writeLine 'grails.plugin.springsecurity.filterChain.chainMap = [' |
| 239 | + writer.writeLine "\t[pattern: '/assets/**', filters: 'none']," |
| 240 | + writer.writeLine "\t[pattern: '/**/js/**', filters: 'none']," |
| 241 | + writer.writeLine "\t[pattern: '/**/css/**', filters: 'none']," |
| 242 | + writer.writeLine "\t[pattern: '/**/images/**', filters: 'none']," |
| 243 | + writer.writeLine "\t[pattern: '/**/favicon.ico', filters: 'none']," |
| 244 | + writer.writeLine "\t[pattern: '/**', filters: 'JOINED_FILTERS']" |
| 245 | + writer.writeLine ']' |
| 246 | + writer.newLine() |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +private void generateFile(String templateName, String packagePath, String className, String fileName = null, String folder = 'grails-app/domain') { |
| 251 | + render template(templateName + '.groovy.template'), |
| 252 | + file("${folder}/$packagePath/${fileName ?: className}.groovy"), |
| 253 | + templateAttributes, false |
| 254 | +} |
| 255 | + |
| 256 | +private void addBeans(List<Map> beans, String pathname) { |
| 257 | + def f = new File(pathname) |
| 258 | + def lines = [] |
| 259 | + beans.each { Map bean -> |
| 260 | + lines << bean.import |
| 261 | + } |
| 262 | + if ( f.exists() ) { |
| 263 | + f.eachLine { line, nb -> |
| 264 | + lines << line |
| 265 | + if ( line.contains('beans = {') ) { |
| 266 | + beans.each { Map bean -> |
| 267 | + lines << ' ' + bean.definition |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | + } else { |
| 272 | + lines << 'beans = {' |
| 273 | + beans.each { Map bean -> |
| 274 | + lines << ' ' + bean.definition |
| 275 | + } |
| 276 | + lines << '}' |
| 277 | + } |
| 278 | + |
| 279 | + f.withWriter('UTF-8') { writer -> |
| 280 | + lines.each { String line -> |
| 281 | + writer.write "${line}${System.lineSeparator()}" |
| 282 | + } |
| 283 | + } |
| 284 | +} |
0 commit comments