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

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

Allow all mangers to access manager view, appLog view and rebuildTextSearchIndex.

File size: 9.3 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', 'ROLE_TaskManager',
135                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager'])
136    def manager = {
137    }
138
139    /**
140    * Render the appAdmin view for admin roles.
141    */
142    @Secured(['ROLE_AppAdmin'])
143    def appAdmin = {
144
145        def offerBaseDataCreation = false
146        def offerDemoDataCreation = false
147        def baseDataCreated = appConfigService.exists("baseDataCreated")
148        def demoDataCreated = appConfigService.exists("demoDataCreated")
149        def demoDataCreationDisabled = appConfigService.exists("demoDataCreationDisabled")
150
151        if(!baseDataCreated)
152            offerBaseDataCreation = true
153
154        if(baseDataCreated && !demoDataCreated && !demoDataCreationDisabled)
155            offerDemoDataCreation = true
156
157        return[baseDataCreated: baseDataCreated,
158                        demoDataCreated: demoDataCreated,
159                        offerDemoDataCreation: offerDemoDataCreation,
160                        offerBaseDataCreation: offerBaseDataCreation,
161                        demoDataCreationDisabled: demoDataCreationDisabled]
162    }
163
164    /**
165    * Allow admin to disable demo data creation.
166    */
167    @Secured(['ROLE_AppAdmin'])
168    def disableDemoDataCreation = {
169        if(!appConfigService.set("demoDataCreationDisabled")) {
170            flash.message = "Demo data creation could not be disabled."
171            redirect(action: appAdmin)
172            return
173        }
174
175        // Success.
176        flash.message = "Demo data creation disabled."
177        redirect(action: appAdmin)
178    }
179
180    /**
181    * Allow admin to create base data.
182    */
183    @Secured(['ROLE_AppAdmin'])
184    def createBaseData = {
185        if(!createDataService.createBaseData()) {
186            flash.message = "Base data could not be created."
187            redirect(action: appAdmin)
188            return
189        }
190
191        // Success.
192        flash.message = "Base data created."
193        redirect(action: appAdmin)
194    }
195
196    /**
197    * Allow admin to create demo data.
198    */
199    @Secured(['ROLE_AppAdmin'])
200    def createDemoData = {
201        if(!createDataService.createDemoData()) {
202            flash.message = "Demo data could not be created."
203            redirect(action: appAdmin)
204            return
205        }
206
207        // Success.
208        flash.message = "Demo data created."
209        redirect(action: appAdmin)
210    }
211
212    /**
213    * Allow admin to create bulk test data.
214    */
215    @Secured(['ROLE_AppAdmin'])
216    def createBulkTestData = {
217        def result = createBulkDataService.createAll()
218        if(!result.error) {
219            flash.message = g.message(code:"default.create.success", args:["Bulk test data", ''])
220            redirect(action: appAdmin)
221            return
222        }
223
224        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
225        redirect(action: appAdmin)
226    }
227
228    /**
229    * Allow admin to create bulk inventory test data.
230    */
231    @Secured(['ROLE_AppAdmin'])
232    def createBulkInventoryTestData = {
233        def result = createBulkDataService.createBulkInventoryTestData()
234        if(!result.error) {
235            flash.message = g.message(code:"default.create.success", args:["Bulk test data", ''])
236            redirect(action: appAdmin)
237            return
238        }
239
240        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
241        redirect(action: appAdmin)
242    }
243
244    /**
245    * Render the application log file.
246    */
247    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
248                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager'])
249    def appLog = {
250        def file = new File(ConfigurationHolder.config.log4j.appenders.appLog.file)
251
252        // Success.
253        [log: file.text]
254    }
255
256    /**
257    * Rebuild the text search index.
258    */
259    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
260                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager'])
261    def rebuildTextSearchIndex = {
262        InventoryIndexJob.triggerNow(['calledBy':'AppCoreController rebuildTextSearchIndex{}'])
263
264        flash.message = g.message(code:"appCore.rebuild.text.search.index")
265        redirect(action: manager)
266    }
267
268} // end of class.
Note: See TracBrowser for help on using the repository browser.