1
2
3
4
5 package edu.internet2.middleware.grouper.ui.customUi;
6
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
12 import org.apache.commons.lang.StringUtils;
13
14 import edu.internet2.middleware.grouper.Group;
15 import edu.internet2.middleware.grouper.GroupFinder;
16 import edu.internet2.middleware.grouper.GrouperSession;
17 import edu.internet2.middleware.grouper.Member;
18 import edu.internet2.middleware.grouper.MemberFinder;
19 import edu.internet2.middleware.grouper.MembershipFinder;
20 import edu.internet2.middleware.grouper.Stem;
21 import edu.internet2.middleware.grouper.Stem.Scope;
22 import edu.internet2.middleware.grouper.StemFinder;
23 import edu.internet2.middleware.grouper.SubjectFinder;
24 import edu.internet2.middleware.grouper.attr.AttributeDef;
25 import edu.internet2.middleware.grouper.attr.finder.AttributeDefFinder;
26 import edu.internet2.middleware.grouper.exception.GrouperSessionException;
27 import edu.internet2.middleware.grouper.membership.MembershipResult;
28 import edu.internet2.middleware.grouper.membership.MembershipSubjectContainer;
29 import edu.internet2.middleware.grouper.misc.GrouperSessionHandler;
30 import edu.internet2.middleware.grouper.misc.GrouperStartup;
31 import edu.internet2.middleware.grouper.util.GrouperUtil;
32 import edu.internet2.middleware.grouperClient.collections.MultiKey;
33 import edu.internet2.middleware.subject.Subject;
34
35
36
37
38
39 public class CustomUiGrouper extends CustomUiUserQueryBase {
40
41
42
43
44
45
46 public static void main(String[] args) throws Exception {
47 GrouperStartup.startup();
48
49 GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
50
51 @Override
52 public Object callback(GrouperSession grouperSession) throws GrouperSessionException {
53
54 Subject subject1 = SubjectFinder.findById("10021368", true);
55 Subject subject2 = SubjectFinder.findById("13228666", true);
56 Subject subject3 = SubjectFinder.findById("10002177", true);
57 Subject subject4 = SubjectFinder.findById("15251428", true);
58
59 Group group = GroupFinder.findByName(grouperSession, "penn:isc:ait:apps:O365:twoStepProd:o365_two_step_prod", true);
60
61 CustomUiGrouperomUi/CustomUiGrouper.html#CustomUiGrouper">CustomUiGrouper customUiGrouper = new CustomUiGrouper();
62
63 for (Subject/subject/Subject.html#Subject">Subject subject : new Subject[]{subject1, subject2, subject3, subject4}) {
64 boolean hasMembership = customUiGrouper.hasGrouperMembership(group, subject);
65
66 System.out.println(hasMembership);
67
68 boolean hasPrivilege = customUiGrouper.hasDirectGrouperGroupPrivilege(group, subject, "read");
69
70 System.out.println(hasPrivilege);
71
72 boolean canPrivilege = customUiGrouper.canHaveGroupPrivilege(group, subject, "read");
73
74 System.out.println(canPrivilege);
75 }
76 return null;
77 }
78 });
79 }
80
81
82
83
84 private Map<MultiKey, Group> membershipGroupNameSourceIdSubjectIdToGroupMap = new HashMap<MultiKey, Group>();
85
86
87
88
89
90 public Map<MultiKey, Group> getMembershipGroupNameSourceIdSubjectIdToGroupMap() {
91 return this.membershipGroupNameSourceIdSubjectIdToGroupMap;
92 }
93
94
95
96
97
98
99
100 public Map<String, Stem> getStemIdAndNameToStem() {
101 return this.stemIdAndNameToStem;
102 }
103
104
105
106
107
108
109
110 public Map<String, AttributeDef> getAttributeDefIdAndNameToAttributeDef() {
111 return this.attributeDefIdAndNameToAttributeDef;
112 }
113
114
115
116
117
118
119 public Map<String, Group> getGroupIdAndNameToGroup() {
120 return this.groupIdAndNameToGroup;
121 }
122
123
124
125
126
127
128
129 public void cacheMembershipsInStem(final String stemNames, final Subject subject) {
130 long startedNanos = System.nanoTime();
131
132 try {
133
134 String[] stemNamesArray = GrouperUtil.splitTrim(stemNames, ",");
135
136 this.debugMapPut("cacheMemStems", stemNamesArray.length);
137
138 for (int i=0;i<stemNamesArray.length;i++) {
139 final String stemName = stemNamesArray[0];
140 final String stemExtension = GrouperUtil.extensionFromName(stemName);
141
142 GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
143
144 public Object callback(GrouperSession grouperSession)
145 throws GrouperSessionException {
146
147 Stem stem = StemFinder.findByName(grouperSession, stemName, true);
148 Member member = MemberFinder.findBySubject(grouperSession, subject, true);
149
150 MembershipResult membershipResult = new MembershipFinder().assignStem(stem).assignStemScope(Scope.SUB).assignEnabled(true)
151 .addField(Group.getDefaultList()).addMemberId(member.getId()).findMembershipResult();
152
153 int foundCount = 0;
154
155 for (MembershipSubjectContainer membershipSubjectContainer : membershipResult.getMembershipSubjectContainers()) {
156
157 CustomUiGrouper.this.membershipGroupNameSourceIdSubjectIdToGroupMap.put(
158 new MultiKey(membershipSubjectContainer.getGroupOwner().getName(), subject.getSourceId(), subject.getId()),
159 membershipSubjectContainer.getGroupOwner());
160 foundCount++;
161 }
162
163 CustomUiGrouper.this.debugMapPut(stemExtension + "_cacheMemStemCount", foundCount);
164 return null;
165 }
166
167 });
168 }
169 } catch (RuntimeException re) {
170
171 this.debugMapPut("cacheMemStemError", GrouperUtil.getFullStackTrace(re));
172 throw re;
173
174 } finally {
175 this.debugMapPut("cacheMemStemMillis", (System.nanoTime()-startedNanos)/1000000);
176 }
177
178
179 }
180
181
182
183
184 private Map<String, Group> groupIdAndNameToGroup = new HashMap<String, Group>();
185
186
187
188
189
190 public void cacheGroups(List<MultiKey> groupIdsAndNames) {
191 if (GrouperUtil.length(groupIdsAndNames) == 0) {
192 return;
193 }
194
195 long startedNanos = System.nanoTime();
196
197 try {
198 for (MultiKey groupIdAndName : GrouperUtil.nonNull(groupIdsAndNames)) {
199
200 String groupId = (String)groupIdAndName.getKey(0);
201 String groupName = (String)groupIdAndName.getKey(1);
202
203 if (StringUtils.isBlank(groupId) && StringUtils.isBlank(groupName)) {
204 continue;
205 }
206 Group group = null;
207 if (!StringUtils.isBlank(groupId)) {
208 group = GroupFinder.findByUuid(GrouperSession.staticGrouperSession(), groupId, false);
209 } else if (!StringUtils.isBlank(groupName)) {
210 group = GroupFinder.findByName(GrouperSession.staticGrouperSession(), groupName, false);
211 }
212 if (group != null) {
213 groupIdAndNameToGroup.put(group.getId(), group);
214 groupIdAndNameToGroup.put(group.getName(), group);
215 }
216
217 }
218
219 this.debugMapPut("groupsFound", groupIdAndNameToGroup.size());
220
221 } catch (RuntimeException re) {
222
223 this.debugMapPut("cacheGroupError", GrouperUtil.getFullStackTrace(re));
224 throw re;
225
226 } finally {
227 this.debugMapPut("cacheGroupMillis", (System.nanoTime()-startedNanos)/1000000);
228 }
229 }
230
231
232
233
234
235
236
237 public void cacheMembershipsInGroups(final Set<String> groupNames, final Subject subject) {
238
239 long startedNanos = System.nanoTime();
240
241 try {
242
243 this.debugMapPut("cacheMemGroupsToCheck", GrouperUtil.length(groupNames));
244
245 GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
246
247 public Object callback(GrouperSession grouperSession)
248 throws GrouperSessionException {
249
250 Member member = MemberFinder.findBySubject(grouperSession, subject, true);
251
252 MembershipFinderpFinder.html#MembershipFinder">MembershipFinder membershipFinder = new MembershipFinder().assignEnabled(true)
253 .addField(Group.getDefaultList()).addMemberId(member.getId());
254
255 for (String groupName : groupNames) {
256 membershipFinder.addGroup(groupName);
257 }
258
259 MembershipResult membershipResult = membershipFinder.findMembershipResult();
260
261 int foundCount = 0;
262
263 for (MembershipSubjectContainer membershipSubjectContainer : membershipResult.getMembershipSubjectContainers()) {
264
265 CustomUiGrouper.this.membershipGroupNameSourceIdSubjectIdToGroupMap.put(
266 new MultiKey(membershipSubjectContainer.getGroupOwner().getName(), subject.getSourceId(), subject.getId()),
267 membershipSubjectContainer.getGroupOwner());
268 foundCount++;
269
270 }
271
272 CustomUiGrouper.this.debugMapPut("cacheMemGroupsCount", foundCount);
273
274 return null;
275 }
276
277 });
278
279 } catch (RuntimeException re) {
280
281 this.debugMapPut("cacheMemGroupsError", GrouperUtil.getFullStackTrace(re));
282 throw re;
283
284 } finally {
285 this.debugMapPut("cacheMemGroupsMillis", (System.nanoTime()-startedNanos)/1000000);
286 }
287
288
289 }
290
291
292
293
294
295
296
297 public boolean hasGrouperMembership(final Group group, final Subject subject) {
298
299 try {
300 return (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
301
302 public Object callback(GrouperSession grouperSession)
303 throws GrouperSessionException {
304 return group.hasMember(subject);
305 }
306
307 });
308
309 } catch (RuntimeException re) {
310
311 this.debugMapPut("hasGrouperMship", GrouperUtil.getFullStackTrace(re));
312 throw re;
313
314 }
315
316 }
317
318
319
320
321
322
323
324
325 public boolean hasDirectGrouperGroupPrivilege(final Group group, final Subject subject, final String fieldName) {
326
327 long startedNanos = System.nanoTime();
328
329 try {
330 return (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
331
332 public Object callback(GrouperSession grouperSession)
333 throws GrouperSessionException {
334
335 return group.hasPrivilege(subject, fieldName);
336
337 }
338
339 });
340
341 } catch (RuntimeException re) {
342
343 this.debugMapPut("hasPrivilege", GrouperUtil.getFullStackTrace(re));
344 throw re;
345
346 } finally {
347 this.debugMapPut("hasPrivTookMillis", (System.nanoTime()-startedNanos)/1000000);
348 }
349
350 }
351
352
353
354
355
356
357
358
359 public boolean canHaveGroupPrivilege(final Group group, final Subject subject, final String fieldName) {
360 long startedNanos = System.nanoTime();
361
362 try {
363 return (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
364
365 public Object callback(GrouperSession grouperSession)
366 throws GrouperSessionException {
367
368 return group.canHavePrivilege(subject, fieldName, false);
369
370 }
371
372 });
373
374 } catch (RuntimeException re) {
375
376 this.debugMapPut("canHavePrivError", GrouperUtil.getFullStackTrace(re));
377 throw re;
378
379 } finally {
380 this.debugMapPut("canHavePrivTookMillis", (System.nanoTime()-startedNanos)/1000000);
381 }
382
383 }
384
385
386
387
388
389
390
391
392 public boolean canHaveStemPrivilege(final Stem stem, final Subject subject, final String fieldName) {
393 long startedNanos = System.nanoTime();
394
395 try {
396 return (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
397
398 public Object callback(GrouperSession grouperSession)
399 throws GrouperSessionException {
400
401 return stem.canHavePrivilege(subject, fieldName, false);
402
403 }
404
405 });
406
407 } catch (RuntimeException re) {
408
409 this.debugMapPut("canHavePrivError", GrouperUtil.getFullStackTrace(re));
410 throw re;
411
412 } finally {
413 this.debugMapPut("canHavePrivTookMillis", (System.nanoTime()-startedNanos)/1000000);
414 }
415
416 }
417
418
419
420
421 private CustomUiEngine customUiEngine;
422
423
424
425
426 private Map<String, Stem> stemIdAndNameToStem = new HashMap<String, Stem>();
427
428
429
430
431 private Map<String, AttributeDef> attributeDefIdAndNameToAttributeDef = new HashMap<String, AttributeDef>();
432
433
434
435
436 public CustomUiGrouper() {
437 }
438
439
440
441
442
443 public CustomUiEngine getCustomUiEngine() {
444 return this.customUiEngine;
445 }
446
447
448
449
450
451 public void setCustomUiEngine(CustomUiEngine customUiEngine) {
452 this.customUiEngine = customUiEngine;
453 }
454
455
456
457
458
459
460
461 public boolean canHaveAttributeDefPrivilege(final AttributeDef attributeDef, final Subject subject, final String fieldName) {
462 long startedNanos = System.nanoTime();
463
464 try {
465 return (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
466
467 public Object callback(GrouperSession grouperSession)
468 throws GrouperSessionException {
469
470 return attributeDef.getPrivilegeDelegate().canHavePrivilege(subject, fieldName, false);
471
472 }
473
474 });
475
476 } catch (RuntimeException re) {
477
478 this.debugMapPut("canHavePrivError", GrouperUtil.getFullStackTrace(re));
479 throw re;
480
481 } finally {
482 this.debugMapPut("canHavePrivTookMillis", (System.nanoTime()-startedNanos)/1000000);
483 }
484
485 }
486
487
488
489
490
491
492
493 public boolean hasDirectGrouperStemPrivilege(final Stem stem, final Subject subject, final String fieldName) {
494
495 long startedNanos = System.nanoTime();
496
497 try {
498 return (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
499
500 public Object callback(GrouperSession grouperSession)
501 throws GrouperSessionException {
502
503 return stem.hasPrivilege(subject, fieldName);
504
505 }
506
507 });
508
509 } catch (RuntimeException re) {
510
511 this.debugMapPut("hasPrivilege", GrouperUtil.getFullStackTrace(re));
512 throw re;
513
514 } finally {
515 this.debugMapPut("hasPrivTookMillis", (System.nanoTime()-startedNanos)/1000000);
516 }
517
518 }
519
520
521
522
523
524
525
526 public boolean hasDirectGrouperAttributeDefPrivilege(final AttributeDef attributeDef, final Subject subject, final String fieldName) {
527
528 long startedNanos = System.nanoTime();
529
530 try {
531 return (Boolean)GrouperSession.internal_callbackRootGrouperSession(new GrouperSessionHandler() {
532
533 public Object callback(GrouperSession grouperSession)
534 throws GrouperSessionException {
535
536 return attributeDef.getPrivilegeDelegate().hasPrivilege(subject, fieldName);
537
538 }
539
540 });
541
542 } catch (RuntimeException re) {
543
544 this.debugMapPut("hasPrivilege", GrouperUtil.getFullStackTrace(re));
545 throw re;
546
547 } finally {
548 this.debugMapPut("hasPrivTookMillis", (System.nanoTime()-startedNanos)/1000000);
549 }
550
551 }
552
553
554
555
556
557
558 public void cacheStems(List<MultiKey> stemIdsAndNames) {
559 if (GrouperUtil.length(stemIdsAndNames) == 0) {
560 return;
561 }
562 long startedNanos = System.nanoTime();
563
564 try {
565 for (MultiKey stemIdAndName : GrouperUtil.nonNull(stemIdsAndNames)) {
566
567 String stemId = (String)stemIdAndName.getKey(0);
568 String stemName = (String)stemIdAndName.getKey(1);
569
570 if (StringUtils.isBlank(stemId) && StringUtils.isBlank(stemName)) {
571 continue;
572 }
573 Stem stem = null;
574 if (!StringUtils.isBlank(stemId)) {
575 stem = StemFinder.findByUuid(GrouperSession.staticGrouperSession(), stemId, false);
576 } else if (!StringUtils.isBlank(stemName)) {
577 stem = StemFinder.findByName(GrouperSession.staticGrouperSession(), stemName, false);
578 }
579 if (stem != null) {
580 stemIdAndNameToStem.put(stem.getId(), stem);
581 stemIdAndNameToStem.put(stem.getName(), stem);
582 }
583
584 }
585
586 this.debugMapPut("stemsFound", stemIdAndNameToStem.size());
587
588 } catch (RuntimeException re) {
589
590 this.debugMapPut("cacheStemError", GrouperUtil.getFullStackTrace(re));
591 throw re;
592
593 } finally {
594 this.debugMapPut("cacheStemMillis", (System.nanoTime()-startedNanos)/1000000);
595 }
596 }
597
598
599
600
601
602
603 public void cacheAttributeDefs(List<MultiKey> attributeDefIdsAndNames) {
604
605 if (GrouperUtil.length(attributeDefIdsAndNames) == 0) {
606 return;
607 }
608
609 long startedNanos = System.nanoTime();
610
611 try {
612 for (MultiKey attributeDefIdAndName : GrouperUtil.nonNull(attributeDefIdsAndNames)) {
613
614 String attributeDefId = (String)attributeDefIdAndName.getKey(0);
615 String nameOfAttributeDef = (String)attributeDefIdAndName.getKey(1);
616
617 if (StringUtils.isBlank(attributeDefId) && StringUtils.isBlank(nameOfAttributeDef)) {
618 continue;
619 }
620 AttributeDef attributeDef = null;
621 if (!StringUtils.isBlank(attributeDefId)) {
622 attributeDef = AttributeDefFinder.findById(attributeDefId, false);
623 } else if (!StringUtils.isBlank(nameOfAttributeDef)) {
624 attributeDef = AttributeDefFinder.findByName(nameOfAttributeDef, false);
625 }
626 if (attributeDef != null) {
627 attributeDefIdAndNameToAttributeDef.put(attributeDef.getId(), attributeDef);
628 attributeDefIdAndNameToAttributeDef.put(attributeDef.getName(), attributeDef);
629 }
630
631 }
632
633 this.debugMapPut("attributeDefsFound", attributeDefIdAndNameToAttributeDef.size());
634
635 } catch (RuntimeException re) {
636
637 this.debugMapPut("cacheAttributeDefError", GrouperUtil.getFullStackTrace(re));
638 throw re;
639
640 } finally {
641 this.debugMapPut("cacheattributeDefMillis", (System.nanoTime()-startedNanos)/1000000);
642 }
643 }
644
645 }