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.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.kuali.rice.core.api.criteria.QueryByCriteria;
24 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
25 import org.kuali.rice.core.api.exception.RiceIllegalStateException;
26 import org.kuali.rice.kim.api.common.assignee.Assignee;
27 import org.kuali.rice.kim.api.common.template.Template;
28 import org.kuali.rice.kim.api.common.template.TemplateQueryResults;
29 import org.kuali.rice.kim.api.permission.Permission;
30 import org.kuali.rice.kim.api.permission.PermissionQueryResults;
31 import org.kuali.rice.kim.api.permission.PermissionService;
32 import org.kuali.rice.kim.api.role.Role;
33 import org.kuali.rice.kim.api.role.RoleMembership;
34 import org.kuali.student.common.mock.MockService;
35
36 import javax.jws.WebParam;
37 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
38
39
40
41
42 public class PermissionServiceMockImpl implements PermissionService, MockService {
43
44 private transient Map<String, Template> permissionTemplateCache = new HashMap<String, Template>();
45 private transient Map<String, Permission> permissionCache = new HashMap<String, Permission>();
46 private transient Map<String, Role> roleCache = new HashMap<String, Role>();
47 private transient Map<String, RoleMembership> roleMembershipCache = new HashMap<String, RoleMembership>();
48
49
50 @Override
51 public void clear() {
52
53 this.permissionCache.clear();
54 this.permissionTemplateCache.clear();
55 this.roleCache.clear();
56 this.roleMembershipCache.clear();
57
58 }
59
60 @Override
61 public List<Template> getAllTemplates() {
62 return new ArrayList<Template>(this.permissionTemplateCache.values());
63 }
64
65 @Override
66 public List<Permission> getAuthorizedPermissions(String principalId,
67 String namespaceCode,
68 String permissionName,
69 Map<String, String> qualification) {
70 List<Permission> list = new ArrayList<Permission>();
71 for (Permission permission : this.permissionCache.values()) {
72 if (namespaceCode.equals(permission.getNamespaceCode())) {
73 if (permissionName.equals(permission.getName())) {
74 if (matchesQualification(permission, qualification)) {
75 if (matchesPrincipalId(permission, principalId)) {
76 list.add(permission);
77 }
78 }
79 }
80 }
81 }
82 return list;
83 }
84
85 private boolean matchesPermissionDetails(Permission permission, Map<String, String> permissionDetails) {
86
87 return true;
88 }
89
90 private boolean matchesQualification(Permission permission, Map<String, String> qualification) {
91
92 return true;
93 }
94
95 private boolean matchesPrincipalId(Permission permission, String principalId) {
96
97 return true;
98 }
99
100 @Override
101 public List<Permission> getAuthorizedPermissionsByTemplate(String principalId,
102 String namespaceCode,
103 String permissionTemplateName,
104 Map<String, String> permissionDetails,
105 Map<String, String> qualification) {
106 List<Permission> list = new ArrayList<Permission>();
107 for (Permission permission : this.permissionCache.values()) {
108 if (permission.getTemplate() != null) {
109 if (namespaceCode.equals(permission.getTemplate().getNamespaceCode())) {
110 if (permissionTemplateName.equals(permission.getTemplate().getName())) {
111 if (matchesPermissionDetails(permission, permissionDetails)) {
112 if (matchesQualification(permission, qualification)) {
113 if (matchesPrincipalId(permission, principalId)) {
114 list.add(permission);
115 }
116 }
117 }
118 }
119 }
120 }
121 }
122 return list;
123 }
124
125 @Override
126 public Permission getPermission(String permissionId) {
127 return this.permissionCache.get(permissionId);
128 }
129
130 @Override
131 public List<Assignee> getPermissionAssignees(String namespaceCode,
132 String permissionName,
133 Map<String, String> qualification) {
134 List<Assignee> list = new ArrayList<Assignee>();
135 for (Permission permission : this.permissionCache.values()) {
136 if (namespaceCode.equals(permission.getNamespaceCode())) {
137 if (permissionName.equals(permission.getName())) {
138 if (matchesQualification(permission, qualification)) {
139 list.addAll(getPermissionAssignees(permission));
140 }
141 }
142 }
143 }
144 return list;
145 }
146
147 private List<Assignee> getPermissionAssignees(Permission permission) {
148 List<Assignee> list = new ArrayList<Assignee>();
149
150 return list;
151 }
152
153 @Override
154 public List<Assignee> getPermissionAssigneesByTemplate(String namespaceCode,
155 String permissionTemplateName,
156 Map<String, String> permissionDetails,
157 Map<String, String> qualification) {
158 List<Assignee> list = new ArrayList<Assignee>();
159 for (Permission permission : this.permissionCache.values()) {
160 if (permission.getTemplate() != null) {
161 if (namespaceCode.equals(permission.getTemplate().getNamespaceCode())) {
162 if (permissionTemplateName.equals(permission.getTemplate().getName())) {
163 if (matchesPermissionDetails(permission, permissionDetails)) {
164 if (matchesQualification(permission, qualification)) {
165 list.addAll(getPermissionAssignees(permission));
166 }
167 }
168 }
169 }
170 }
171 }
172 return list;
173 }
174
175 @Override
176 public Template getPermissionTemplate(String permissionTemplateId) {
177 return this.permissionTemplateCache.get(permissionTemplateId);
178 }
179
180 @Override
181 public Template findPermTemplateByNamespaceCodeAndName(String namespaceCode,
182 String permissionTemplateName) {
183 for (Template template : this.permissionTemplateCache.values()) {
184 if (template.getNamespaceCode().equals(namespaceCode)) {
185 if (template.getName().equals(permissionTemplateName)) {
186 return template;
187 }
188 }
189 }
190 return null;
191 }
192
193 @Override
194 public Permission findPermByNamespaceCodeAndName(String namespaceCode, String permissionName) {
195 List<Permission> list = new ArrayList<Permission>();
196 for (Permission permission : this.permissionCache.values()) {
197 if (namespaceCode.equals(permission.getNamespaceCode())) {
198 if (permissionName.equals(permission.getName())) {
199 if (permission.isActive()) {
200 list.add(permission);
201 }
202 }
203 }
204 }
205 if (list.isEmpty()) {
206 return null;
207 }
208 return list.get(0);
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 @Override
231 public List<Permission> findPermissionsByTemplate(String namespaceCode,
232 String permissionTemplateName) {
233 List<Permission> list = new ArrayList<Permission>();
234 for (Permission permission : this.permissionCache.values()) {
235 if (permission.getTemplate() != null) {
236 if (namespaceCode.equals(permission.getTemplate().getNamespaceCode())) {
237 if (permissionTemplateName.equals(permission.getTemplate().getName())) {
238 list.add(permission);
239 }
240 }
241 }
242 }
243 return list;
244 }
245
246 @Override
247 public List<String> getRoleIdsForPermission(String namespaceCode,
248 String permissionName) {
249 throw new UnsupportedOperationException("Not supported yet.");
250 }
251
252 @Override
253 public boolean hasPermission(String principalId, String namespaceCode,
254 String permissionName) {
255 throw new UnsupportedOperationException("Not supported yet.");
256 }
257
258 @Override
259 public boolean hasPermissionByTemplate(String principalId,
260 String namespaceCode,
261 String permissionTemplateName,
262 Map<String, String> permissionDetails) {
263 throw new UnsupportedOperationException("Not supported yet.");
264 }
265
266 @Override
267 public boolean isAuthorized(String principalId, String namespaceCode,
268 String permissionName,
269 Map<String, String> qualification) {
270 if (principalId != null) {
271 if (principalId.equals("123")) {
272 return true;
273 } else if (principalId.endsWith("TestDataLoader")) {
274 return true;
275 }
276 return false;
277 } else {
278 return false;
279 }
280 }
281
282 @Override
283 public boolean isAuthorizedByTemplate(String principalId,
284 String namespaceCode,
285 String permissionTemplateName,
286 Map<String, String> permissionDetails,
287 Map<String, String> qualification) {
288 throw new UnsupportedOperationException("Not supported yet.");
289 }
290
291 @Override
292 public boolean isPermissionDefined(String namespaceCode, String permissionName) {
293 for (Permission permission : this.permissionCache.values()) {
294 if (namespaceCode.equals(permission.getNamespaceCode())) {
295 if (permissionName.equals(permission.getName())) {
296 return true;
297 }
298 }
299 }
300
301 return false;
302 }
303
304 @Override
305 public boolean isPermissionDefinedByTemplate(String namespaceCode,
306 String permissionTemplateName,
307 Map<String, String> permissionDetails) {
308 for (Permission permission : this.permissionCache.values()) {
309 if (permission.getTemplate() != null) {
310 if (namespaceCode.equals(permission.getTemplate().getNamespaceCode())) {
311 if (permissionTemplateName.equals(permission.getTemplate().getName())) {
312 if (matchesPermissionDetails(permission, permissionDetails)) {
313 return true;
314 }
315 }
316 }
317 }
318 }
319 return false;
320 }
321
322 @Override
323 public PermissionQueryResults findPermissions(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
324 throw new UnsupportedOperationException("Not supported yet.");
325 }
326
327 @Override
328 public Permission createPermission(Permission permission) throws RiceIllegalArgumentException, RiceIllegalStateException {
329 throw new UnsupportedOperationException("Not supported yet.");
330 }
331
332 @Override
333 public Permission updatePermission(Permission permission) throws RiceIllegalArgumentException, RiceIllegalStateException {
334 throw new UnsupportedOperationException("Not supported yet.");
335 }
336
337 @Override
338 public TemplateQueryResults findPermissionTemplates(QueryByCriteria queryByCriteria) throws RiceIllegalArgumentException {
339 throw new UnsupportedOperationException("Not supported yet.");
340 }
341 }
342