@Override
  public Iterator<IEntityGroup> findParentGroups(IGroupMember member) throws GroupsException {

    /*
     * This method has the potential to be called A LOT, especially if
     * there's a lot of portal data (portlets & groups).  It's important
     * not to waste time on nonsensical checks.
     */

    if (!IPERSON_CLASS.equals(member.getLeafType())) {
      // This is going to happen;  GaP code is not responsible for
      // knowing that PAGS only supports groups of IPerson (we are).
      return Collections.emptyIterator();
    }

    logger.debug("finding containing groups for member key {}", member.getKey());

    final Set<IEntityGroup> set = Collections.emptySet();
    Iterator<IEntityGroup> rslt = set.iterator(); // default

    if (member.isGroup()) {
      // PAGS groups may only contain other PAGS groups (and people, of course)
      final IEntityGroup ieg = (IEntityGroup) member;
      if (PagsService.SERVICE_NAME_PAGS.equals(ieg.getServiceName().toString())) {
        rslt = findParentGroupsForGroup((IEntityGroup) member);
      }
    } else {
      rslt = findParentGroupsForEntity((IEntity) member);
    }

    return rslt;
  }
  /* (non-Javadoc)
   * @see org.jasig.portal.groups.IEntityGroupStore#findEntitiesForGroup(org.jasig.portal.groups.IEntityGroup)
   */
  @SuppressWarnings("unchecked")
  public Iterator findEntitiesForGroup(IEntityGroup group) throws GroupsException {

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Searching Grouper for members of the group with key: " + group.getKey());
    }

    try {

      // execute a search for members of the specified group
      GcGetMembers getGroupsMembers = new GcGetMembers();
      getGroupsMembers.addGroupName(group.getLocalKey());
      getGroupsMembers.assignIncludeSubjectDetail(true);
      WsGetMembersResults results = getGroupsMembers.execute();

      if (results == null
          || results.getResults() == null
          || results.getResults().length == 0
          || results.getResults()[0].getWsSubjects() == null) {
        LOGGER.debug("No members found for Grouper group with key " + group.getLocalKey());
        return Collections.<IGroupMember>emptyList().iterator();
      }

      WsSubject[] gInfos = results.getResults()[0].getWsSubjects();
      final List<IGroupMember> members = new ArrayList<IGroupMember>(gInfos.length);

      // add each result to the member list
      for (WsSubject gInfo : gInfos) {

        // if the member is not a group (aka person)
        if (!StringUtils.equals(gInfo.getSourceId(), "g:gsa")) {

          if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(
                "creating leaf member:"
                    + gInfo.getId()
                    + " and name: "
                    + gInfo.getName()
                    + " from group: "
                    + group.getLocalKey());
          }
          // use the name instead of id as it shows better in the display
          IGroupMember member = new EntityImpl(gInfo.getName(), IPerson.class);
          members.add(member);
        }
      }

      // return an iterator for the assembled group
      return members.iterator();

    } catch (Exception e) {
      LOGGER.warn(
          "Exception while attempting to retrieve "
              + "member entities of group with key "
              + group.getKey()
              + " from Grouper web services: "
              + e.getMessage());
      return Collections.<IGroupMember>emptyList().iterator();
    }
  }
  /**
   * Construct an IEntityGroup from a Grouper WsGroup.
   *
   * @param wsGroup
   * @return the group
   */
  protected IEntityGroup createUportalGroupFromGrouperGroup(WsGroup wsGroup) {
    IEntityGroup iEntityGroup = new EntityGroupImpl(wsGroup.getName(), IPerson.class);

    // need to set the group name and description to the actual
    // display name and description
    iEntityGroup.setName(wsGroup.getDisplayName());
    iEntityGroup.setDescription(wsGroup.getDescription());
    return iEntityGroup;
  }
  @SuppressWarnings("unchecked")
  public Iterator findMemberGroups(IEntityGroup group) throws GroupsException {
    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("Searching for group-type members of group with key: " + group.getKey());
    }

    try {

      if (!validKey(group.getLocalKey())) {
        return Collections.<IEntityGroup>emptyList().iterator();
      }

      GcGetMembers gcGetMembers = new GcGetMembers();
      gcGetMembers.addGroupName(group.getLocalKey());
      gcGetMembers.assignIncludeSubjectDetail(true);
      gcGetMembers.addSourceId("g:gsa");

      WsGetMembersResults results = gcGetMembers.execute();

      if (results == null
          || results.getResults() == null
          || results.getResults().length == 0
          || results.getResults()[0].getWsSubjects() == null) {
        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("No group-type members found for group with key " + group.getKey());
        }
        return Collections.<IEntityGroup>emptyList().iterator();
      }

      final List<IEntityGroup> members = new ArrayList<IEntityGroup>();
      WsSubject[] subjects = results.getResults()[0].getWsSubjects();

      for (WsSubject wsSubject : subjects) {
        if (validKey(wsSubject.getName())) {
          WsGroup wsGroup = findGroupFromKey(wsSubject.getName());
          if (wsGroup != null) {
            IEntityGroup member = createUportalGroupFromGrouperGroup(wsGroup);
            members.add(member);
            if (LOGGER.isTraceEnabled()) {
              LOGGER.trace("found IEntityGroup member: " + member);
            }
          }
        }
      }

      return members.iterator();

    } catch (Exception e) {
      LOGGER.warn(
          "Exception while attempting to retrieve "
              + "member groups of group with key "
              + group.getKey()
              + " from Grouper web services: "
              + e.getMessage());
      return Collections.<IGroupMember>emptyList().iterator();
    }
  }
  /**
   * @see
   *     org.jasig.portal.groups.IEntityGroupStore#updateMembers(org.jasig.portal.groups.IEntityGroup)
   */
  public void updateMembers(IEntityGroup group) throws GroupsException {

    // assume key is fully qualified group name
    String groupName = group.getLocalKey();

    GcAddMember gcAddMember = new GcAddMember().assignGroupName(groupName);

    for (IGroupMember iGroupMember : group.getChildren()) {
      EntityIdentifier entityIdentifier = iGroupMember.getEntityIdentifier();
      String identifier = entityIdentifier.getKey();
      gcAddMember.addSubjectIdentifier(identifier);
    }
    gcAddMember.execute();
  }
  private IEntityGroup convertPagsGroupToEntity(IPersonAttributesGroupDefinition group) {

    final String cacheKey = group.getName();
    Element element = entityGroupCache.get(cacheKey);

    if (element == null) {
      final IEntityGroup entityGroup = new EntityTestingGroupImpl(group.getName(), IPERSON_CLASS);
      entityGroup.setName(group.getName());
      entityGroup.setDescription(group.getDescription());
      element = new Element(cacheKey, entityGroup);
      entityGroupCache.put(element);
    }

    return (IEntityGroup) element.getObjectValue();
  }
  /* (non-Javadoc)
   * @see org.jasig.portal.groups.IEntityGroupStore#findMemberGroupKeys(org.jasig.portal.groups.IEntityGroup)
   */
  @SuppressWarnings("unchecked")
  public String[] findMemberGroupKeys(IEntityGroup group) throws GroupsException {

    // first the get an iterator for the member groups
    final Iterator<IEntityGroup> it = findMemberGroups(group);

    // construct a list of group keys from this iterator
    List<String> keys = new ArrayList<String>();
    while (it.hasNext()) {
      IEntityGroup eg = it.next();
      keys.add(eg.getKey());
    }

    // return an iterator over the assembled list
    return keys.toArray(new String[keys.size()]);
  }
Exemplo n.º 8
0
  public Entity getEntity(String entityType, String entityId, boolean populateChildren) {

    // get the EntityEnum for the specified entity type
    EntityEnum entityEnum = EntityEnum.getEntityEnum(entityType);

    // if the entity type is a group, use the group service's findGroup method
    // to locate it
    if (entityEnum.isGroup()) {
      // attempt to find the entity
      IEntityGroup entityGroup = GroupService.findGroup(entityId);
      if (entityGroup == null) {
        return null;
      } else {
        Entity entity = EntityFactory.createEntity(entityGroup, entityEnum);
        if (populateChildren) {
          @SuppressWarnings("unchecked")
          Iterator<IGroupMember> members = (Iterator<IGroupMember>) entityGroup.getMembers();
          entity = populateChildren(entity, members);
        }
        IAuthorizationPrincipal authP = getPrincipalForEntity(entity);
        Principal principal = new PrincipalImpl(authP.getKey(), authP.getPrincipalString());

        entity.setPrincipal(principal);
        return entity;
      }
    }

    // otherwise use the getGroupMember method
    else {
      IGroupMember groupMember = GroupService.getGroupMember(entityId, entityEnum.getClazz());
      if (groupMember == null || groupMember instanceof IEntityGroup) {
        return null;
      }
      Entity entity = EntityFactory.createEntity(groupMember, entityEnum);

      // the group member interface doesn't include the entity name, so
      // we'll need to look that up manually
      entity.setName(lookupEntityName(entity));
      if (EntityEnum.GROUP.toString().equals(entity.getEntityType())
          || EntityEnum.PERSON.toString().equals(entity.getEntityType())) {
        IAuthorizationPrincipal authP = getPrincipalForEntity(entity);
        Principal principal = new PrincipalImpl(authP.getKey(), authP.getPrincipalString());
        entity.setPrincipal(principal);
      }
      return entity;
    }
  }
  /**
   * Decorates a group if needed.
   *
   * @param group The group to decorate.
   * @return The decorated group if some grouper grouper groups have to be loaded in it.
   */
  private IEntityGroup decorate(final IEntityGroup group) {

    if (group == null) {
      return group;
    }

    final String groupName = group.getName();
    String eltsToLoad = null;

    if (groupName != null) {
      eltsToLoad = grouperMapping.getProperty(groupName);
    }

    // Retrieves the name of the Grouper groups to load.
    String[] grouperGroups = null;

    if (eltsToLoad != null) {
      grouperGroups = eltsToLoad.split(ESCOConstants.SEP);
    }
    final EntityGroupImpl decoratedGroup =
        new ESCODynEntityGroupDecorator(
            group.getKey(),
            group.getLeafType(),
            group.getCreatorID(),
            group.getName(),
            group.getDescription(),
            grouperGroups);

    return decoratedGroup;
  }
  @Override
  public Iterator<IEntityGroup> findMemberGroups(IEntityGroup group) throws GroupsException {

    /*
     * The GaP system prevents this method from being called with a nn-PAGS group.
     */

    IPersonAttributesGroupDefinition pagsGroup = getPagsGroupDefByName(group.getName());
    List<IEntityGroup> results = new ArrayList<IEntityGroup>();
    for (IPersonAttributesGroupDefinition member : pagsGroup.getMembers()) {
      results.add(convertPagsGroupToEntity(member));
    }
    return results.iterator();
  }
  /** @see org.jasig.portal.groups.IEntityGroupStore#update(org.jasig.portal.groups.IEntityGroup) */
  public void update(IEntityGroup group) throws GroupsException {

    // assume key is fully qualified group name
    String groupName = group.getLocalKey();

    String description = group.getDescription();

    // the name is the displayExtension
    String displayExtension = group.getName();

    WsGroupToSave wsGroupToSave = new WsGroupToSave();
    wsGroupToSave.setCreateParentStemsIfNotExist("T");
    wsGroupToSave.setWsGroupLookup(new WsGroupLookup(groupName, null));
    WsGroup wsGroup = new WsGroup();
    wsGroup.setName(groupName);
    wsGroup.setDisplayExtension(displayExtension);
    wsGroup.setDescription(description);
    wsGroupToSave.setWsGroup(wsGroup);

    new GcGroupSave().addGroupToSave(wsGroupToSave).execute();

    updateMembers(group);
  }
  /* (non-Javadoc)
   * @see org.jasig.portal.groups.IEntityGroupStore#find(java.lang.String)
   */
  public IEntityGroup find(String key) throws GroupsException {

    try {

      // Search the Grouper server for groups with the specified local
      // key
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Searching Grouper for a direct match for key: " + key);
      }
      WsGroup wsGroup = findGroupFromKey(key);
      if (wsGroup == null) {
        return null;
      }
      IEntityGroup group = createUportalGroupFromGrouperGroup(wsGroup);

      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(
            "Retrieved group from the Grouper server matching key "
                + key
                + ": "
                + group.toString());
      }

      // return the group
      return group;

    } catch (Exception e) {
      LOGGER.warn(
          "Exception while attempting to retrieve "
              + "group with key "
              + key
              + " from Grouper web services: "
              + e.getMessage());
      return null;
    }
  }
  /* (non-Javadoc)
   * @see org.jasig.portal.groups.IEntityGroupStore#contains(org.jasig.portal.groups.IEntityGroup, org.jasig.portal.groups.IGroupMember)
   */
  public boolean contains(IEntityGroup group, IGroupMember member) throws GroupsException {

    String groupContainerName = group.getLocalKey();
    String groupMemberName = member.getKey();

    if (!validKey(groupContainerName) || !validKey(groupMemberName)) {
      return false;
    }

    GcHasMember gcHasMember = new GcHasMember();
    gcHasMember.assignGroupName(groupContainerName);
    gcHasMember.addSubjectLookup(new WsSubjectLookup(null, "g:gsa", groupMemberName));
    WsHasMemberResults wsHasMemberResults = gcHasMember.execute();
    if (GrouperClientUtils.length(wsHasMemberResults.getResults()) == 1) {
      WsHasMemberResult wsHasMemberResult = wsHasMemberResults.getResults()[0];
      return StringUtils.equals("IS_MEMBER", wsHasMemberResult.getResultMetadata().getResultCode());
    }
    return false;
  }
  @Override
  public boolean contains(IEntityGroup group, IGroupMember member) {

    /*
     * This method has the potential to be called A LOT, especially if
     * there's a lot of portal data (portlets & groups).  It's important
     * not to waste time on nonsensical checks.
     */

    if (!IPERSON_CLASS.equals(member.getLeafType())) {
      // Maybe this call to contains() shouldn't even happen, since
      // group.getLeafType() is (presumably) IPerson.class.
      return false;
    }

    if (member.isGroup()) {
      // PAGS groups may only contain other PAGS groups (and people, of course)
      final IEntityGroup ieg = (IEntityGroup) member;
      if (!PagsService.SERVICE_NAME_PAGS.equals(ieg.getServiceName().toString())) {
        return false;
      }
    }

    final MembershipCacheKey cacheKey =
        new MembershipCacheKey(group.getEntityIdentifier(), member.getEntityIdentifier());
    Element element = membershipCache.get(cacheKey);
    if (element == null) {

      logger.debug(
          "Checking if group {} contains member {}/{}",
          group.getName(),
          member.getKey(),
          member.getLeafType().getSimpleName());

      boolean answer = false; // default
      final PagsGroup groupDef = convertEntityToGroupDef(group);
      if (member.isGroup()) {
        final String key = ((IEntityGroup) member).getLocalKey();
        answer = groupDef.hasMember(key);
      } else {
        try {
          final IPersonAttributeDao pa = PersonAttributeDaoLocator.getPersonAttributeDao();
          final IPersonAttributes personAttributes = pa.getPerson(member.getKey());

          if (personAttributes != null) {
            final RestrictedPerson rp = PersonFactory.createRestrictedPerson();
            rp.setAttributes(personAttributes.getAttributes());
            answer = groupDef.contains(rp);
          }
        } catch (Exception ex) {
          logger.error(
              "Exception acquiring attributes for member "
                  + member
                  + " while checking if group "
                  + group
                  + " contains this member.",
              ex);
          return false;
        }
      }

      element = new Element(cacheKey, answer);
      membershipCache.put(element);
    }

    return (Boolean) element.getObjectValue();
  }
 @Override
 public EntityIdentifier[] searchForGroups(String query, int method, Class leaftype)
     throws GroupsException {
   if (leaftype != IPERSON_CLASS) {
     return EMPTY_SEARCH_RESULTS;
   }
   Set<IPersonAttributesGroupDefinition> pagsGroups =
       personAttributesGroupDefinitionDao.getPersonAttributesGroupDefinitions();
   List<EntityIdentifier> results = new ArrayList<EntityIdentifier>();
   switch (method) {
     case IS:
       for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
         IEntityGroup g = convertPagsGroupToEntity(pagsGroup);
         if (g.getName().equalsIgnoreCase(query)) {
           results.add(g.getEntityIdentifier());
         }
       }
       break;
     case STARTS_WITH:
       for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
         IEntityGroup g = convertPagsGroupToEntity(pagsGroup);
         if (g.getName().toUpperCase().startsWith(query.toUpperCase())) {
           results.add(g.getEntityIdentifier());
         }
       }
       break;
     case ENDS_WITH:
       for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
         IEntityGroup g = convertPagsGroupToEntity(pagsGroup);
         if (g.getName().toUpperCase().endsWith(query.toUpperCase())) {
           results.add(g.getEntityIdentifier());
         }
       }
       break;
     case CONTAINS:
       for (IPersonAttributesGroupDefinition pagsGroup : pagsGroups) {
         IEntityGroup g = convertPagsGroupToEntity(pagsGroup);
         if (g.getName().toUpperCase().indexOf(query.toUpperCase()) != -1) {
           results.add(g.getEntityIdentifier());
         }
       }
       break;
   }
   return results.toArray(new EntityIdentifier[] {});
 }
 private Iterator<IEntityGroup> findParentGroupsForGroup(IEntityGroup group) {
   logger.debug(
       "Finding containing groups for group {} (key {})", group.getName(), group.getKey());
   Set<IEntityGroup> parents = getParentGroups(group.getName(), new HashSet<IEntityGroup>());
   return parents.iterator();
 }
  /** @see org.jasig.portal.groups.IEntityGroupStore#delete(org.jasig.portal.groups.IEntityGroup) */
  public void delete(IEntityGroup group) throws GroupsException {

    String groupName = group.getLocalKey();
    new GcGroupDelete().addGroupLookup(new WsGroupLookup(groupName, null)).execute();
  }
 private PagsGroup convertEntityToGroupDef(IEntityGroup group) {
   IPersonAttributesGroupDefinition pagsGroup = getPagsGroupDefByName(group.getName());
   return initGroupDef(pagsGroup);
 }