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

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

Install searchable plugin, configure and start inventory search.

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