001: /* Copyright 2005 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.layout.dlm.providers;
007:
008: import org.jasig.portal.EntityIdentifier;
009: import org.jasig.portal.groups.GroupsException;
010: import org.jasig.portal.groups.IEntityGroup;
011: import org.jasig.portal.groups.IGroupConstants;
012: import org.jasig.portal.groups.IGroupMember;
013: import org.jasig.portal.layout.dlm.Evaluator;
014: import org.jasig.portal.security.IPerson;
015: import org.jasig.portal.services.GroupService;
016:
017: /**
018: * Answers isApplicable() in the affirmative if the user represented by the
019: * passed in IPerson is a member of the group whose name is passed to the
020: * constructor of this class.
021: *
022: * Added support for a 'deepMemberOf' mode, from 2.5 patches
023: * (UP-1284) and cache group key rather than the group itself
024: * (UP-1532). d.e 2006/12/19.
025: *
026: * @author mboyd@sungardsct.com
027: * @version $Revision: 36776 $ $Date: 2006-12-20 14:52:09 -0700 (Wed, 20 Dec 2006) $
028: * @since uPortal 2.5
029: */
030: public class GroupMembershipEvaluator implements Evaluator {
031: private static final int MEMBER_OF_MODE = 0;
032:
033: private static final int DEEP_MEMBER_OF_MODE = 1;
034:
035: private String groupName = null;
036:
037: private String groupKey = null;
038:
039: private final int evaluatorMode;
040:
041: public GroupMembershipEvaluator(String mode, String name) {
042: if (mode.equals("memberOf")) {
043: evaluatorMode = MEMBER_OF_MODE;
044: } else if (mode.equals("deepMemberOf")) {
045: evaluatorMode = DEEP_MEMBER_OF_MODE;
046: } else {
047: throw new RuntimeException(
048: "Unsupported mode '"
049: + mode
050: + "' specified. Only 'memberOf' and 'deepMemberOf' are "
051: + "supported at this time.");
052: }
053: this .groupName = name;
054: this .groupKey = getGroupKey();
055: }
056:
057: private String getGroupKey() {
058: EntityIdentifier[] groups = null;
059: try {
060: groups = GroupService.searchForGroups(groupName,
061: IGroupConstants.IS,
062: org.jasig.portal.security.IPerson.class);
063: } catch (GroupsException e1) {
064: throw new RuntimeException(
065: "An exception occurred searching for "
066: + "the group " + groupName + ".", e1);
067: }
068: if (groups == null || groups.length == 0)
069: throw new RuntimeException("Group with name '" + groupName
070: + "' not found for " + this .getClass().getName()
071: + ". All evaluations will return false.");
072:
073: return groups[0].getKey();
074: }
075:
076: private IEntityGroup getGroup(String key) {
077: try {
078: return GroupService.findGroup(key);
079: } catch (GroupsException e) {
080: throw new RuntimeException(
081: "An exception occurred retrieving " + "the group "
082: + groupName + ".", e);
083: }
084: }
085:
086: public boolean isApplicable(IPerson p) {
087: if (groupKey == null || p == null)
088: return false;
089:
090: IEntityGroup group = getGroup(groupKey);
091: EntityIdentifier ei = p.getEntityIdentifier();
092:
093: try {
094: IGroupMember groupMember = GroupService.getGroupMember(ei);
095: boolean isMember = false;
096:
097: if (evaluatorMode == MEMBER_OF_MODE) {
098: isMember = groupMember.isMemberOf(group);
099: } else {
100: isMember = groupMember.isDeepMemberOf(group);
101: }
102: return isMember;
103: } catch (GroupsException e) {
104: throw new RuntimeException("Unable to determine if user "
105: + p.getFullName() + " is in group " + groupName
106: + ".", e);
107: }
108: }
109: }
|