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