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