| [62] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured | 
|---|
| [58] | 2 |  | 
|---|
| [149] | 3 | @Secured(['ROLE_Manager','ROLE_AppAdmin']) | 
|---|
| [97] | 4 | class PersonController extends BaseAppAdminController { | 
|---|
| [58] | 5 |  | 
|---|
| [147] | 6 | def authenticateService | 
|---|
|  | 7 | def filterService | 
|---|
| [58] | 8 |  | 
|---|
| [150] | 9 | // the delete, save and update actions only accept POST requests | 
|---|
|  | 10 | static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST'] | 
|---|
| [58] | 11 |  | 
|---|
| [150] | 12 | def index = { | 
|---|
|  | 13 | redirect action: list, params: params | 
|---|
|  | 14 | } | 
|---|
| [58] | 15 |  | 
|---|
| [147] | 16 | def list = { | 
|---|
|  | 17 | params.max = Math.min( params.max ? params.max.toInteger() : 10,  100 ) | 
|---|
| [58] | 18 |  | 
|---|
| [250] | 19 | if(!params.filter) { | 
|---|
|  | 20 | return [personList: Person.list(params), | 
|---|
|  | 21 | personTotal: Person.count(), | 
|---|
|  | 22 | filterParams: params] | 
|---|
|  | 23 | } | 
|---|
| [147] | 24 |  | 
|---|
|  | 25 | // filterPane: | 
|---|
|  | 26 | return[ personList: filterService.filter( params, Person ), | 
|---|
|  | 27 | personTotal: filterService.count( params, Person ), | 
|---|
|  | 28 | filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params), | 
|---|
|  | 29 | params:params ] | 
|---|
|  | 30 | } | 
|---|
|  | 31 |  | 
|---|
| [150] | 32 | def show = { | 
|---|
| [147] | 33 |  | 
|---|
|  | 34 | // In the case of an actionSubmit button, rewrite action name from 'index'. | 
|---|
|  | 35 | if(params._action_Show) | 
|---|
|  | 36 | { params.action='show' } | 
|---|
|  | 37 |  | 
|---|
| [150] | 38 | def person = Person.get(params.id) | 
|---|
|  | 39 | if (!person) { | 
|---|
|  | 40 | flash.message = "Person not found with id $params.id" | 
|---|
|  | 41 | redirect action: list | 
|---|
|  | 42 | return | 
|---|
|  | 43 | } | 
|---|
|  | 44 | List roleNames = [] | 
|---|
|  | 45 | for (role in person.authorities) { | 
|---|
|  | 46 | roleNames << role.authority | 
|---|
|  | 47 | } | 
|---|
|  | 48 | roleNames.sort { n1, n2 -> | 
|---|
|  | 49 | n1 <=> n2 | 
|---|
|  | 50 | } | 
|---|
|  | 51 | [person: person, roleNames: roleNames] | 
|---|
|  | 52 | } | 
|---|
| [58] | 53 |  | 
|---|
| [150] | 54 | /** | 
|---|
|  | 55 | * Person delete action. Before removing an existing person, | 
|---|
|  | 56 | * they should be removed from those authorities which they are involved. | 
|---|
|  | 57 | */ | 
|---|
|  | 58 | def delete = { | 
|---|
| [58] | 59 |  | 
|---|
| [150] | 60 | def person = Person.get(params.id) | 
|---|
|  | 61 | if (person) { | 
|---|
|  | 62 | def authPrincipal = authenticateService.principal() | 
|---|
|  | 63 | // Avoid self-delete. | 
|---|
|  | 64 | if (!(authPrincipal instanceof String) && authPrincipal.username == person.loginName) { | 
|---|
|  | 65 | flash.message = "You cannot delete yourself, please login as another manager and try again." | 
|---|
| [147] | 66 | redirect(action:show,id:params.id) | 
|---|
| [150] | 67 | } | 
|---|
|  | 68 | else { | 
|---|
|  | 69 | //first, delete this person from Persons_Authorities table. | 
|---|
|  | 70 | Authority.findAll().each { it.removeFromPersons(person) } | 
|---|
| [147] | 71 | person.isActive = false | 
|---|
|  | 72 | person.save(flush: true) | 
|---|
|  | 73 |  | 
|---|
| [97] | 74 | try { | 
|---|
| [147] | 75 | person.delete(flush: true) | 
|---|
| [91] | 76 | flash.message = "Person $params.id deleted." | 
|---|
| [97] | 77 | redirect(action:list) | 
|---|
|  | 78 | } | 
|---|
|  | 79 | catch(org.springframework.dao.DataIntegrityViolationException e) { | 
|---|
|  | 80 | flash.message = "Could not delete '$person.loginName' due to database constraints, but all authorities have been removed." | 
|---|
|  | 81 | redirect(action:show,id:params.id) | 
|---|
|  | 82 | } | 
|---|
| [150] | 83 | } | 
|---|
|  | 84 | } | 
|---|
|  | 85 | else { | 
|---|
|  | 86 | flash.message = "Person not found with id $params.id" | 
|---|
|  | 87 | } | 
|---|
|  | 88 | } | 
|---|
| [58] | 89 |  | 
|---|
| [150] | 90 | def edit = { | 
|---|
| [58] | 91 |  | 
|---|
| [147] | 92 | // In the case of an actionSubmit button, rewrite action name from 'index'. | 
|---|
|  | 93 | if(params._action_Edit) | 
|---|
|  | 94 | { params.action='edit' } | 
|---|
|  | 95 |  | 
|---|
| [150] | 96 | def person = Person.get(params.id) | 
|---|
|  | 97 | if (!person) { | 
|---|
|  | 98 | flash.message = "Person not found with id $params.id" | 
|---|
|  | 99 | redirect action: list | 
|---|
|  | 100 | return | 
|---|
|  | 101 | } | 
|---|
|  | 102 | params.message = "To allow login at least the 'ROLE_AppUser' authority must be given." | 
|---|
|  | 103 | return buildPersonModel(person) | 
|---|
|  | 104 | } | 
|---|
| [58] | 105 |  | 
|---|
| [150] | 106 | /** | 
|---|
|  | 107 | * Person update action. | 
|---|
|  | 108 | */ | 
|---|
|  | 109 | def update = { | 
|---|
| [58] | 110 |  | 
|---|
| [150] | 111 | def person = Person.get(params.id) | 
|---|
|  | 112 | if (!person) { | 
|---|
|  | 113 | flash.message = "Person not found with id $params.id" | 
|---|
|  | 114 | redirect action: edit, id: params.id | 
|---|
|  | 115 | return | 
|---|
|  | 116 | } | 
|---|
| [58] | 117 |  | 
|---|
| [150] | 118 | long version = params.version.toLong() | 
|---|
|  | 119 | if (person.version > version) { | 
|---|
|  | 120 | person.errors.rejectValue 'version', "person.optimistic.locking.failure", | 
|---|
|  | 121 | "Another user has updated this Person while you were editing." | 
|---|
| [97] | 122 | render view: 'edit', model: buildPersonModel(person) | 
|---|
| [150] | 123 | return | 
|---|
|  | 124 | } | 
|---|
| [58] | 125 |  | 
|---|
| [150] | 126 | person.properties = params | 
|---|
| [73] | 127 |  | 
|---|
| [97] | 128 | if(params.pass == "") { | 
|---|
|  | 129 | person.pass = "InsertNothingToClearValidation" | 
|---|
|  | 130 | } | 
|---|
|  | 131 | else { | 
|---|
|  | 132 | if (person.validate()) { | 
|---|
| [73] | 133 | person.password = authenticateService.encodePassword(params.pass) | 
|---|
|  | 134 | } | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [178] | 137 | if (!person.hasErrors() && person.save(flush: true)) { | 
|---|
| [73] | 138 | Authority.findAll().each { it.removeFromPersons(person) } | 
|---|
|  | 139 | addRoles(person) | 
|---|
| [97] | 140 | flash.message = "Person '$params.id - $params.loginName' updated." | 
|---|
| [73] | 141 | redirect action: show, id: person.id | 
|---|
|  | 142 | } | 
|---|
|  | 143 | else { | 
|---|
|  | 144 | render view: 'edit', model: buildPersonModel(person) | 
|---|
|  | 145 | } | 
|---|
|  | 146 |  | 
|---|
| [150] | 147 | } | 
|---|
| [58] | 148 |  | 
|---|
| [150] | 149 | def create = { | 
|---|
|  | 150 | params.message = "To allow login at least the 'ROLE_AppUser' authority must be given." | 
|---|
|  | 151 | [person: new Person(params), authorityList: Authority.list()] | 
|---|
|  | 152 | } | 
|---|
| [58] | 153 |  | 
|---|
| [150] | 154 | /** | 
|---|
|  | 155 | * Person save action. | 
|---|
|  | 156 | */ | 
|---|
|  | 157 | def save = { | 
|---|
| [58] | 158 |  | 
|---|
| [150] | 159 | def person = new Person() | 
|---|
|  | 160 | person.properties = params | 
|---|
|  | 161 | person.password = authenticateService.encodePassword(params.pass) | 
|---|
| [178] | 162 | if (person.save(flush: true)) { | 
|---|
| [150] | 163 | addRoles(person) | 
|---|
|  | 164 | redirect action: show, id: person.id | 
|---|
|  | 165 | } | 
|---|
|  | 166 | else { | 
|---|
|  | 167 | render view: 'create', model: [authorityList: Authority.list(), person: person] | 
|---|
|  | 168 | } | 
|---|
|  | 169 | } | 
|---|
| [58] | 170 |  | 
|---|
| [150] | 171 | private void addRoles(person) { | 
|---|
|  | 172 | for (String key in params.keySet()) { | 
|---|
|  | 173 | if (key.contains('ROLE') && 'on' == params.get(key)) { | 
|---|
|  | 174 | Authority.findByAuthority(key).addToPersons(person) | 
|---|
|  | 175 | } | 
|---|
|  | 176 | } | 
|---|
|  | 177 | } | 
|---|
| [58] | 178 |  | 
|---|
| [150] | 179 | private Map buildPersonModel(person) { | 
|---|
| [58] | 180 |  | 
|---|
| [150] | 181 | List roles = Authority.list() | 
|---|
|  | 182 | roles.sort { r1, r2 -> | 
|---|
|  | 183 | r1.authority <=> r2.authority | 
|---|
|  | 184 | } | 
|---|
|  | 185 | Set userRoleNames = [] | 
|---|
|  | 186 | for (role in person.authorities) { | 
|---|
|  | 187 | userRoleNames << role.authority | 
|---|
|  | 188 | } | 
|---|
|  | 189 | LinkedHashMap<Authority, Boolean> roleMap = [:] | 
|---|
|  | 190 | for (role in roles) { | 
|---|
|  | 191 | roleMap[(role)] = userRoleNames.contains(role.authority) | 
|---|
|  | 192 | } | 
|---|
| [58] | 193 |  | 
|---|
| [150] | 194 | return [person: person, roleMap: roleMap] | 
|---|
|  | 195 | } | 
|---|
| [58] | 196 | } | 
|---|