[59] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
| 2 | |
---|
[237] | 3 | /** |
---|
| 4 | * Controller class for the application core views. |
---|
| 5 | */ |
---|
[59] | 6 | class 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 | /** |
---|
| 39 | * Allow a person to change their session timeout setting. |
---|
| 40 | */ |
---|
[127] | 41 | def changeSessionTimeout = { |
---|
| 42 | if (request.method == 'GET') { |
---|
[291] | 43 | def personInstance = authService.currentUser |
---|
[127] | 44 | return [ personInstance : personInstance ] |
---|
| 45 | } |
---|
| 46 | if (request.method == 'POST') { |
---|
[291] | 47 | def personInstance = authService.currentUser |
---|
[127] | 48 | personInstance.properties = params |
---|
[178] | 49 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
---|
[127] | 50 | def sess = getSession() |
---|
| 51 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
---|
| 52 | flash.message = "Session timeout changed." |
---|
[139] | 53 | redirect(action:start) |
---|
[127] | 54 | } |
---|
| 55 | else { |
---|
| 56 | render(view:'changeSessionTimeout',model:[personInstance:personInstance]) |
---|
| 57 | } |
---|
| 58 | } |
---|
[149] | 59 | } |
---|
[127] | 60 | |
---|
[237] | 61 | /** |
---|
| 62 | * Allow a person to change their password. |
---|
| 63 | */ |
---|
[73] | 64 | def changePassword = { |
---|
| 65 | //def principal = authenticateService.principal() |
---|
[307] | 66 | //log.info principal.getAuthorities() |
---|
[73] | 67 | |
---|
| 68 | if (request.method == 'GET') { |
---|
[291] | 69 | def personInstance = authService.currentUser |
---|
[73] | 70 | return [ personInstance : personInstance ] |
---|
[150] | 71 | } |
---|
[73] | 72 | |
---|
| 73 | if (request.method == 'POST') { |
---|
[291] | 74 | def personInstance = authService.currentUser |
---|
[73] | 75 | |
---|
[99] | 76 | if(params.confirmPass == params.pass) { |
---|
[98] | 77 | personInstance.pass = params.pass |
---|
[310] | 78 | personInstance.password = authService.encodePassword(personInstance.pass) |
---|
[98] | 79 | |
---|
[178] | 80 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
---|
[98] | 81 | //userCache.removeUserFromCache(personInstance.loginName) |
---|
| 82 | flash.message = "Password changed successfully." |
---|
[139] | 83 | redirect(action:start) |
---|
[98] | 84 | } |
---|
| 85 | else { |
---|
| 86 | render(view:'changePassword',model:[personInstance:personInstance]) |
---|
| 87 | } |
---|
[73] | 88 | } |
---|
| 89 | else { |
---|
[99] | 90 | personInstance.errors.reject('person.pass.doesNotMatch', // Error code, see grails-app/i18n/message.properties |
---|
| 91 | ['pass', 'class Person'].toArray(), // Groovy ListArray cast to Object[] |
---|
| 92 | '[NothingUseMessageProperites]') // Default mapping string. |
---|
[73] | 93 | render(view:'changePassword',model:[personInstance:personInstance]) |
---|
[98] | 94 | } |
---|
[149] | 95 | |
---|
| 96 | } |
---|
[73] | 97 | } |
---|
| 98 | |
---|
[237] | 99 | /** |
---|
| 100 | * Render the manager view for manager or admin roles. |
---|
| 101 | */ |
---|
[298] | 102 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager']) |
---|
[91] | 103 | def manager = { |
---|
| 104 | } |
---|
[73] | 105 | |
---|
[237] | 106 | /** |
---|
| 107 | * Render the appAdmin view for admin roles. |
---|
| 108 | */ |
---|
[149] | 109 | @Secured(['ROLE_AppAdmin']) |
---|
[106] | 110 | def appAdmin = { |
---|
[237] | 111 | |
---|
| 112 | def offerBaseDataCreation = false |
---|
| 113 | def offerDemoDataCreation = false |
---|
| 114 | def baseDataCreated = appConfigService.exists("baseDataCreated") |
---|
| 115 | def demoDataCreated = appConfigService.exists("demoDataCreated") |
---|
| 116 | def demoDataCreationDisabled = appConfigService.exists("demoDataCreationDisabled") |
---|
| 117 | |
---|
| 118 | if(!baseDataCreated) |
---|
| 119 | offerBaseDataCreation = true |
---|
| 120 | |
---|
| 121 | if(baseDataCreated && !demoDataCreated && !demoDataCreationDisabled) |
---|
| 122 | offerDemoDataCreation = true |
---|
| 123 | |
---|
| 124 | return[baseDataCreated: baseDataCreated, |
---|
| 125 | demoDataCreated: demoDataCreated, |
---|
| 126 | offerDemoDataCreation: offerDemoDataCreation, |
---|
| 127 | offerBaseDataCreation: offerBaseDataCreation, |
---|
| 128 | demoDataCreationDisabled: demoDataCreationDisabled] |
---|
[59] | 129 | } |
---|
| 130 | |
---|
[237] | 131 | /** |
---|
| 132 | * Allow admin to disable demo data creation. |
---|
| 133 | */ |
---|
[149] | 134 | @Secured(['ROLE_AppAdmin']) |
---|
[237] | 135 | def disableDemoDataCreation = { |
---|
| 136 | if(!appConfigService.set("demoDataCreationDisabled")) { |
---|
| 137 | flash.message = "Demo data creation could not be disabled." |
---|
| 138 | redirect(action: appAdmin) |
---|
| 139 | return |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | // Success. |
---|
| 143 | flash.message = "Demo data creation disabled." |
---|
| 144 | redirect(action: appAdmin) |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /** |
---|
| 148 | * Allow admin to create base data. |
---|
| 149 | */ |
---|
| 150 | @Secured(['ROLE_AppAdmin']) |
---|
[149] | 151 | def createBaseData = { |
---|
[237] | 152 | if(!createDataService.createBaseData()) { |
---|
| 153 | flash.message = "Base data could not be created." |
---|
| 154 | redirect(action: appAdmin) |
---|
| 155 | return |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // Success. |
---|
| 159 | flash.message = "Base data created." |
---|
| 160 | redirect(action: appAdmin) |
---|
[149] | 161 | } |
---|
| 162 | |
---|
[237] | 163 | /** |
---|
| 164 | * Allow admin to create demo data. |
---|
| 165 | */ |
---|
[149] | 166 | @Secured(['ROLE_AppAdmin']) |
---|
| 167 | def createDemoData = { |
---|
[237] | 168 | if(!createDataService.createDemoData()) { |
---|
| 169 | flash.message = "Demo data could not be created." |
---|
| 170 | redirect(action: appAdmin) |
---|
| 171 | return |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | // Success. |
---|
| 175 | flash.message = "Demo data created." |
---|
| 176 | redirect(action: appAdmin) |
---|
[149] | 177 | } |
---|
| 178 | |
---|
[258] | 179 | /** |
---|
| 180 | * Allow admin to create bulk test data. |
---|
| 181 | */ |
---|
| 182 | @Secured(['ROLE_AppAdmin']) |
---|
| 183 | def createBulkTestData = { |
---|
| 184 | if(!createBulkDataService.create()) { |
---|
| 185 | flash.message = "Bulk test data could not be created." |
---|
| 186 | redirect(action: appAdmin) |
---|
| 187 | return |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | // Success. |
---|
| 191 | flash.message = "Bulk test data created." |
---|
| 192 | redirect(action: appAdmin) |
---|
| 193 | } |
---|
| 194 | |
---|
[237] | 195 | } // end of class. |
---|