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

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

Integrate create data functions with appConfig.

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