source: trunk/grails-app/controllers/AppCoreController.groovy @ 688

Last change on this file since 688 was 688, checked in by gav, 14 years ago

Add admin feature to create recommended extended attributes for assets and level 1 assetSubItems.

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