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