[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 | |
---|
[185] | 8 | def personService |
---|
[149] | 9 | def createDataService |
---|
[237] | 10 | def appConfigService |
---|
[71] | 11 | |
---|
[139] | 12 | def index = { redirect(action:start,params:params) } |
---|
[59] | 13 | |
---|
| 14 | // the delete, save and update actions only accept POST requests |
---|
| 15 | //def allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
| 16 | |
---|
[139] | 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 | */ |
---|
[127] | 22 | def welcome = { |
---|
[216] | 23 | def personInstance = personService.currentUser |
---|
[127] | 24 | flash.message = "Welcome, ${personInstance.firstName} ${personInstance.lastName}." |
---|
| 25 | |
---|
| 26 | def sess = getSession() |
---|
| 27 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
---|
[139] | 28 | redirect(action:start) |
---|
[127] | 29 | } |
---|
| 30 | |
---|
[237] | 31 | /** |
---|
| 32 | * Render the start view. |
---|
| 33 | */ |
---|
[139] | 34 | def start = { |
---|
[59] | 35 | } |
---|
| 36 | |
---|
[237] | 37 | /** |
---|
| 38 | * Allow a person to change their session timeout setting. |
---|
| 39 | */ |
---|
[127] | 40 | def changeSessionTimeout = { |
---|
| 41 | if (request.method == 'GET') { |
---|
[216] | 42 | def personInstance = personService.currentUser |
---|
[127] | 43 | return [ personInstance : personInstance ] |
---|
| 44 | } |
---|
| 45 | if (request.method == 'POST') { |
---|
[216] | 46 | def personInstance = personService.currentUser |
---|
[127] | 47 | personInstance.properties = params |
---|
[178] | 48 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
---|
[127] | 49 | def sess = getSession() |
---|
| 50 | sess.setMaxInactiveInterval(personInstance.sessionTimeout) |
---|
| 51 | flash.message = "Session timeout changed." |
---|
[139] | 52 | redirect(action:start) |
---|
[127] | 53 | } |
---|
| 54 | else { |
---|
| 55 | render(view:'changeSessionTimeout',model:[personInstance:personInstance]) |
---|
| 56 | } |
---|
| 57 | } |
---|
[149] | 58 | } |
---|
[127] | 59 | |
---|
[237] | 60 | /** |
---|
| 61 | * Allow a person to change their password. |
---|
| 62 | */ |
---|
[73] | 63 | def changePassword = { |
---|
| 64 | //def principal = authenticateService.principal() |
---|
| 65 | //println principal.getAuthorities() |
---|
| 66 | |
---|
| 67 | if (request.method == 'GET') { |
---|
[216] | 68 | def personInstance = personService.currentUser |
---|
[73] | 69 | return [ personInstance : personInstance ] |
---|
[150] | 70 | } |
---|
[73] | 71 | |
---|
| 72 | if (request.method == 'POST') { |
---|
[216] | 73 | def personInstance = personService.currentUser |
---|
[73] | 74 | |
---|
[99] | 75 | if(params.confirmPass == params.pass) { |
---|
[98] | 76 | personInstance.pass = params.pass |
---|
| 77 | personInstance.password = authenticateService.encodePassword(personInstance.pass) |
---|
| 78 | |
---|
[178] | 79 | if (!personInstance.hasErrors() && personInstance.save(flush: true)) { |
---|
[98] | 80 | //userCache.removeUserFromCache(personInstance.loginName) |
---|
| 81 | flash.message = "Password changed successfully." |
---|
[139] | 82 | redirect(action:start) |
---|
[98] | 83 | } |
---|
| 84 | else { |
---|
| 85 | render(view:'changePassword',model:[personInstance:personInstance]) |
---|
| 86 | } |
---|
[73] | 87 | } |
---|
| 88 | else { |
---|
[99] | 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. |
---|
[73] | 92 | render(view:'changePassword',model:[personInstance:personInstance]) |
---|
[98] | 93 | } |
---|
[149] | 94 | |
---|
| 95 | } |
---|
[73] | 96 | } |
---|
| 97 | |
---|
[237] | 98 | /** |
---|
| 99 | * Render the manager view for manager or admin roles. |
---|
| 100 | */ |
---|
[149] | 101 | @Secured(['ROLE_Manager','ROLE_AppAdmin']) |
---|
[91] | 102 | def manager = { |
---|
| 103 | } |
---|
[73] | 104 | |
---|
[237] | 105 | /** |
---|
| 106 | * Render the appAdmin view for admin roles. |
---|
| 107 | */ |
---|
[149] | 108 | @Secured(['ROLE_AppAdmin']) |
---|
[106] | 109 | def appAdmin = { |
---|
[237] | 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] |
---|
[59] | 128 | } |
---|
| 129 | |
---|
[237] | 130 | /** |
---|
| 131 | * Allow admin to disable demo data creation. |
---|
| 132 | */ |
---|
[149] | 133 | @Secured(['ROLE_AppAdmin']) |
---|
[237] | 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']) |
---|
[149] | 150 | def createBaseData = { |
---|
[237] | 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) |
---|
[149] | 160 | } |
---|
| 161 | |
---|
[237] | 162 | /** |
---|
| 163 | * Allow admin to create demo data. |
---|
| 164 | */ |
---|
[149] | 165 | @Secured(['ROLE_AppAdmin']) |
---|
| 166 | def createDemoData = { |
---|
[237] | 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) |
---|
[149] | 176 | } |
---|
| 177 | |
---|
[237] | 178 | } // end of class. |
---|