private Group getPeerGroupOf(
     Group group, AuthorizableManager authorizableManager, Map<String, Object> toSave)
     throws AccessDeniedException, StorageClientException {
   Group peerGroup = null;
   if (group.hasProperty(UserConstants.PROP_MANAGERS_GROUP)) {
     String managersGroupId = (String) group.getProperty(UserConstants.PROP_MANAGERS_GROUP);
     if (group.getId().equals(managersGroupId)) {
       return group;
     }
     peerGroup = (Group) toSave.get(managersGroupId);
     if (peerGroup == null) {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(
             "For {} Not in toSave List loading Managers Group from store {} ",
             group.getId(),
             managersGroupId);
       }
       peerGroup = (Group) authorizableManager.findAuthorizable(managersGroupId);
     } else {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(
             "For {} got Managers Group from save list {} ", group.getId(), managersGroupId);
       }
     }
   } else if (group.hasProperty(UserConstants.PROP_MANAGED_GROUP)) {
     String managedGroupId = (String) group.getProperty(UserConstants.PROP_MANAGED_GROUP);
     if (group.getId().equals(managedGroupId)) {
       return group;
     }
     peerGroup = (Group) toSave.get(managedGroupId);
     if (peerGroup == null) {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(
             "For {} Not in toSave List loading Managed Group from store {} ",
             group.getId(),
             managedGroupId);
       }
       peerGroup = (Group) authorizableManager.findAuthorizable(managedGroupId);
     } else {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(
             "For {} got Managed Group from save list {} ", group.getId(), managedGroupId);
       }
     }
   }
   return peerGroup;
 }
  @Override
  protected TreeMap<String, Group> getGroups(
      Authorizable member, AuthorizableManager userManager, SlingHttpServletRequest request)
      throws StorageClientException, AccessDeniedException {
    TreeMap<String, Group> managedGroups = new TreeMap<String, Group>();
    Iterator<Group> allGroupsIter = member.memberOf(userManager);
    for (String principal : member.getPrincipals()) {
      Group group = (Group) userManager.findAuthorizable(principal);
      if (group != null && !group.getId().equals(Group.EVERYONE)) {

        boolean isManager = false;

        if (isPseudoGroup(group) && isManagerGroup(group, userManager)) {
          // The group we want is the child of the pseudo group
          isManager = true;
          group =
              (Group)
                  userManager.findAuthorizable(
                      (String) group.getProperty(UserConstants.PROP_PSEUDO_GROUP_PARENT));
        } else {
          for (String managerId :
              StorageClientUtils.nonNullStringArray(
                  (String[]) group.getProperty(UserConstants.PROP_GROUP_MANAGERS))) {
            if (member.getId().equals(managerId)) {
              isManager = true;
              break;
            }
          }
        }

        if (isManager) {
          final String category = stringRequestParameter(request, "category", null);
          if (category == null) { // no filtering
            managedGroups.put(group.getId(), group);
          } else { // KERN-1865 category filter
            if (category.equals(group.getProperty("sakai:category"))) {
              managedGroups.put(group.getId(), group);
            }
          }
        }
      }
    }

    return managedGroups;
  }
  @Before
  public void setUp() throws Exception {
    provider = new MyRelatedGroupsPropertyProvider(searchServiceFactory);
    when(request.getRemoteUser()).thenReturn("user1");

    when(repo.loginAdministrative()).thenReturn(session);
    when(session.getAuthorizableManager()).thenReturn(authMgr);
    when(authMgr.findAuthorizable("user1")).thenReturn(auth1);

    Group group1 = mock(Group.class);
    when(group1.getId()).thenReturn("group1");
    when(group1.getProperty(GROUP_TITLE_PROPERTY)).thenReturn("Group 1 Test");
    when(group1.getProperty("sakai:tag-uuid")).thenReturn(new String[] {"123-456"});

    when(auth1.memberOf(authMgr)).thenReturn(Sets.newHashSet(group1).iterator());

    when(searchServiceFactory.getSearchResultSet(eq(request), any(Query.class))).thenReturn(rs);
  }
  /**
   * @param request The request that contains the authorizables.
   * @param group The group that should be modified.
   * @param propertyName The name of the property on the group where the authorizable IDs should be
   *     added/deleted.
   * @param paramName The name of the parameter that contains the authorizable IDs. ie: :manager or
   *     :viewer. If :manager is specified, :manager@Delete will be used for deletes.
   * @param extraPropertyValues An array of authorizable IDs that should be added as well.
   * @param toSave
   * @throws RepositoryException
   */
  protected void handleAuthorizablesOnProperty(
      SlingHttpServletRequest request,
      Group group,
      String propertyName,
      String paramName,
      String[] extraPropertyValues,
      Map<String, Object> toSave) {
    Set<String> propertyValueSet = new HashSet<String>();

    if (group.hasProperty(propertyName)) {
      String[] existingProperties = (String[]) group.getProperty(propertyName);
      for (String property : existingProperties) {
        propertyValueSet.add(property);
      }
    }

    boolean changed = false;

    // Remove all the managers that are in the :manager@Delete request parameter.
    String[] propertiesToDelete =
        request.getParameterValues(paramName + SlingPostConstants.SUFFIX_DELETE);
    if (propertiesToDelete != null) {
      for (String propertyToDelete : propertiesToDelete) {
        propertyValueSet.remove(propertyToDelete);
        changed = true;
      }
    }

    // Add the new ones (if any)
    String[] proeprtiesToAdd = request.getParameterValues(paramName);
    if (proeprtiesToAdd != null) {
      for (String propertyToAdd : proeprtiesToAdd) {
        propertyValueSet.add(propertyToAdd);
        changed = true;
      }
    }

    // Add the extra ones (if any.)
    if (extraPropertyValues != null) {
      for (String propertyValue : extraPropertyValues) {
        propertyValueSet.add(propertyValue);
        changed = true;
      }
    }

    // Write the property.
    if (changed) {
      group.setProperty(
          propertyName, propertyValueSet.toArray(new String[propertyValueSet.size()]));
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Adding to save Queue {} {}", group.getId(), group.getSafeProperties());
      }
      toSave.put(group.getId(), group);
    }
  }