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