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
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3/**
4* Controller class for the application core views.
5*/
6class AppCoreController extends BaseController {
7
8    def authService
9    def appConfigService
10    def createDataService
11    def createBulkDataService
12
13    def index = { redirect(action:start,params:params) }
14
15    // the delete, save and update actions only accept POST requests
16    //def allowedMethods = [delete:'POST', save:'POST', update:'POST']
17
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    */
23    def welcome = {
24        def personInstance = authService.currentUser
25        flash.message = "Welcome, ${personInstance.firstName} ${personInstance.lastName}."
26
27        def sess = getSession()
28        sess.setMaxInactiveInterval(personInstance.sessionTimeout)
29        redirect(action:start)
30    }
31
32    /**
33    * Render the start view.
34    */
35    def start = {
36    }
37
38    /**
39    * Save the asset Tree status in the current http session.
40    */
41    def saveAssetTreeStatus = {
42        session.assetTreeVisibleBranches = params.assetTreeVisibleBranches
43    }
44
45    /**
46    * Allow a person to change their session timeout setting.
47    */
48    def changeSessionTimeout = {
49        if (request.method == 'GET') {
50            def personInstance = authService.currentUser
51            return [ personInstance : personInstance ]       
52        }
53        if (request.method == 'POST') {
54            def personInstance = authService.currentUser
55                personInstance.properties = params
56                if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
57                    def sess = getSession()
58                    sess.setMaxInactiveInterval(personInstance.sessionTimeout)
59                    flash.message = "Session timeout changed."
60                    redirect(action:start)
61                }
62                else {
63                    render(view:'changeSessionTimeout',model:[personInstance:personInstance])
64                }
65        }
66    }
67
68    /**
69    * Allow a person to change their password.
70    */
71    def changePassword = {
72        //def principal = authenticateService.principal()
73        //log.info principal.getAuthorities()
74
75        if (request.method == 'GET') {
76            def personInstance = authService.currentUser
77            return [ personInstance : personInstance ]       
78        }
79
80        if (request.method == 'POST') {
81            def personInstance = authService.currentUser
82
83            if(params.confirmPass == params.pass) {
84                personInstance.pass = params.pass
85                personInstance.password = authService.encodePassword(personInstance.pass)
86
87                if (!personInstance.hasErrors() && personInstance.save(flush: true)) {
88                    //userCache.removeUserFromCache(personInstance.loginName)
89                    flash.message = "Password changed successfully."
90                    redirect(action:start)
91                }
92                else {
93                    render(view:'changePassword',model:[personInstance:personInstance])
94                }
95            }
96            else {
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.
100                render(view:'changePassword',model:[personInstance:personInstance])
101            }
102
103        }
104    }
105
106    /**
107    * Render the manager view for manager or admin roles.
108    */
109    @Secured(['ROLE_AppAdmin', 'ROLE_Manager'])
110    def manager = {
111    }
112
113    /**
114    * Render the appAdmin view for admin roles.
115    */
116    @Secured(['ROLE_AppAdmin'])
117    def appAdmin = {
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]
136    }
137
138    /**
139    * Allow admin to disable demo data creation.
140    */
141    @Secured(['ROLE_AppAdmin'])
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'])
158    def createBaseData = {
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)
168    }
169
170    /**
171    * Allow admin to create demo data.
172    */
173    @Secured(['ROLE_AppAdmin'])
174    def createDemoData = {
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)
184    }
185
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
202} // end of class.
Note: See TracBrowser for help on using the repository browser.