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

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

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

File size: 10.7 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 assetService
12    def appConfigService
13    def createDataService
14    def searchableService
15    def assetSubItemService
16    def createBulkDataService
17
18    def index = { redirect(action:start,params:params) }
19
20    // the delete, save and update actions only accept POST requests
21    //def allowedMethods = [delete:'POST', save:'POST', update:'POST']
22
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    */
28    def welcome = {
29        def personInstance = authService.currentUser
30        flash.message = "Welcome, ${personInstance.firstName} ${personInstance.lastName}."
31
32        def sess = getSession()
33        sess.setMaxInactiveInterval(personInstance.sessionTimeout)
34        redirect(action:start)
35    }
36
37    /**
38    * Render the start view.
39    */
40    def start = {
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]
53                applicationString += " (rev " + applicationVcsRevision + ")"
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
67        def sections = Section.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }
68
69        return [grailsVersion: grailsVersion,
70                    applicationString: applicationString,
71                    plugins: plugins,
72                    sections: sections]
73    }
74
75    /**
76    * Allow a person to change their session timeout setting.
77    */
78    def changeSessionTimeout = {
79        if (request.method == 'GET') {
80            def personInstance = authService.currentUser
81            return [ personInstance : personInstance ]       
82        }
83        if (request.method == 'POST') {
84            def personInstance = authService.currentUser
85                personInstance.properties = params
86                if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
87                    def sess = getSession()
88                    sess.setMaxInactiveInterval(personInstance.sessionTimeout)
89                    flash.message = "Session timeout changed."
90                    redirect(action:start)
91                }
92                else {
93                    render(view:'changeSessionTimeout',model:[personInstance:personInstance])
94                }
95        }
96    }
97
98    /**
99    * Allow a person to change their password.
100    */
101    def changePassword = {
102        //def principal = authenticateService.principal()
103        //log.info principal.getAuthorities()
104
105        if (request.method == 'GET') {
106            def personInstance = authService.currentUser
107            return [ personInstance : personInstance ]       
108        }
109
110        if (request.method == 'POST') {
111            def personInstance = authService.currentUser
112
113            if(params.confirmPass == params.pass) {
114                personInstance.pass = params.pass
115                personInstance.password = authService.encodePassword(personInstance.pass)
116
117                if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
118                    //userCache.removeUserFromCache(personInstance.loginName)
119                    flash.message = "Password changed successfully."
120                    redirect(action:start)
121                }
122                else {
123                    render(view:'changePassword',model:[personInstance:personInstance])
124                }
125            }
126            else {
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.
130                render(view:'changePassword',model:[personInstance:personInstance])
131            }
132
133        }
134    }
135
136    /**
137    * Render the manager view for manager or admin roles.
138    */
139    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
140                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager'])
141    def manager = {
142    }
143
144    /**
145    * Render the appAdmin view for admin roles.
146    */
147    @Secured(['ROLE_AppAdmin'])
148    def appAdmin = {
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]
167    }
168
169    /**
170    * Allow admin to disable demo data creation.
171    */
172    @Secured(['ROLE_AppAdmin'])
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'])
189    def createBaseData = {
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)
199    }
200
201    /**
202    * Allow admin to create demo data.
203    */
204    @Secured(['ROLE_AppAdmin'])
205    def createDemoData = {
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)
215    }
216
217    /**
218    * Allow admin to create bulk test data.
219    */
220    @Secured(['ROLE_AppAdmin'])
221    def createBulkTestData = {
222        def result = createBulkDataService.createAll()
223        if(!result.error) {
224            flash.message = g.message(code:"default.create.success", args:["Bulk test data", ''])
225            redirect(action: appAdmin)
226            return
227        }
228
229        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
230        redirect(action: appAdmin)
231    }
232
233    /**
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    /**
250    * Render the application log file.
251    */
252    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
253                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager'])
254    def appLog = {
255        def file = new File(ConfigurationHolder.config.log4j.appenders.appLog.file)
256
257        // Success.
258        [log: file.text]
259    }
260
261    /**
262    * Rebuild the text search index.
263    */
264    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager',
265                        'ROLE_InventoryManager', 'ROLE_AssetManager', 'ROLE_ProductionManager'])
266    def rebuildTextSearchIndex = {
267        InventoryIndexJob.triggerNow(['calledBy':'AppCoreController rebuildTextSearchIndex{}'])
268
269        flash.message = g.message(code:"appCore.rebuild.text.search.index")
270        redirect(action: manager)
271    }
272
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
305} // end of class.
Note: See TracBrowser for help on using the repository browser.