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

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

Change assetDetail report to allow reporting by section and display site in report.

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