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