| 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
|---|
| 2 | import org.codehaus.groovy.grails.commons.* |
|---|
| 3 | import org.apache.commons.lang.WordUtils |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * Controller class for the application core views. |
|---|
| 7 | */ |
|---|
| 8 | class AppCoreController extends BaseController { |
|---|
| 9 | |
|---|
| 10 | def authService |
|---|
| 11 | def appConfigService |
|---|
| 12 | def createDataService |
|---|
| 13 | def createBulkDataService |
|---|
| 14 | |
|---|
| 15 | def index = { redirect(action:start,params:params) } |
|---|
| 16 | |
|---|
| 17 | // the delete, save and update actions only accept POST requests |
|---|
| 18 | //def allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
|---|
| 19 | |
|---|
| 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 | */ |
|---|
| 25 | def welcome = { |
|---|
| 26 | def personInstance = authService.currentUser |
|---|
| 27 | flash.message = "Welcome, ${personInstance.firstName} ${personInstance.lastName}." |
|---|
| 28 | |
|---|
| 29 | def sess = getSession() |
|---|
| 30 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
|---|
| 31 | redirect(action:start) |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Render the start view. |
|---|
| 36 | */ |
|---|
| 37 | def start = { |
|---|
| 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] |
|---|
| 50 | applicationString += " (rev " + applicationVcsRevision + ")" |
|---|
| 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] |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | /** |
|---|
| 70 | * Allow a person to change their session timeout setting. |
|---|
| 71 | */ |
|---|
| 72 | def changeSessionTimeout = { |
|---|
| 73 | if (request.method == 'GET') { |
|---|
| 74 | def personInstance = authService.currentUser |
|---|
| 75 | return [ personInstance : personInstance ] |
|---|
| 76 | } |
|---|
| 77 | if (request.method == 'POST') { |
|---|
| 78 | def personInstance = authService.currentUser |
|---|
| 79 | personInstance.properties = params |
|---|
| 80 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
|---|
| 81 | def sess = getSession() |
|---|
| 82 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
|---|
| 83 | flash.message = "Session timeout changed." |
|---|
| 84 | redirect(action:start) |
|---|
| 85 | } |
|---|
| 86 | else { |
|---|
| 87 | render(view:'changeSessionTimeout',model:[personInstance:personInstance]) |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Allow a person to change their password. |
|---|
| 94 | */ |
|---|
| 95 | def changePassword = { |
|---|
| 96 | //def principal = authenticateService.principal() |
|---|
| 97 | //log.info principal.getAuthorities() |
|---|
| 98 | |
|---|
| 99 | if (request.method == 'GET') { |
|---|
| 100 | def personInstance = authService.currentUser |
|---|
| 101 | return [ personInstance : personInstance ] |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | if (request.method == 'POST') { |
|---|
| 105 | def personInstance = authService.currentUser |
|---|
| 106 | |
|---|
| 107 | if(params.confirmPass == params.pass) { |
|---|
| 108 | personInstance.pass = params.pass |
|---|
| 109 | personInstance.password = authService.encodePassword(personInstance.pass) |
|---|
| 110 | |
|---|
| 111 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
|---|
| 112 | //userCache.removeUserFromCache(personInstance.loginName) |
|---|
| 113 | flash.message = "Password changed successfully." |
|---|
| 114 | redirect(action:start) |
|---|
| 115 | } |
|---|
| 116 | else { |
|---|
| 117 | render(view:'changePassword',model:[personInstance:personInstance]) |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | else { |
|---|
| 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. |
|---|
| 124 | render(view:'changePassword',model:[personInstance:personInstance]) |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** |
|---|
| 131 | * Render the manager view for manager or admin roles. |
|---|
| 132 | */ |
|---|
| 133 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager']) |
|---|
| 134 | def manager = { |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /** |
|---|
| 138 | * Render the appAdmin view for admin roles. |
|---|
| 139 | */ |
|---|
| 140 | @Secured(['ROLE_AppAdmin']) |
|---|
| 141 | def appAdmin = { |
|---|
| 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] |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * Allow admin to disable demo data creation. |
|---|
| 164 | */ |
|---|
| 165 | @Secured(['ROLE_AppAdmin']) |
|---|
| 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']) |
|---|
| 182 | def createBaseData = { |
|---|
| 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) |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | /** |
|---|
| 195 | * Allow admin to create demo data. |
|---|
| 196 | */ |
|---|
| 197 | @Secured(['ROLE_AppAdmin']) |
|---|
| 198 | def createDemoData = { |
|---|
| 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) |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * Allow admin to create bulk test data. |
|---|
| 212 | */ |
|---|
| 213 | @Secured(['ROLE_AppAdmin']) |
|---|
| 214 | def createBulkTestData = { |
|---|
| 215 | def result = createBulkDataService.createAll() |
|---|
| 216 | if(!result.error) { |
|---|
| 217 | flash.message = g.message(code:"default.create.success", args:["Bulk test data", '']) |
|---|
| 218 | redirect(action: appAdmin) |
|---|
| 219 | return |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
|---|
| 223 | redirect(action: appAdmin) |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | /** |
|---|
| 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 | /** |
|---|
| 243 | * Render the application log file. |
|---|
| 244 | */ |
|---|
| 245 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager']) |
|---|
| 246 | def appLog = { |
|---|
| 247 | def file = new File(ConfigurationHolder.config.log4j.appenders.appLog.file) |
|---|
| 248 | |
|---|
| 249 | // Success. |
|---|
| 250 | [log: file.text] |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | } // end of class. |
|---|