[59] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
[358] | 2 | import org.codehaus.groovy.grails.commons.* |
---|
[521] | 3 | import org.apache.commons.lang.WordUtils |
---|
[59] | 4 | |
---|
[237] | 5 | /** |
---|
| 6 | * Controller class for the application core views. |
---|
| 7 | */ |
---|
[59] | 8 | class AppCoreController extends BaseController { |
---|
| 9 | |
---|
[291] | 10 | def authService |
---|
[258] | 11 | def appConfigService |
---|
[149] | 12 | def createDataService |
---|
[258] | 13 | def createBulkDataService |
---|
[71] | 14 | |
---|
[139] | 15 | def index = { redirect(action:start,params:params) } |
---|
[59] | 16 | |
---|
| 17 | // the delete, save and update actions only accept POST requests |
---|
| 18 | //def allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
| 19 | |
---|
[139] | 20 | /** |
---|
| 21 | * This is where we arrive after login. |
---|
| 22 | * Attach the welcome flash message and redirect to where ever we want the user to start. |
---|
| 23 | * e.g. redirect(controller:"taskDetailed", action:"search") |
---|
| 24 | */ |
---|
[127] | 25 | def welcome = { |
---|
[291] | 26 | def personInstance = authService.currentUser |
---|
[127] | 27 | flash.message = "Welcome, ${personInstance.firstName} ${personInstance.lastName}." |
---|
| 28 | |
---|
| 29 | def sess = getSession() |
---|
| 30 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
---|
[139] | 31 | redirect(action:start) |
---|
[127] | 32 | } |
---|
| 33 | |
---|
[237] | 34 | /** |
---|
| 35 | * Render the start view. |
---|
| 36 | */ |
---|
[139] | 37 | def start = { |
---|
[521] | 38 | def grailsVersion = grailsApplication.metadata['app.grails.version'] |
---|
| 39 | def applicationVersion = grailsApplication.metadata['app.version'] |
---|
| 40 | def applicationName = grailsApplication.metadata['app.name'] |
---|
| 41 | def applicationVcsRevision = grailsApplication.metadata['app.vcsRevision'] |
---|
| 42 | |
---|
| 43 | // Build the application string. |
---|
| 44 | def applicationString = WordUtils.capitalize(applicationName) |
---|
| 45 | if(applicationVersion) |
---|
| 46 | applicationString += "-" + applicationVersion |
---|
| 47 | if(applicationVcsRevision) { |
---|
| 48 | if(applicationVcsRevision.size() > 7) { // Svn's $Rev: NUM $ |
---|
| 49 | applicationVcsRevision = applicationVcsRevision[6..-3] |
---|
[531] | 50 | applicationString += " (rev " + applicationVcsRevision + ")" |
---|
[521] | 51 | } |
---|
| 52 | else |
---|
| 53 | applicationString += " (" + applicationVcsRevision + ")" |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | // Build the plugins string. |
---|
| 57 | def pluginProperties = grailsApplication.metadata.findAll {it.key.contains('plugin')} |
---|
| 58 | pluginProperties.each() { |
---|
| 59 | it.key = WordUtils.capitalize( (it.key + GString.EMPTY).split("\\.")[-1] ) |
---|
| 60 | } |
---|
| 61 | pluginProperties = pluginProperties.sort { p1, p2 -> p1.key.compareToIgnoreCase(p2.key) } |
---|
| 62 | def plugins = pluginProperties.collect{ it.key + '-' + it.value }.join(", ") |
---|
| 63 | |
---|
| 64 | return [grailsVersion: grailsVersion, |
---|
| 65 | applicationString: applicationString, |
---|
| 66 | plugins: plugins] |
---|
[59] | 67 | } |
---|
| 68 | |
---|
[237] | 69 | /** |
---|
| 70 | * Allow a person to change their session timeout setting. |
---|
| 71 | */ |
---|
[127] | 72 | def changeSessionTimeout = { |
---|
| 73 | if (request.method == 'GET') { |
---|
[291] | 74 | def personInstance = authService.currentUser |
---|
[127] | 75 | return [ personInstance : personInstance ] |
---|
| 76 | } |
---|
| 77 | if (request.method == 'POST') { |
---|
[291] | 78 | def personInstance = authService.currentUser |
---|
[127] | 79 | personInstance.properties = params |
---|
[178] | 80 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
---|
[127] | 81 | def sess = getSession() |
---|
| 82 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
---|
| 83 | flash.message = "Session timeout changed." |
---|
[139] | 84 | redirect(action:start) |
---|
[127] | 85 | } |
---|
| 86 | else { |
---|
| 87 | render(view:'changeSessionTimeout',model:[personInstance:personInstance]) |
---|
| 88 | } |
---|
| 89 | } |
---|
[149] | 90 | } |
---|
[127] | 91 | |
---|
[237] | 92 | /** |
---|
| 93 | * Allow a person to change their password. |
---|
| 94 | */ |
---|
[73] | 95 | def changePassword = { |
---|
| 96 | //def principal = authenticateService.principal() |
---|
[307] | 97 | //log.info principal.getAuthorities() |
---|
[73] | 98 | |
---|
| 99 | if (request.method == 'GET') { |
---|
[291] | 100 | def personInstance = authService.currentUser |
---|
[73] | 101 | return [ personInstance : personInstance ] |
---|
[150] | 102 | } |
---|
[73] | 103 | |
---|
| 104 | if (request.method == 'POST') { |
---|
[291] | 105 | def personInstance = authService.currentUser |
---|
[73] | 106 | |
---|
[99] | 107 | if(params.confirmPass == params.pass) { |
---|
[98] | 108 | personInstance.pass = params.pass |
---|
[310] | 109 | personInstance.password = authService.encodePassword(personInstance.pass) |
---|
[98] | 110 | |
---|
[178] | 111 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
---|
[98] | 112 | //userCache.removeUserFromCache(personInstance.loginName) |
---|
| 113 | flash.message = "Password changed successfully." |
---|
[139] | 114 | redirect(action:start) |
---|
[98] | 115 | } |
---|
| 116 | else { |
---|
| 117 | render(view:'changePassword',model:[personInstance:personInstance]) |
---|
| 118 | } |
---|
[73] | 119 | } |
---|
| 120 | else { |
---|
[99] | 121 | personInstance.errors.reject('person.pass.doesNotMatch', // Error code, see grails-app/i18n/message.properties |
---|
| 122 | ['pass', 'class Person'].toArray(), // Groovy ListArray cast to Object[] |
---|
| 123 | '[NothingUseMessageProperites]') // Default mapping string. |
---|
[73] | 124 | render(view:'changePassword',model:[personInstance:personInstance]) |
---|
[98] | 125 | } |
---|
[149] | 126 | |
---|
| 127 | } |
---|
[73] | 128 | } |
---|
| 129 | |
---|
[237] | 130 | /** |
---|
| 131 | * Render the manager view for manager or admin roles. |
---|
| 132 | */ |
---|
[298] | 133 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager']) |
---|
[91] | 134 | def manager = { |
---|
| 135 | } |
---|
[73] | 136 | |
---|
[237] | 137 | /** |
---|
| 138 | * Render the appAdmin view for admin roles. |
---|
| 139 | */ |
---|
[149] | 140 | @Secured(['ROLE_AppAdmin']) |
---|
[106] | 141 | def appAdmin = { |
---|
[237] | 142 | |
---|
| 143 | def offerBaseDataCreation = false |
---|
| 144 | def offerDemoDataCreation = false |
---|
| 145 | def baseDataCreated = appConfigService.exists("baseDataCreated") |
---|
| 146 | def demoDataCreated = appConfigService.exists("demoDataCreated") |
---|
| 147 | def demoDataCreationDisabled = appConfigService.exists("demoDataCreationDisabled") |
---|
| 148 | |
---|
| 149 | if(!baseDataCreated) |
---|
| 150 | offerBaseDataCreation = true |
---|
| 151 | |
---|
| 152 | if(baseDataCreated && !demoDataCreated && !demoDataCreationDisabled) |
---|
| 153 | offerDemoDataCreation = true |
---|
| 154 | |
---|
| 155 | return[baseDataCreated: baseDataCreated, |
---|
| 156 | demoDataCreated: demoDataCreated, |
---|
| 157 | offerDemoDataCreation: offerDemoDataCreation, |
---|
| 158 | offerBaseDataCreation: offerBaseDataCreation, |
---|
| 159 | demoDataCreationDisabled: demoDataCreationDisabled] |
---|
[59] | 160 | } |
---|
| 161 | |
---|
[237] | 162 | /** |
---|
| 163 | * Allow admin to disable demo data creation. |
---|
| 164 | */ |
---|
[149] | 165 | @Secured(['ROLE_AppAdmin']) |
---|
[237] | 166 | def disableDemoDataCreation = { |
---|
| 167 | if(!appConfigService.set("demoDataCreationDisabled")) { |
---|
| 168 | flash.message = "Demo data creation could not be disabled." |
---|
| 169 | redirect(action: appAdmin) |
---|
| 170 | return |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | // Success. |
---|
| 174 | flash.message = "Demo data creation disabled." |
---|
| 175 | redirect(action: appAdmin) |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * Allow admin to create base data. |
---|
| 180 | */ |
---|
| 181 | @Secured(['ROLE_AppAdmin']) |
---|
[149] | 182 | def createBaseData = { |
---|
[237] | 183 | if(!createDataService.createBaseData()) { |
---|
| 184 | flash.message = "Base data could not be created." |
---|
| 185 | redirect(action: appAdmin) |
---|
| 186 | return |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | // Success. |
---|
| 190 | flash.message = "Base data created." |
---|
| 191 | redirect(action: appAdmin) |
---|
[149] | 192 | } |
---|
| 193 | |
---|
[237] | 194 | /** |
---|
| 195 | * Allow admin to create demo data. |
---|
| 196 | */ |
---|
[149] | 197 | @Secured(['ROLE_AppAdmin']) |
---|
| 198 | def createDemoData = { |
---|
[237] | 199 | if(!createDataService.createDemoData()) { |
---|
| 200 | flash.message = "Demo data could not be created." |
---|
| 201 | redirect(action: appAdmin) |
---|
| 202 | return |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | // Success. |
---|
| 206 | flash.message = "Demo data created." |
---|
| 207 | redirect(action: appAdmin) |
---|
[149] | 208 | } |
---|
| 209 | |
---|
[258] | 210 | /** |
---|
| 211 | * Allow admin to create bulk test data. |
---|
| 212 | */ |
---|
| 213 | @Secured(['ROLE_AppAdmin']) |
---|
| 214 | def createBulkTestData = { |
---|
[548] | 215 | def result = createBulkDataService.createAll() |
---|
| 216 | if(!result.error) { |
---|
| 217 | flash.message = g.message(code:"default.create.success", args:["Bulk test data", '']) |
---|
[258] | 218 | redirect(action: appAdmin) |
---|
| 219 | return |
---|
| 220 | } |
---|
| 221 | |
---|
[548] | 222 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[258] | 223 | redirect(action: appAdmin) |
---|
| 224 | } |
---|
| 225 | |
---|
[358] | 226 | /** |
---|
[548] | 227 | * Allow admin to create bulk inventory test data. |
---|
| 228 | */ |
---|
| 229 | @Secured(['ROLE_AppAdmin']) |
---|
| 230 | def createBulkInventoryTestData = { |
---|
| 231 | def result = createBulkDataService.createBulkInventoryTestData() |
---|
| 232 | if(!result.error) { |
---|
| 233 | flash.message = g.message(code:"default.create.success", args:["Bulk test data", '']) |
---|
| 234 | redirect(action: appAdmin) |
---|
| 235 | return |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 239 | redirect(action: appAdmin) |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | /** |
---|
[358] | 243 | * Render the application log file. |
---|
| 244 | */ |
---|
[439] | 245 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager']) |
---|
[358] | 246 | def appLog = { |
---|
| 247 | def file = new File(ConfigurationHolder.config.log4j.appenders.appLog.file) |
---|
| 248 | |
---|
| 249 | // Success. |
---|
| 250 | [log: file.text] |
---|
| 251 | } |
---|
| 252 | |
---|
[237] | 253 | } // end of class. |
---|