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