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

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

Fix small bug in AppCore controller changePassword function.
Introduced when authenticateService was replaced with authService.

File size: 6.4 KB
RevLine 
[59]1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
[237]3/**
4* Controller class for the application core views.
5*/
[59]6class AppCoreController extends BaseController {
7
[291]8    def authService
[258]9    def appConfigService
[149]10    def createDataService
[258]11    def createBulkDataService
[71]12
[139]13    def index = { redirect(action:start,params:params) }
[59]14
15    // the delete, save and update actions only accept POST requests
16    //def allowedMethods = [delete:'POST', save:'POST', update:'POST']
17
[139]18    /**
19    * This is where we arrive after login.
20    *  Attach the welcome flash message and redirect to where ever we want the user to start.
21    * e.g. redirect(controller:"taskDetailed", action:"search")
22    */
[127]23    def welcome = {
[291]24        def personInstance = authService.currentUser
[127]25        flash.message = "Welcome, ${personInstance.firstName} ${personInstance.lastName}."
26
27        def sess = getSession()
28        sess.setMaxInactiveInterval(personInstance.sessionTimeout)
[139]29        redirect(action:start)
[127]30    }
31
[237]32    /**
33    * Render the start view.
34    */
[139]35    def start = {
[59]36    }
37
[237]38    /**
[307]39    * Save the asset Tree status in the current http session.
40    */
41    def saveAssetTreeStatus = {
42        session.assetTreeVisibleBranches = params.assetTreeVisibleBranches
43    }
44
45    /**
[237]46    * Allow a person to change their session timeout setting.
47    */
[127]48    def changeSessionTimeout = {
49        if (request.method == 'GET') {
[291]50            def personInstance = authService.currentUser
[127]51            return [ personInstance : personInstance ]       
52        }
53        if (request.method == 'POST') {
[291]54            def personInstance = authService.currentUser
[127]55                personInstance.properties = params
[178]56                if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
[127]57                    def sess = getSession()
58                    sess.setMaxInactiveInterval(personInstance.sessionTimeout)
59                    flash.message = "Session timeout changed."
[139]60                    redirect(action:start)
[127]61                }
62                else {
63                    render(view:'changeSessionTimeout',model:[personInstance:personInstance])
64                }
65        }
[149]66    }
[127]67
[237]68    /**
69    * Allow a person to change their password.
70    */
[73]71    def changePassword = {
72        //def principal = authenticateService.principal()
[307]73        //log.info principal.getAuthorities()
[73]74
75        if (request.method == 'GET') {
[291]76            def personInstance = authService.currentUser
[73]77            return [ personInstance : personInstance ]       
[150]78        }
[73]79
80        if (request.method == 'POST') {
[291]81            def personInstance = authService.currentUser
[73]82
[99]83            if(params.confirmPass == params.pass) {
[98]84                personInstance.pass = params.pass
[310]85                personInstance.password = authService.encodePassword(personInstance.pass)
[98]86
[178]87                if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
[98]88                    //userCache.removeUserFromCache(personInstance.loginName)
89                    flash.message = "Password changed successfully."
[139]90                    redirect(action:start)
[98]91                }
92                else {
93                    render(view:'changePassword',model:[personInstance:personInstance])
94                }
[73]95            }
96            else {
[99]97                personInstance.errors.reject('person.pass.doesNotMatch',            // Error code, see grails-app/i18n/message.properties
98                                                                ['pass', 'class Person'].toArray(),      // Groovy ListArray cast to Object[]
99                                                                 '[NothingUseMessageProperites]')  // Default mapping string.
[73]100                render(view:'changePassword',model:[personInstance:personInstance])
[98]101            }
[149]102
103        }
[73]104    }
105
[237]106    /**
107    * Render the manager view for manager or admin roles.
108    */
[298]109    @Secured(['ROLE_AppAdmin', 'ROLE_Manager'])
[91]110    def manager = {
111    }
[73]112
[237]113    /**
114    * Render the appAdmin view for admin roles.
115    */
[149]116    @Secured(['ROLE_AppAdmin'])
[106]117    def appAdmin = {
[237]118
119        def offerBaseDataCreation = false
120        def offerDemoDataCreation = false
121        def baseDataCreated = appConfigService.exists("baseDataCreated")
122        def demoDataCreated = appConfigService.exists("demoDataCreated")
123        def demoDataCreationDisabled = appConfigService.exists("demoDataCreationDisabled")
124
125        if(!baseDataCreated)
126            offerBaseDataCreation = true
127
128        if(baseDataCreated && !demoDataCreated && !demoDataCreationDisabled)
129            offerDemoDataCreation = true
130
131        return[baseDataCreated: baseDataCreated,
132                        demoDataCreated: demoDataCreated,
133                        offerDemoDataCreation: offerDemoDataCreation,
134                        offerBaseDataCreation: offerBaseDataCreation,
135                        demoDataCreationDisabled: demoDataCreationDisabled]
[59]136    }
137
[237]138    /**
139    * Allow admin to disable demo data creation.
140    */
[149]141    @Secured(['ROLE_AppAdmin'])
[237]142    def disableDemoDataCreation = {
143        if(!appConfigService.set("demoDataCreationDisabled")) {
144            flash.message = "Demo data creation could not be disabled."
145            redirect(action: appAdmin)
146            return
147        }
148
149        // Success.
150        flash.message = "Demo data creation disabled."
151        redirect(action: appAdmin)
152    }
153
154    /**
155    * Allow admin to create base data.
156    */
157    @Secured(['ROLE_AppAdmin'])
[149]158    def createBaseData = {
[237]159        if(!createDataService.createBaseData()) {
160            flash.message = "Base data could not be created."
161            redirect(action: appAdmin)
162            return
163        }
164
165        // Success.
166        flash.message = "Base data created."
167        redirect(action: appAdmin)
[149]168    }
169
[237]170    /**
171    * Allow admin to create demo data.
172    */
[149]173    @Secured(['ROLE_AppAdmin'])
174    def createDemoData = {
[237]175        if(!createDataService.createDemoData()) {
176            flash.message = "Demo data could not be created."
177            redirect(action: appAdmin)
178            return
179        }
180
181        // Success.
182        flash.message = "Demo data created."
183        redirect(action: appAdmin)
[149]184    }
185
[258]186    /**
187    * Allow admin to create bulk test data.
188    */
189    @Secured(['ROLE_AppAdmin'])
190    def createBulkTestData = {
191        if(!createBulkDataService.create()) {
192            flash.message = "Bulk test data could not be created."
193            redirect(action: appAdmin)
194            return
195        }
196
197        // Success.
198        flash.message = "Bulk test data created."
199        redirect(action: appAdmin)
200    }
201
[237]202} // end of class.
Note: See TracBrowser for help on using the repository browser.