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
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2import org.codehaus.groovy.grails.commons.*
3import org.apache.commons.lang.WordUtils
4
5/**
6* Controller class for the application core views.
7*/
8class AppCoreController extends BaseController {
9
10    def authService
11    def appConfigService
12    def createDataService
13    def searchableService
14    def createBulkDataService
15
16    def index = { redirect(action:start,params:params) }
17
18    // the delete, save and update actions only accept POST requests
19    //def allowedMethods = [delete:'POST', save:'POST', update:'POST']
20
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    */
26    def welcome = {
27        def personInstance = authService.currentUser
28        flash.message = "Welcome, ${personInstance.firstName} ${personInstance.lastName}."
29
30        def sess = getSession()
31        sess.setMaxInactiveInterval(personInstance.sessionTimeout)
32        redirect(action:start)
33    }
34
35    /**
36    * Render the start view.
37    */
38    def start = {
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]
51                applicationString += " (rev " + applicationVcsRevision + ")"
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]
68    }
69
70    /**
71    * Allow a person to change their session timeout setting.
72    */
73    def changeSessionTimeout = {
74        if (request.method == 'GET') {
75            def personInstance = authService.currentUser
76            return [ personInstance : personInstance ]       
77        }
78        if (request.method == 'POST') {
79            def personInstance = authService.currentUser
80                personInstance.properties = params
81                if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
82                    def sess = getSession()
83                    sess.setMaxInactiveInterval(personInstance.sessionTimeout)
84                    flash.message = "Session timeout changed."
85                    redirect(action:start)
86                }
87                else {
88                    render(view:'changeSessionTimeout',model:[personInstance:personInstance])
89                }
90        }
91    }
92
93    /**
94    * Allow a person to change their password.
95    */
96    def changePassword = {
97        //def principal = authenticateService.principal()
98        //log.info principal.getAuthorities()
99
100        if (request.method == 'GET') {
101            def personInstance = authService.currentUser
102            return [ personInstance : personInstance ]       
103        }
104
105        if (request.method == 'POST') {
106            def personInstance = authService.currentUser
107
108            if(params.confirmPass == params.pass) {
109                personInstance.pass = params.pass
110                personInstance.password = authService.encodePassword(personInstance.pass)
111
112                if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
113                    //userCache.removeUserFromCache(personInstance.loginName)
114                    flash.message = "Password changed successfully."
115                    redirect(action:start)
116                }
117                else {
118                    render(view:'changePassword',model:[personInstance:personInstance])
119                }
120            }
121            else {
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.
125                render(view:'changePassword',model:[personInstance:personInstance])
126            }
127
128        }
129    }
130
131    /**
132    * Render the manager view for manager or admin roles.
133    */
134    @Secured(['ROLE_AppAdmin', 'ROLE_Manager'])
135    def manager = {
136    }
137
138    /**
139    * Render the appAdmin view for admin roles.
140    */
141    @Secured(['ROLE_AppAdmin'])
142    def appAdmin = {
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]
161    }
162
163    /**
164    * Allow admin to disable demo data creation.
165    */
166    @Secured(['ROLE_AppAdmin'])
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'])
183    def createBaseData = {
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)
193    }
194
195    /**
196    * Allow admin to create demo data.
197    */
198    @Secured(['ROLE_AppAdmin'])
199    def createDemoData = {
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)
209    }
210
211    /**
212    * Allow admin to create bulk test data.
213    */
214    @Secured(['ROLE_AppAdmin'])
215    def createBulkTestData = {
216        def result = createBulkDataService.createAll()
217        if(!result.error) {
218            flash.message = g.message(code:"default.create.success", args:["Bulk test data", ''])
219            redirect(action: appAdmin)
220            return
221        }
222
223        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
224        redirect(action: appAdmin)
225    }
226
227    /**
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    /**
244    * Render the application log file.
245    */
246    @Secured(['ROLE_AppAdmin', 'ROLE_Manager'])
247    def appLog = {
248        def file = new File(ConfigurationHolder.config.log4j.appenders.appLog.file)
249
250        // Success.
251        [log: file.text]
252    }
253
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
267} // end of class.
Note: See TracBrowser for help on using the repository browser.