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