| [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. |
|---|
| [913] | 60 | def userPlugins = org.codehaus.groovy.grails.plugins.PluginManagerHolder.pluginManager.userPlugins |
|---|
| [521] | 61 | |
|---|
| [913] | 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 | |
|---|
| [687] | 68 | def sections = Section.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
|---|
| 69 | |
|---|
| [707] | 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 | |
|---|
| [521] | 85 | return [grailsVersion: grailsVersion, |
|---|
| 86 | applicationString: applicationString, |
|---|
| [687] | 87 | plugins: plugins, |
|---|
| [707] | 88 | sections: sections, |
|---|
| 89 | showTab: showTab] |
|---|
| [59] | 90 | } |
|---|
| 91 | |
|---|
| [237] | 92 | /** |
|---|
| 93 | * Allow a person to change their session timeout setting. |
|---|
| 94 | */ |
|---|
| [127] | 95 | def changeSessionTimeout = { |
|---|
| 96 | if (request.method == 'GET') { |
|---|
| [291] | 97 | def personInstance = authService.currentUser |
|---|
| [127] | 98 | return [ personInstance : personInstance ] |
|---|
| 99 | } |
|---|
| 100 | if (request.method == 'POST') { |
|---|
| [291] | 101 | def personInstance = authService.currentUser |
|---|
| [127] | 102 | personInstance.properties = params |
|---|
| [178] | 103 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
|---|
| [127] | 104 | def sess = getSession() |
|---|
| 105 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
|---|
| 106 | flash.message = "Session timeout changed." |
|---|
| [139] | 107 | redirect(action:start) |
|---|
| [127] | 108 | } |
|---|
| 109 | else { |
|---|
| 110 | render(view:'changeSessionTimeout',model:[personInstance:personInstance]) |
|---|
| 111 | } |
|---|
| 112 | } |
|---|
| [149] | 113 | } |
|---|
| [127] | 114 | |
|---|
| [237] | 115 | /** |
|---|
| 116 | * Allow a person to change their password. |
|---|
| 117 | */ |
|---|
| [73] | 118 | def changePassword = { |
|---|
| 119 | //def principal = authenticateService.principal() |
|---|
| [307] | 120 | //log.info principal.getAuthorities() |
|---|
| [73] | 121 | |
|---|
| 122 | if (request.method == 'GET') { |
|---|
| [291] | 123 | def personInstance = authService.currentUser |
|---|
| [73] | 124 | return [ personInstance : personInstance ] |
|---|
| [150] | 125 | } |
|---|
| [73] | 126 | |
|---|
| 127 | if (request.method == 'POST') { |
|---|
| [291] | 128 | def personInstance = authService.currentUser |
|---|
| [73] | 129 | |
|---|
| [99] | 130 | if(params.confirmPass == params.pass) { |
|---|
| [98] | 131 | personInstance.pass = params.pass |
|---|
| [310] | 132 | personInstance.password = authService.encodePassword(personInstance.pass) |
|---|
| [98] | 133 | |
|---|
| [178] | 134 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
|---|
| [98] | 135 | //userCache.removeUserFromCache(personInstance.loginName) |
|---|
| 136 | flash.message = "Password changed successfully." |
|---|
| [139] | 137 | redirect(action:start) |
|---|
| [98] | 138 | } |
|---|
| 139 | else { |
|---|
| 140 | render(view:'changePassword',model:[personInstance:personInstance]) |
|---|
| 141 | } |
|---|
| [73] | 142 | } |
|---|
| 143 | else { |
|---|
| [99] | 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. |
|---|
| [73] | 147 | render(view:'changePassword',model:[personInstance:personInstance]) |
|---|
| [98] | 148 | } |
|---|
| [149] | 149 | |
|---|
| 150 | } |
|---|
| [73] | 151 | } |
|---|
| 152 | |
|---|
| [237] | 153 | /** |
|---|
| 154 | * Render the manager view for manager or admin roles. |
|---|
| 155 | */ |
|---|
| [627] | 156 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', |
|---|
| 157 | 'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager']) |
|---|
| [91] | 158 | def manager = { |
|---|
| 159 | } |
|---|
| [73] | 160 | |
|---|
| [237] | 161 | /** |
|---|
| 162 | * Render the appAdmin view for admin roles. |
|---|
| 163 | */ |
|---|
| [149] | 164 | @Secured(['ROLE_AppAdmin']) |
|---|
| [106] | 165 | def appAdmin = { |
|---|
| [237] | 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] |
|---|
| [59] | 184 | } |
|---|
| 185 | |
|---|
| [237] | 186 | /** |
|---|
| 187 | * Allow admin to disable demo data creation. |
|---|
| 188 | */ |
|---|
| [149] | 189 | @Secured(['ROLE_AppAdmin']) |
|---|
| [237] | 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']) |
|---|
| [149] | 206 | def createBaseData = { |
|---|
| [237] | 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) |
|---|
| [149] | 216 | } |
|---|
| 217 | |
|---|
| [237] | 218 | /** |
|---|
| 219 | * Allow admin to create demo data. |
|---|
| 220 | */ |
|---|
| [149] | 221 | @Secured(['ROLE_AppAdmin']) |
|---|
| 222 | def createDemoData = { |
|---|
| [237] | 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) |
|---|
| [149] | 232 | } |
|---|
| 233 | |
|---|
| [258] | 234 | /** |
|---|
| 235 | * Allow admin to create bulk test data. |
|---|
| 236 | */ |
|---|
| 237 | @Secured(['ROLE_AppAdmin']) |
|---|
| 238 | def createBulkTestData = { |
|---|
| [548] | 239 | def result = createBulkDataService.createAll() |
|---|
| 240 | if(!result.error) { |
|---|
| 241 | flash.message = g.message(code:"default.create.success", args:["Bulk test data", '']) |
|---|
| [258] | 242 | redirect(action: appAdmin) |
|---|
| 243 | return |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| [548] | 246 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
|---|
| [258] | 247 | redirect(action: appAdmin) |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| [358] | 250 | /** |
|---|
| [548] | 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 | /** |
|---|
| [358] | 267 | * Render the application log file. |
|---|
| 268 | */ |
|---|
| [627] | 269 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', |
|---|
| 270 | 'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager']) |
|---|
| [358] | 271 | def appLog = { |
|---|
| 272 | def file = new File(ConfigurationHolder.config.log4j.appenders.appLog.file) |
|---|
| 273 | |
|---|
| 274 | // Success. |
|---|
| 275 | [log: file.text] |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| [562] | 278 | /** |
|---|
| [622] | 279 | * Rebuild the text search index. |
|---|
| [562] | 280 | */ |
|---|
| [627] | 281 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', |
|---|
| 282 | 'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager']) |
|---|
| [562] | 283 | def rebuildTextSearchIndex = { |
|---|
| [622] | 284 | InventoryIndexJob.triggerNow(['calledBy':'AppCoreController rebuildTextSearchIndex{}']) |
|---|
| [562] | 285 | |
|---|
| [622] | 286 | flash.message = g.message(code:"appCore.rebuild.text.search.index") |
|---|
| [562] | 287 | redirect(action: manager) |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| [688] | 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 | |
|---|
| [237] | 322 | } // end of class. |
|---|