1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.kim.permission.mock; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Collection; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
import java.util.UUID; |
24 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
25 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
26 | |
|
27 | |
|
28 | |
import org.kuali.rice.kim.api.KimConstants; |
29 | |
import org.kuali.rice.kim.api.group.Group; |
30 | |
import org.kuali.rice.kim.api.group.GroupMember; |
31 | |
import org.kuali.rice.kim.api.group.GroupMemberQueryResults; |
32 | |
import org.kuali.rice.kim.api.group.GroupQueryResults; |
33 | |
import org.kuali.rice.kim.api.group.GroupService; |
34 | |
import org.kuali.rice.kim.api.role.Role; |
35 | |
|
36 | |
import javax.jws.WebParam; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 0 | public class GroupServiceMockImpl implements GroupService { |
42 | |
|
43 | 0 | private transient Map<String, Group> groupCache = new HashMap<String, Group>(); |
44 | 0 | private transient Map<String, GroupMember> groupMembershipCache = new HashMap<String, GroupMember>(); |
45 | |
|
46 | |
@Override |
47 | |
public List<String> getDirectGroupIdsByPrincipalId(String principalId) { |
48 | 0 | List<String> groups = new ArrayList<String>(); |
49 | 0 | for (GroupMember groupMembership : this.groupMembershipCache.values()) { |
50 | 0 | if (groupMembership.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) { |
51 | 0 | if (groupMembership.getMemberId().equals(principalId)) { |
52 | 0 | groups.add(groupMembership.getGroupId()); |
53 | |
} |
54 | |
} |
55 | |
} |
56 | 0 | return groups; |
57 | |
} |
58 | |
|
59 | |
@Override |
60 | |
public List<String> getDirectMemberGroupIds(String groupId) { |
61 | 0 | List<String> members = new ArrayList<String>(); |
62 | 0 | for (GroupMember groupMembership : this.groupMembershipCache.values()) { |
63 | 0 | if (groupMembership.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) { |
64 | 0 | if (groupMembership.getGroupId().equals(groupId)) { |
65 | 0 | members.add(groupMembership.getMemberId()); |
66 | |
} |
67 | |
} |
68 | |
} |
69 | 0 | return members; |
70 | |
} |
71 | |
|
72 | |
@Override |
73 | |
public List<String> getDirectMemberPrincipalIds(String groupId) { |
74 | 0 | List<String> members = new ArrayList<String>(); |
75 | 0 | for (GroupMember groupMembership : this.groupMembershipCache.values()) { |
76 | 0 | if (groupMembership.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) { |
77 | 0 | if (groupMembership.getGroupId().equals(groupId)) { |
78 | 0 | members.add(groupMembership.getMemberId()); |
79 | |
} |
80 | |
} |
81 | |
} |
82 | 0 | return members; |
83 | |
} |
84 | |
|
85 | |
@Override |
86 | |
public List<String> getDirectParentGroupIds(String groupId) { |
87 | 0 | List<String> members = new ArrayList<String>(); |
88 | 0 | for (GroupMember groupMembership : this.groupMembershipCache.values()) { |
89 | 0 | if (groupMembership.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) { |
90 | 0 | if (groupMembership.getMemberId().equals(groupId)) { |
91 | 0 | members.add(groupMembership.getGroupId()); |
92 | |
} |
93 | |
} |
94 | |
} |
95 | 0 | return members; |
96 | |
} |
97 | |
|
98 | |
|
99 | |
@Override |
100 | |
public GroupMemberQueryResults findGroupMembers(QueryByCriteria queryByCriteria) { |
101 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public Map<String, String> getAttributes(String groupId) throws RiceIllegalArgumentException { |
106 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
@Override |
111 | |
public List<Group> getGroups(Collection<String> groupIds) { |
112 | 0 | List<Group> groups = new ArrayList<Group>(); |
113 | 0 | for (String groupId : groupIds) { |
114 | 0 | Group info = this.getGroup(groupId); |
115 | 0 | groups.add(info); |
116 | 0 | } |
117 | 0 | return groups; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
@Override |
123 | |
public List<String> getGroupIdsByPrincipalId(String principalId) { |
124 | 0 | List<String> allGroups = new ArrayList<String>(); |
125 | 0 | List<String> directGroups = getDirectGroupIdsByPrincipalId(principalId); |
126 | 0 | allGroups.addAll(directGroups); |
127 | 0 | for (String groupId : directGroups) { |
128 | 0 | List<String> ancestors = this.getParentGroupIds(groupId); |
129 | 0 | allGroups.addAll(ancestors); |
130 | 0 | } |
131 | 0 | return allGroups; |
132 | |
} |
133 | |
|
134 | |
@Override |
135 | |
public List<String> getGroupIdsByPrincipalIdAndNamespaceCode(String principalId, |
136 | |
String namespaceCode) { |
137 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
138 | |
} |
139 | |
|
140 | |
@Override |
141 | |
public Group getGroup(String groupId) { |
142 | 0 | return this.groupCache.get(groupId); |
143 | |
} |
144 | |
|
145 | |
@Override |
146 | |
public Group getGroupByNameAndNamespaceCode(String namespaceCode, String groupName) { |
147 | 0 | for (Group group : this.groupCache.values()) { |
148 | 0 | if (namespaceCode.equals(group.getNamespaceCode())) { |
149 | 0 | if (groupName.equals(group.getName())) { |
150 | 0 | return group; |
151 | |
} |
152 | |
} |
153 | |
} |
154 | 0 | return null; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
@Override |
160 | |
public List<GroupMember> getMembers(List<String> groupIds) { |
161 | 0 | List<GroupMember> infos = new ArrayList<GroupMember>(); |
162 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
163 | 0 | if (groupIds.contains(info.getGroupId())) { |
164 | 0 | infos.add(info); |
165 | |
} |
166 | |
} |
167 | 0 | return infos; |
168 | |
} |
169 | |
|
170 | |
@Override |
171 | |
public List<GroupMember> getMembersOfGroup(String groupId) { |
172 | 0 | List<GroupMember> infos = new ArrayList<GroupMember>(); |
173 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
174 | 0 | if (groupId.equals(info.getGroupId())) { |
175 | 0 | infos.add(info); |
176 | |
} |
177 | |
} |
178 | 0 | return infos; |
179 | |
} |
180 | |
|
181 | |
@Override |
182 | |
public List<Group> getGroupsByPrincipalId(String principalId) throws RiceIllegalArgumentException { |
183 | 0 | Collection<String> groupIds = this.getGroupIdsByPrincipalId(principalId); |
184 | 0 | List<Group> groups = new ArrayList<Group>(); |
185 | 0 | for (String groupId : groupIds) { |
186 | 0 | Group group = this.getGroup(groupId); |
187 | 0 | groups.add(group); |
188 | 0 | } |
189 | 0 | return groups; |
190 | |
} |
191 | |
|
192 | |
@Override |
193 | |
public List<Group> getGroupsByPrincipalIdAndNamespaceCode(String principalId, |
194 | |
String namespaceCode) { |
195 | 0 | List<Group> groups = new ArrayList<Group>(); |
196 | 0 | for (Group group : this.getGroupsByPrincipalId(principalId)) { |
197 | 0 | if (namespaceCode.equals(group.getNamespaceCode())) { |
198 | 0 | groups.add(group); |
199 | |
} |
200 | |
} |
201 | 0 | return groups; |
202 | |
} |
203 | |
|
204 | |
@Override |
205 | |
public List<String> getMemberGroupIds(String groupId) { |
206 | 0 | List<String> groupIds = new ArrayList<String>(); |
207 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
208 | 0 | if (groupId.equals(info.getGroupId())) { |
209 | 0 | if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) { |
210 | 0 | groupIds.add(info.getMemberId()); |
211 | 0 | groupIds.addAll(this.getMemberGroupIds(info.getMemberId())); |
212 | |
} |
213 | |
|
214 | |
} |
215 | |
} |
216 | 0 | return groupIds; |
217 | |
} |
218 | |
|
219 | |
@Override |
220 | |
public List<String> getMemberPrincipalIds(String groupId) { |
221 | 0 | List<String> principalIds = new ArrayList<String>(); |
222 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
223 | 0 | if (groupId.equals(info.getGroupId())) { |
224 | 0 | if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) { |
225 | 0 | principalIds.addAll(this.getMemberPrincipalIds(info.getMemberId())); |
226 | 0 | } else if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) { |
227 | 0 | principalIds.add(info.getMemberId()); |
228 | |
} |
229 | |
} |
230 | |
} |
231 | 0 | return principalIds; |
232 | |
} |
233 | |
|
234 | |
@Override |
235 | |
public List<String> getParentGroupIds(String groupId) { |
236 | 0 | List<String> groupIds = new ArrayList<String>(); |
237 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
238 | 0 | if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) { |
239 | 0 | if (groupId.equals(info.getMemberId())) { |
240 | 0 | groupIds.add(info.getGroupId()); |
241 | 0 | groupIds.addAll(this.getParentGroupIds(info.getGroupId())); |
242 | |
} |
243 | |
} |
244 | |
} |
245 | 0 | return groupIds; |
246 | |
} |
247 | |
|
248 | |
@Override |
249 | |
public boolean isDirectMemberOfGroup(String principalId, String groupId) { |
250 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
251 | 0 | if (groupId.equals(info.getGroupId())) { |
252 | 0 | if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) { |
253 | 0 | if (principalId.equals(info.getMemberId())) { |
254 | 0 | return true; |
255 | |
} |
256 | |
} |
257 | |
} |
258 | |
} |
259 | 0 | return false; |
260 | |
} |
261 | |
|
262 | |
|
263 | |
public boolean isGroupActive(String groupId) { |
264 | 0 | return this.getGroup(groupId).isActive(); |
265 | |
} |
266 | |
|
267 | |
@Override |
268 | |
public boolean isGroupMemberOfGroup(String groupMemberId, String groupId) { |
269 | 0 | for (String directChildGroupId : this.getDirectMemberGroupIds(groupId)) { |
270 | 0 | if (directChildGroupId.equals(groupMemberId)) { |
271 | 0 | return true; |
272 | |
} |
273 | 0 | if (this.isGroupMemberOfGroup(groupMemberId, directChildGroupId)) { |
274 | 0 | return true; |
275 | |
} |
276 | |
} |
277 | 0 | return false; |
278 | |
} |
279 | |
|
280 | |
@Override |
281 | |
public boolean isMemberOfGroup(String principalId, String groupId) { |
282 | 0 | return this.getMemberPrincipalIds(groupId).contains(principalId); |
283 | |
} |
284 | |
|
285 | |
@Override |
286 | |
public List<String> findGroupIds(QueryByCriteria queryByCriteria) { |
287 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
288 | |
} |
289 | |
|
290 | |
@Override |
291 | |
public GroupQueryResults findGroups(QueryByCriteria queryByCriteria) { |
292 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
293 | |
} |
294 | |
|
295 | |
|
296 | |
|
297 | |
@Override |
298 | |
public boolean addGroupToGroup(String childId, String parentId) |
299 | |
throws UnsupportedOperationException { |
300 | 0 | if (childId.equals(parentId)) { |
301 | 0 | throw new IllegalArgumentException("Can't add group to itself."); |
302 | |
} |
303 | 0 | if (isGroupMemberOfGroup(parentId, childId)) { |
304 | 0 | throw new IllegalArgumentException("Circular group reference."); |
305 | |
} |
306 | 0 | if (this.getDirectMemberGroupIds(parentId).contains(childId)) { |
307 | 0 | return false; |
308 | |
} |
309 | 0 | GroupMember.Builder GMB = GroupMember.Builder.create(parentId, |
310 | |
childId, |
311 | |
KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE); |
312 | 0 | GMB.setId(UUID.randomUUID().toString()); |
313 | 0 | GroupMember groupMembership = GMB.build(); |
314 | 0 | this.groupMembershipCache.put(groupMembership.getMemberId(), groupMembership); |
315 | 0 | return true; |
316 | |
} |
317 | |
|
318 | |
@Override |
319 | |
public boolean addPrincipalToGroup(String principalId, String groupId) |
320 | |
throws UnsupportedOperationException { |
321 | 0 | if (this.getDirectMemberPrincipalIds(groupId).contains(principalId)) { |
322 | 0 | return false; |
323 | |
} |
324 | 0 | GroupMember.Builder GMB = GroupMember.Builder.create(groupId, |
325 | |
principalId, |
326 | |
KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
327 | 0 | GMB.setId(UUID.randomUUID().toString()); |
328 | 0 | GroupMember groupMembership = GMB.build(); |
329 | |
|
330 | |
|
331 | |
|
332 | 0 | this.groupMembershipCache.put(groupMembership.getMemberId(), groupMembership); |
333 | 0 | return true; |
334 | |
} |
335 | |
|
336 | |
@Override |
337 | |
public Group createGroup(Group group) |
338 | |
throws UnsupportedOperationException { |
339 | 0 | Group copy = new MockHelper().copy(group); |
340 | 0 | if (copy.getId() != null) { |
341 | 0 | if (this.getGroup(copy.getId()) != null) { |
342 | 0 | throw new IllegalArgumentException("duplicate id"); |
343 | |
} |
344 | |
} else { |
345 | 0 | Group.Builder.create(group) .setId(UUID.randomUUID().toString()); |
346 | |
} |
347 | 0 | if (this.getGroupByNameAndNamespaceCode(copy.getNamespaceCode(), copy.getName()) != null) { |
348 | 0 | throw new IllegalArgumentException("name in use"); |
349 | |
} |
350 | 0 | this.groupCache.put(copy.getId(), copy); |
351 | 0 | return copy; |
352 | |
|
353 | |
} |
354 | |
|
355 | |
@Override |
356 | |
public void removeAllMembers(String groupId) throws UnsupportedOperationException { |
357 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
358 | 0 | if (info.getGroupId().equals(groupId)) { |
359 | 0 | groupMembershipCache.remove(info.getMemberId()); |
360 | |
} |
361 | |
} |
362 | 0 | } |
363 | |
|
364 | |
@Override |
365 | |
public boolean removeGroupFromGroup(String childId, String parentId) throws UnsupportedOperationException { |
366 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
367 | 0 | if (info.getGroupId().equals(parentId)) { |
368 | 0 | if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) { |
369 | 0 | if (info.getMemberId().equals(childId)) { |
370 | 0 | groupMembershipCache.remove(info.getMemberId()); |
371 | 0 | return true; |
372 | |
} |
373 | |
} |
374 | |
} |
375 | |
} |
376 | 0 | return false; |
377 | |
} |
378 | |
|
379 | |
@Override |
380 | |
public boolean removePrincipalFromGroup(String principalId, String groupId) |
381 | |
throws UnsupportedOperationException { |
382 | 0 | for (GroupMember info : this.groupMembershipCache.values()) { |
383 | 0 | if (info.getGroupId().equals(groupId)) { |
384 | |
|
385 | 0 | if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) { |
386 | 0 | if (this.removePrincipalFromGroup(principalId, info.getMemberId())) { |
387 | 0 | return true; |
388 | |
} |
389 | 0 | if (info.getMemberId().equals(principalId)) { |
390 | 0 | groupMembershipCache.remove(info.getMemberId()); |
391 | 0 | return true; |
392 | |
} |
393 | |
} |
394 | |
} |
395 | |
} |
396 | 0 | return false; |
397 | |
} |
398 | |
|
399 | |
@Override |
400 | |
public Group updateGroup(String groupId, Group Group) throws UnsupportedOperationException { |
401 | 0 | if (groupId == null) { |
402 | 0 | throw new IllegalArgumentException("Group id cannot be null"); |
403 | |
} |
404 | 0 | if (Group.getId() == null) { |
405 | 0 | throw new IllegalArgumentException("New group id cannot be null"); |
406 | |
} |
407 | 0 | Group existing = this.getGroup(groupId); |
408 | 0 | if (existing == null) { |
409 | 0 | throw new IllegalArgumentException("group id not found"); |
410 | |
} |
411 | 0 | Group matching = this.getGroupByNameAndNamespaceCode(Group.getNamespaceCode(), Group.getName()); |
412 | 0 | if (matching != null) { |
413 | 0 | if (matching != existing) { |
414 | 0 | throw new IllegalArgumentException("name in use"); |
415 | |
} |
416 | |
} |
417 | 0 | Group copy = new MockHelper().copy(Group); |
418 | 0 | this.groupCache.put(copy.getId(), copy); |
419 | 0 | return copy; |
420 | |
} |
421 | |
|
422 | |
@Override |
423 | |
|
424 | |
public Group updateGroup(Group Group) throws UnsupportedOperationException { |
425 | 0 | return updateGroup(Group.getId(), Group); |
426 | |
} |
427 | |
|
428 | |
@Override |
429 | |
public GroupMember createGroupMember(@WebParam(name="groupMember") GroupMember groupMember) throws RiceIllegalArgumentException{ |
430 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
431 | |
} |
432 | |
|
433 | |
@Override |
434 | |
public GroupMember updateGroupMember(@WebParam(name="groupMember") GroupMember groupMember) throws RiceIllegalArgumentException{ |
435 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
436 | |
} |
437 | |
|
438 | |
} |
439 | |
|