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