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.*; |
19 | |
import org.kuali.rice.kim.bo.Group; |
20 | |
import org.kuali.rice.kim.bo.Role; |
21 | |
import org.kuali.rice.kim.bo.group.dto.GroupInfo; |
22 | |
import org.kuali.rice.kim.bo.group.dto.GroupMembershipInfo; |
23 | |
import org.kuali.rice.kim.service.GroupService; |
24 | |
import org.kuali.rice.kim.service.GroupUpdateService; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class GroupServiceMockImpl implements GroupService, |
30 | |
GroupUpdateService { |
31 | |
|
32 | 0 | private transient Map<String, GroupInfo> groupCache = new HashMap<String, GroupInfo>(); |
33 | 0 | private transient Map<String, GroupMembershipInfo> groupMembershipCache = new HashMap<String, GroupMembershipInfo>(); |
34 | |
|
35 | |
@Override |
36 | |
public List<String> getDirectGroupIdsForPrincipal(String principalId) { |
37 | 0 | List<String> groups = new ArrayList<String>(); |
38 | 0 | for (GroupMembershipInfo groupMembership : this.groupMembershipCache.values()) { |
39 | 0 | if (groupMembership.getMemberTypeCode().equals(Role.PRINCIPAL_MEMBER_TYPE)) { |
40 | 0 | if (groupMembership.getMemberId().equals(principalId)) { |
41 | 0 | groups.add(groupMembership.getGroupId()); |
42 | |
} |
43 | |
} |
44 | |
} |
45 | 0 | return groups; |
46 | |
} |
47 | |
|
48 | |
@Override |
49 | |
public List<String> getDirectMemberGroupIds(String groupId) { |
50 | 0 | List<String> members = new ArrayList<String>(); |
51 | 0 | for (GroupMembershipInfo groupMembership : this.groupMembershipCache.values()) { |
52 | 0 | if (groupMembership.getMemberTypeCode().equals(Role.GROUP_MEMBER_TYPE)) { |
53 | 0 | if (groupMembership.getGroupId().equals(groupId)) { |
54 | 0 | members.add(groupMembership.getMemberId()); |
55 | |
} |
56 | |
} |
57 | |
} |
58 | 0 | return members; |
59 | |
} |
60 | |
|
61 | |
@Override |
62 | |
public List<String> getDirectMemberPrincipalIds(String groupId) { |
63 | 0 | List<String> members = new ArrayList<String>(); |
64 | 0 | for (GroupMembershipInfo groupMembership : this.groupMembershipCache.values()) { |
65 | 0 | if (groupMembership.getMemberTypeCode().equals(Role.PRINCIPAL_MEMBER_TYPE)) { |
66 | 0 | if (groupMembership.getGroupId().equals(groupId)) { |
67 | 0 | members.add(groupMembership.getMemberId()); |
68 | |
} |
69 | |
} |
70 | |
} |
71 | 0 | return members; |
72 | |
} |
73 | |
|
74 | |
@Override |
75 | |
public List<String> getDirectParentGroupIds(String groupId) { |
76 | 0 | List<String> members = new ArrayList<String>(); |
77 | 0 | for (GroupMembershipInfo groupMembership : this.groupMembershipCache.values()) { |
78 | 0 | if (groupMembership.getMemberTypeCode().equals(Role.PRINCIPAL_MEMBER_TYPE)) { |
79 | 0 | if (groupMembership.getMemberId().equals(groupId)) { |
80 | 0 | members.add(groupMembership.getGroupId()); |
81 | |
} |
82 | |
} |
83 | |
} |
84 | 0 | return members; |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public Map<String, String> getGroupAttributes(String groupId) { |
89 | 0 | return this.getGroupInfo(groupId).getAttributes(); |
90 | |
} |
91 | |
|
92 | |
@Override |
93 | |
public List<String> getGroupIdsForPrincipal(String principalId) { |
94 | 0 | List<String> allGroups = new ArrayList<String>(); |
95 | 0 | List<String> directGroups = getDirectGroupIdsForPrincipal(principalId); |
96 | 0 | allGroups.addAll(directGroups); |
97 | 0 | for (String groupId : directGroups) { |
98 | 0 | List<String> ancestors = this.getParentGroupIds(groupId); |
99 | 0 | allGroups.addAll(ancestors); |
100 | 0 | } |
101 | 0 | return allGroups; |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public List<String> getGroupIdsForPrincipalByNamespace(String principalId, |
106 | |
String namespaceCode) { |
107 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
108 | |
} |
109 | |
|
110 | |
@Override |
111 | |
public GroupInfo getGroupInfo(String groupId) { |
112 | 0 | return this.groupCache.get(groupId); |
113 | |
} |
114 | |
|
115 | |
@Override |
116 | |
public GroupInfo getGroupInfoByName(String namespaceCode, String groupName) { |
117 | 0 | for (GroupInfo group : this.groupCache.values()) { |
118 | 0 | if (namespaceCode.equals(group.getNamespaceCode())) { |
119 | 0 | if (groupName.equals(group.getGroupName())) { |
120 | 0 | return group; |
121 | |
} |
122 | |
} |
123 | |
} |
124 | 0 | return null; |
125 | |
} |
126 | |
|
127 | |
@Override |
128 | |
public Map<String, GroupInfo> getGroupInfos(Collection<String> groupIds) { |
129 | 0 | Map<String, GroupInfo> groups = new HashMap<String, GroupInfo>(); |
130 | 0 | for (String groupId : groupIds) { |
131 | 0 | GroupInfo info = this.getGroupInfo(groupId); |
132 | 0 | groups.put(groupId, info); |
133 | 0 | } |
134 | 0 | return groups; |
135 | |
} |
136 | |
|
137 | |
@Override |
138 | |
public Collection<GroupMembershipInfo> getGroupMembers(List<String> groupIds) { |
139 | 0 | Collection<GroupMembershipInfo> infos = new ArrayList<GroupMembershipInfo>(); |
140 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
141 | 0 | if (groupIds.contains(info.getGroupId())) { |
142 | 0 | infos.add(info); |
143 | |
} |
144 | |
} |
145 | 0 | return infos; |
146 | |
} |
147 | |
|
148 | |
@Override |
149 | |
public Collection<GroupMembershipInfo> getGroupMembersOfGroup(String groupId) { |
150 | 0 | Collection<GroupMembershipInfo> infos = new ArrayList<GroupMembershipInfo>(); |
151 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
152 | 0 | if (groupId.equals(info.getGroupId())) { |
153 | 0 | infos.add(info); |
154 | |
} |
155 | |
} |
156 | 0 | return infos; |
157 | |
} |
158 | |
|
159 | |
@Override |
160 | |
public List<GroupInfo> getGroupsForPrincipal(String principalId) { |
161 | 0 | Collection<String> groupIds = this.getGroupIdsForPrincipal(principalId); |
162 | 0 | List<GroupInfo> groups = new ArrayList<GroupInfo>(); |
163 | 0 | for (String groupId : groupIds) { |
164 | 0 | GroupInfo group = this.getGroupInfo(groupId); |
165 | 0 | groups.add(group); |
166 | 0 | } |
167 | 0 | return groups; |
168 | |
} |
169 | |
|
170 | |
@Override |
171 | |
public List<GroupInfo> getGroupsForPrincipalByNamespace(String principalId, |
172 | |
String namespaceCode) { |
173 | 0 | List<GroupInfo> groups = new ArrayList<GroupInfo>(); |
174 | 0 | for (GroupInfo group : this.getGroupsForPrincipal(principalId)) { |
175 | 0 | if (namespaceCode.equals(group.getNamespaceCode())) { |
176 | 0 | groups.add(group); |
177 | |
} |
178 | |
} |
179 | 0 | return groups; |
180 | |
} |
181 | |
|
182 | |
@Override |
183 | |
public List<String> getMemberGroupIds(String groupId) { |
184 | 0 | List<String> groupIds = new ArrayList<String>(); |
185 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
186 | 0 | if (groupId.equals(info.getGroupId())) { |
187 | 0 | if (info.getMemberTypeCode().equals(Role.GROUP_MEMBER_TYPE)) { |
188 | 0 | groupIds.add(info.getMemberId()); |
189 | 0 | groupIds.addAll(this.getMemberGroupIds(info.getMemberId())); |
190 | |
} |
191 | |
|
192 | |
} |
193 | |
} |
194 | 0 | return groupIds; |
195 | |
} |
196 | |
|
197 | |
@Override |
198 | |
public List<String> getMemberPrincipalIds(String groupId) { |
199 | 0 | List<String> principalIds = new ArrayList<String>(); |
200 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
201 | 0 | if (groupId.equals(info.getGroupId())) { |
202 | 0 | if (info.getMemberTypeCode().equals(Role.GROUP_MEMBER_TYPE)) { |
203 | 0 | principalIds.addAll(this.getMemberPrincipalIds(info.getMemberId())); |
204 | 0 | } else if (info.getMemberTypeCode().equals(Role.PRINCIPAL_MEMBER_TYPE)) { |
205 | 0 | principalIds.add(info.getMemberId()); |
206 | |
} |
207 | |
} |
208 | |
} |
209 | 0 | return principalIds; |
210 | |
} |
211 | |
|
212 | |
@Override |
213 | |
public List<String> getParentGroupIds(String groupId) { |
214 | 0 | List<String> groupIds = new ArrayList<String>(); |
215 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
216 | 0 | if (info.getMemberTypeCode().equals(Role.GROUP_MEMBER_TYPE)) { |
217 | 0 | if (groupId.equals(info.getMemberId())) { |
218 | 0 | groupIds.add(info.getGroupId()); |
219 | 0 | groupIds.addAll(this.getParentGroupIds(info.getGroupId())); |
220 | |
} |
221 | |
} |
222 | |
} |
223 | 0 | return groupIds; |
224 | |
} |
225 | |
|
226 | |
@Override |
227 | |
public boolean isDirectMemberOfGroup(String principalId, String groupId) { |
228 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
229 | 0 | if (groupId.equals(info.getGroupId())) { |
230 | 0 | if (info.getMemberTypeCode().equals(Role.PRINCIPAL_MEMBER_TYPE)) { |
231 | 0 | if (principalId.equals(info.getMemberId())) { |
232 | 0 | return true; |
233 | |
} |
234 | |
} |
235 | |
} |
236 | |
} |
237 | 0 | return false; |
238 | |
} |
239 | |
|
240 | |
@Override |
241 | |
public boolean isGroupActive(String groupId) { |
242 | 0 | return this.getGroupInfo(groupId).isActive(); |
243 | |
} |
244 | |
|
245 | |
@Override |
246 | |
public boolean isGroupMemberOfGroup(String groupMemberId, String groupId) { |
247 | 0 | for (String directChildGroupId : this.getDirectMemberGroupIds(groupId)) { |
248 | 0 | if (directChildGroupId.equals(groupMemberId)) { |
249 | 0 | return true; |
250 | |
} |
251 | 0 | if (this.isGroupMemberOfGroup(groupMemberId, directChildGroupId)) { |
252 | 0 | return true; |
253 | |
} |
254 | |
} |
255 | 0 | return false; |
256 | |
} |
257 | |
|
258 | |
@Override |
259 | |
public boolean isMemberOfGroup(String principalId, String groupId) { |
260 | 0 | return this.getMemberPrincipalIds(groupId).contains(principalId); |
261 | |
} |
262 | |
|
263 | |
@Override |
264 | |
public List<String> lookupGroupIds(Map<String, String> searchCriteria) { |
265 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
266 | |
} |
267 | |
|
268 | |
@Override |
269 | |
public List<? extends Group> lookupGroups(Map<String, String> searchCriteria) { |
270 | 0 | throw new UnsupportedOperationException("Not supported yet."); |
271 | |
} |
272 | |
|
273 | |
@Override |
274 | |
public boolean addGroupToGroup(String childId, String parentId) |
275 | |
throws UnsupportedOperationException { |
276 | 0 | if (childId.equals(parentId)) { |
277 | 0 | throw new IllegalArgumentException("Can't add group to itself."); |
278 | |
} |
279 | 0 | if (isGroupMemberOfGroup(parentId, childId)) { |
280 | 0 | throw new IllegalArgumentException("Circular group reference."); |
281 | |
} |
282 | 0 | if (this.getDirectMemberGroupIds(parentId).contains(childId)) { |
283 | 0 | return false; |
284 | |
} |
285 | 0 | GroupMembershipInfo groupMembership = new GroupMembershipInfo(parentId, |
286 | |
UUID.randomUUID().toString(), |
287 | |
childId, |
288 | |
Role.GROUP_MEMBER_TYPE, |
289 | |
new Date(), |
290 | |
null); |
291 | 0 | this.groupMembershipCache.put(groupMembership.getGroupMemberId(), groupMembership); |
292 | 0 | return true; |
293 | |
} |
294 | |
|
295 | |
@Override |
296 | |
public boolean addPrincipalToGroup(String principalId, String groupId) |
297 | |
throws UnsupportedOperationException { |
298 | 0 | if (this.getDirectMemberPrincipalIds(groupId).contains(principalId)) { |
299 | 0 | return false; |
300 | |
} |
301 | 0 | GroupMembershipInfo groupMembership = new GroupMembershipInfo(groupId, |
302 | |
UUID.randomUUID().toString(), |
303 | |
principalId, |
304 | |
Role.PRINCIPAL_MEMBER_TYPE, |
305 | |
new Date(), |
306 | |
null); |
307 | 0 | this.groupMembershipCache.put(groupMembership.getGroupMemberId(), groupMembership); |
308 | 0 | return true; |
309 | |
} |
310 | |
|
311 | |
@Override |
312 | |
public GroupInfo createGroup(GroupInfo groupInfo) |
313 | |
throws UnsupportedOperationException { |
314 | 0 | GroupInfo copy = new MockHelper().copy(groupInfo); |
315 | 0 | if (copy.getGroupId() != null) { |
316 | 0 | if (this.getGroupInfo(copy.getGroupId()) != null) { |
317 | 0 | throw new IllegalArgumentException("duplicate id"); |
318 | |
} |
319 | |
} else { |
320 | 0 | copy.setGroupId(UUID.randomUUID().toString()); |
321 | |
} |
322 | 0 | if (this.getGroupInfoByName(copy.getNamespaceCode(), copy.getGroupName()) != null) { |
323 | 0 | throw new IllegalArgumentException("name in use"); |
324 | |
} |
325 | 0 | this.groupCache.put(copy.getGroupId(), copy); |
326 | 0 | return copy; |
327 | |
|
328 | |
} |
329 | |
|
330 | |
@Override |
331 | |
public void removeAllGroupMembers(String groupId) throws UnsupportedOperationException { |
332 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
333 | 0 | if (info.getGroupId().equals(groupId)) { |
334 | 0 | groupMembershipCache.remove(info.getGroupMemberId()); |
335 | |
} |
336 | |
} |
337 | 0 | } |
338 | |
|
339 | |
@Override |
340 | |
public boolean removeGroupFromGroup(String childId, String parentId) throws UnsupportedOperationException { |
341 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
342 | 0 | if (info.getGroupId().equals(parentId)) { |
343 | 0 | if (info.getMemberTypeCode().equals(Role.GROUP_MEMBER_TYPE)) { |
344 | 0 | if (info.getMemberId().equals(childId)) { |
345 | 0 | groupMembershipCache.remove(info.getGroupMemberId()); |
346 | 0 | return true; |
347 | |
} |
348 | |
} |
349 | |
} |
350 | |
} |
351 | 0 | return false; |
352 | |
} |
353 | |
|
354 | |
@Override |
355 | |
public boolean removePrincipalFromGroup(String principalId, String groupId) |
356 | |
throws UnsupportedOperationException { |
357 | 0 | for (GroupMembershipInfo info : this.groupMembershipCache.values()) { |
358 | 0 | if (info.getGroupId().equals(groupId)) { |
359 | 0 | if (info.getMemberTypeCode().equals(Role.GROUP_MEMBER_TYPE)) { |
360 | 0 | if (this.removePrincipalFromGroup(principalId, info.getMemberId())) { |
361 | 0 | return true; |
362 | |
} |
363 | 0 | if (info.getMemberId().equals(principalId)) { |
364 | 0 | groupMembershipCache.remove(info.getGroupMemberId()); |
365 | 0 | return true; |
366 | |
} |
367 | |
} |
368 | |
} |
369 | |
} |
370 | 0 | return false; |
371 | |
} |
372 | |
|
373 | |
@Override |
374 | |
public GroupInfo updateGroup(String groupId, GroupInfo groupInfo) throws UnsupportedOperationException { |
375 | 0 | if (groupId == null) { |
376 | 0 | throw new IllegalArgumentException("Group id cannot be null"); |
377 | |
} |
378 | 0 | if (groupInfo.getGroupId() == null) { |
379 | 0 | throw new IllegalArgumentException("New group id cannot be null"); |
380 | |
} |
381 | 0 | GroupInfo existing = this.getGroupInfo(groupId); |
382 | 0 | if (existing == null) { |
383 | 0 | throw new IllegalArgumentException("group id not found"); |
384 | |
} |
385 | 0 | GroupInfo matching = this.getGroupInfoByName(groupInfo.getNamespaceCode(), groupInfo.getGroupName()); |
386 | 0 | if (matching != null) { |
387 | 0 | if (matching != existing) { |
388 | 0 | throw new IllegalArgumentException("name in use"); |
389 | |
} |
390 | |
} |
391 | 0 | GroupInfo copy = new MockHelper().copy(groupInfo); |
392 | 0 | this.groupCache.put(copy.getGroupId(), copy); |
393 | 0 | return copy; |
394 | |
} |
395 | |
} |
396 | |
|