[58] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | /** |
---|
| 5 | * User controller. |
---|
| 6 | */ |
---|
[59] | 7 | @Secured(['ROLE_ADMIN']) |
---|
[58] | 8 | class PersonController { |
---|
| 9 | |
---|
| 10 | def authenticateService |
---|
| 11 | |
---|
| 12 | // the delete, save and update actions only accept POST requests |
---|
| 13 | static Map allowedMethods = [delete: 'POST', save: 'POST', update: 'POST'] |
---|
| 14 | |
---|
| 15 | def index = { |
---|
| 16 | redirect action: list, params: params |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | def list = { |
---|
| 20 | if (!params.max) { |
---|
| 21 | params.max = 10 |
---|
| 22 | } |
---|
| 23 | [personList: Person.list(params)] |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | def show = { |
---|
| 27 | def person = Person.get(params.id) |
---|
| 28 | if (!person) { |
---|
| 29 | flash.message = "Person not found with id $params.id" |
---|
| 30 | redirect action: list |
---|
| 31 | return |
---|
| 32 | } |
---|
| 33 | List roleNames = [] |
---|
| 34 | for (role in person.authorities) { |
---|
| 35 | roleNames << role.authority |
---|
| 36 | } |
---|
| 37 | roleNames.sort { n1, n2 -> |
---|
| 38 | n1 <=> n2 |
---|
| 39 | } |
---|
| 40 | [person: person, roleNames: roleNames] |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Person delete action. Before removing an existing person, |
---|
| 45 | * he should be removed from those authorities which he is involved. |
---|
| 46 | */ |
---|
| 47 | def delete = { |
---|
| 48 | |
---|
| 49 | def person = Person.get(params.id) |
---|
| 50 | if (person) { |
---|
| 51 | def authPrincipal = authenticateService.principal() |
---|
| 52 | //avoid self-delete if the logged-in user is an admin |
---|
| 53 | if (!(authPrincipal instanceof String) && authPrincipal.username == person.loginName) { |
---|
| 54 | flash.message = "You can not delete yourself, please login as another admin and try again" |
---|
| 55 | } |
---|
| 56 | else { |
---|
| 57 | //first, delete this person from Persons_Authorities table. |
---|
| 58 | Authority.findAll().each { it.removeFromPersons(person) } |
---|
| 59 | person.delete() |
---|
| 60 | flash.message = "Person $params.id deleted." |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | else { |
---|
| 64 | flash.message = "Person not found with id $params.id" |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | redirect action: list |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | def edit = { |
---|
| 71 | |
---|
| 72 | def person = Person.get(params.id) |
---|
| 73 | if (!person) { |
---|
| 74 | flash.message = "Person not found with id $params.id" |
---|
| 75 | redirect action: list |
---|
| 76 | return |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | return buildPersonModel(person) |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Person update action. |
---|
| 84 | */ |
---|
| 85 | def update = { |
---|
| 86 | |
---|
| 87 | def person = Person.get(params.id) |
---|
| 88 | if (!person) { |
---|
| 89 | flash.message = "Person not found with id $params.id" |
---|
| 90 | redirect action: edit, id: params.id |
---|
| 91 | return |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | long version = params.version.toLong() |
---|
| 95 | if (person.version > version) { |
---|
| 96 | person.errors.rejectValue 'version', "person.optimistic.locking.failure", |
---|
| 97 | "Another user has updated this Person while you were editing." |
---|
| 98 | render view: 'edit', model: buildPersonModel(person) |
---|
| 99 | return |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | def oldPassword = person.password |
---|
| 103 | person.properties = params |
---|
| 104 | if (!params.password.equals(oldPassword)) { |
---|
| 105 | person.password = authenticateService.encodePassword(params.password) |
---|
| 106 | } |
---|
| 107 | if (person.save()) { |
---|
| 108 | Authority.findAll().each { it.removeFromPersons(person) } |
---|
| 109 | addRoles(person) |
---|
| 110 | redirect action: show, id: person.id |
---|
| 111 | } |
---|
| 112 | else { |
---|
| 113 | render view: 'edit', model: buildPersonModel(person) |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | def create = { |
---|
| 118 | [person: new Person(params), authorityList: Authority.list()] |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * Person save action. |
---|
| 123 | */ |
---|
| 124 | def save = { |
---|
| 125 | |
---|
| 126 | def person = new Person() |
---|
| 127 | person.properties = params |
---|
| 128 | person.password = authenticateService.encodePassword(params.password) |
---|
| 129 | if (person.save()) { |
---|
| 130 | addRoles(person) |
---|
| 131 | redirect action: show, id: person.id |
---|
| 132 | } |
---|
| 133 | else { |
---|
| 134 | render view: 'create', model: [authorityList: Authority.list(), person: person] |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | private void addRoles(person) { |
---|
| 139 | for (String key in params.keySet()) { |
---|
| 140 | if (key.contains('ROLE') && 'on' == params.get(key)) { |
---|
| 141 | Authority.findByAuthority(key).addToPersons(person) |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | private Map buildPersonModel(person) { |
---|
| 147 | |
---|
| 148 | List roles = Authority.list() |
---|
| 149 | roles.sort { r1, r2 -> |
---|
| 150 | r1.authority <=> r2.authority |
---|
| 151 | } |
---|
| 152 | Set userRoleNames = [] |
---|
| 153 | for (role in person.authorities) { |
---|
| 154 | userRoleNames << role.authority |
---|
| 155 | } |
---|
| 156 | LinkedHashMap<Authority, Boolean> roleMap = [:] |
---|
| 157 | for (role in roles) { |
---|
| 158 | roleMap[(role)] = userRoleNames.contains(role.authority) |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | return [person: person, roleMap: roleMap] |
---|
| 162 | } |
---|
| 163 | } |
---|