[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 | |
---|
[687] | 65 | def sections = Section.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 66 | |
---|
[521] | 67 | return [grailsVersion: grailsVersion, |
---|
| 68 | applicationString: applicationString, |
---|
[687] | 69 | plugins: plugins, |
---|
| 70 | sections: sections] |
---|
[59] | 71 | } |
---|
| 72 | |
---|
[237] | 73 | /** |
---|
| 74 | * Allow a person to change their session timeout setting. |
---|
| 75 | */ |
---|
[127] | 76 | def changeSessionTimeout = { |
---|
| 77 | if (request.method == 'GET') { |
---|
[291] | 78 | def personInstance = authService.currentUser |
---|
[127] | 79 | return [ personInstance : personInstance ] |
---|
| 80 | } |
---|
| 81 | if (request.method == 'POST') { |
---|
[291] | 82 | def personInstance = authService.currentUser |
---|
[127] | 83 | personInstance.properties = params |
---|
[178] | 84 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
---|
[127] | 85 | def sess = getSession() |
---|
| 86 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
---|
| 87 | flash.message = "Session timeout changed." |
---|
[139] | 88 | redirect(action:start) |
---|
[127] | 89 | } |
---|
| 90 | else { |
---|
| 91 | render(view:'changeSessionTimeout',model:[personInstance:personInstance]) |
---|
| 92 | } |
---|
| 93 | } |
---|
[149] | 94 | } |
---|
[127] | 95 | |
---|
[237] | 96 | /** |
---|
| 97 | * Allow a person to change their password. |
---|
| 98 | */ |
---|
[73] | 99 | def changePassword = { |
---|
| 100 | //def principal = authenticateService.principal() |
---|
[307] | 101 | //log.info principal.getAuthorities() |
---|
[73] | 102 | |
---|
| 103 | if (request.method == 'GET') { |
---|
[291] | 104 | def personInstance = authService.currentUser |
---|
[73] | 105 | return [ personInstance : personInstance ] |
---|
[150] | 106 | } |
---|
[73] | 107 | |
---|
| 108 | if (request.method == 'POST') { |
---|
[291] | 109 | def personInstance = authService.currentUser |
---|
[73] | 110 | |
---|
[99] | 111 | if(params.confirmPass == params.pass) { |
---|
[98] | 112 | personInstance.pass = params.pass |
---|
[310] | 113 | personInstance.password = authService.encodePassword(personInstance.pass) |
---|
[98] | 114 | |
---|
[178] | 115 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
---|
[98] | 116 | //userCache.removeUserFromCache(personInstance.loginName) |
---|
| 117 | flash.message = "Password changed successfully." |
---|
[139] | 118 | redirect(action:start) |
---|
[98] | 119 | } |
---|
| 120 | else { |
---|
| 121 | render(view:'changePassword',model:[personInstance:personInstance]) |
---|
| 122 | } |
---|
[73] | 123 | } |
---|
| 124 | else { |
---|
[99] | 125 | personInstance.errors.reject('person.pass.doesNotMatch', // Error code, see grails-app/i18n/message.properties |
---|
| 126 | ['pass', 'class Person'].toArray(), // Groovy ListArray cast to Object[] |
---|
| 127 | '[NothingUseMessageProperites]') // Default mapping string. |
---|
[73] | 128 | render(view:'changePassword',model:[personInstance:personInstance]) |
---|
[98] | 129 | } |
---|
[149] | 130 | |
---|
| 131 | } |
---|
[73] | 132 | } |
---|
| 133 | |
---|
[237] | 134 | /** |
---|
| 135 | * Render the manager view for manager or admin roles. |
---|
| 136 | */ |
---|
[627] | 137 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', |
---|
| 138 | 'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager']) |
---|
[91] | 139 | def manager = { |
---|
| 140 | } |
---|
[73] | 141 | |
---|
[237] | 142 | /** |
---|
| 143 | * Render the appAdmin view for admin roles. |
---|
| 144 | */ |
---|
[149] | 145 | @Secured(['ROLE_AppAdmin']) |
---|
[106] | 146 | def appAdmin = { |
---|
[237] | 147 | |
---|
| 148 | def offerBaseDataCreation = false |
---|
| 149 | def offerDemoDataCreation = false |
---|
| 150 | def baseDataCreated = appConfigService.exists("baseDataCreated") |
---|
| 151 | def demoDataCreated = appConfigService.exists("demoDataCreated") |
---|
| 152 | def demoDataCreationDisabled = appConfigService.exists("demoDataCreationDisabled") |
---|
| 153 | |
---|
| 154 | if(!baseDataCreated) |
---|
| 155 | offerBaseDataCreation = true |
---|
| 156 | |
---|
| 157 | if(baseDataCreated && !demoDataCreated && !demoDataCreationDisabled) |
---|
| 158 | offerDemoDataCreation = true |
---|
| 159 | |
---|
| 160 | return[baseDataCreated: baseDataCreated, |
---|
| 161 | demoDataCreated: demoDataCreated, |
---|
| 162 | offerDemoDataCreation: offerDemoDataCreation, |
---|
| 163 | offerBaseDataCreation: offerBaseDataCreation, |
---|
| 164 | demoDataCreationDisabled: demoDataCreationDisabled] |
---|
[59] | 165 | } |
---|
| 166 | |
---|
[237] | 167 | /** |
---|
| 168 | * Allow admin to disable demo data creation. |
---|
| 169 | */ |
---|
[149] | 170 | @Secured(['ROLE_AppAdmin']) |
---|
[237] | 171 | def disableDemoDataCreation = { |
---|
| 172 | if(!appConfigService.set("demoDataCreationDisabled")) { |
---|
| 173 | flash.message = "Demo data creation could not be disabled." |
---|
| 174 | redirect(action: appAdmin) |
---|
| 175 | return |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | // Success. |
---|
| 179 | flash.message = "Demo data creation disabled." |
---|
| 180 | redirect(action: appAdmin) |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | /** |
---|
| 184 | * Allow admin to create base data. |
---|
| 185 | */ |
---|
| 186 | @Secured(['ROLE_AppAdmin']) |
---|
[149] | 187 | def createBaseData = { |
---|
[237] | 188 | if(!createDataService.createBaseData()) { |
---|
| 189 | flash.message = "Base data could not be created." |
---|
| 190 | redirect(action: appAdmin) |
---|
| 191 | return |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | // Success. |
---|
| 195 | flash.message = "Base data created." |
---|
| 196 | redirect(action: appAdmin) |
---|
[149] | 197 | } |
---|
| 198 | |
---|
[237] | 199 | /** |
---|
| 200 | * Allow admin to create demo data. |
---|
| 201 | */ |
---|
[149] | 202 | @Secured(['ROLE_AppAdmin']) |
---|
| 203 | def createDemoData = { |
---|
[237] | 204 | if(!createDataService.createDemoData()) { |
---|
| 205 | flash.message = "Demo data could not be created." |
---|
| 206 | redirect(action: appAdmin) |
---|
| 207 | return |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | // Success. |
---|
| 211 | flash.message = "Demo data created." |
---|
| 212 | redirect(action: appAdmin) |
---|
[149] | 213 | } |
---|
| 214 | |
---|
[258] | 215 | /** |
---|
| 216 | * Allow admin to create bulk test data. |
---|
| 217 | */ |
---|
| 218 | @Secured(['ROLE_AppAdmin']) |
---|
| 219 | def createBulkTestData = { |
---|
[548] | 220 | def result = createBulkDataService.createAll() |
---|
| 221 | if(!result.error) { |
---|
| 222 | flash.message = g.message(code:"default.create.success", args:["Bulk test data", '']) |
---|
[258] | 223 | redirect(action: appAdmin) |
---|
| 224 | return |
---|
| 225 | } |
---|
| 226 | |
---|
[548] | 227 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[258] | 228 | redirect(action: appAdmin) |
---|
| 229 | } |
---|
| 230 | |
---|
[358] | 231 | /** |
---|
[548] | 232 | * Allow admin to create bulk inventory test data. |
---|
| 233 | */ |
---|
| 234 | @Secured(['ROLE_AppAdmin']) |
---|
| 235 | def createBulkInventoryTestData = { |
---|
| 236 | def result = createBulkDataService.createBulkInventoryTestData() |
---|
| 237 | if(!result.error) { |
---|
| 238 | flash.message = g.message(code:"default.create.success", args:["Bulk test data", '']) |
---|
| 239 | redirect(action: appAdmin) |
---|
| 240 | return |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 244 | redirect(action: appAdmin) |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
[358] | 248 | * Render the application log file. |
---|
| 249 | */ |
---|
[627] | 250 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', |
---|
| 251 | 'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager']) |
---|
[358] | 252 | def appLog = { |
---|
| 253 | def file = new File(ConfigurationHolder.config.log4j.appenders.appLog.file) |
---|
| 254 | |
---|
| 255 | // Success. |
---|
| 256 | [log: file.text] |
---|
| 257 | } |
---|
| 258 | |
---|
[562] | 259 | /** |
---|
[622] | 260 | * Rebuild the text search index. |
---|
[562] | 261 | */ |
---|
[627] | 262 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', |
---|
| 263 | 'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager']) |
---|
[562] | 264 | def rebuildTextSearchIndex = { |
---|
[622] | 265 | InventoryIndexJob.triggerNow(['calledBy':'AppCoreController rebuildTextSearchIndex{}']) |
---|
[562] | 266 | |
---|
[622] | 267 | flash.message = g.message(code:"appCore.rebuild.text.search.index") |
---|
[562] | 268 | redirect(action: manager) |
---|
| 269 | } |
---|
| 270 | |
---|
[237] | 271 | } // end of class. |
---|