public int countEvalGroupsForUser(String userId, String permission) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("userId: " + userId + ", permission: " + permission);
    }

    int count = externalLogic.countEvalGroupsForUser(userId, permission);

    // also check the adhoc groups
    //    taking this out for now because we do not want to allow adhoc groups to provide permission
    // to create templates/evals
    //    List<EvalAdhocGroup> adhocGroups = adhocSupportLogic.getAdhocGroupsForOwner(userId);
    //    count += adhocGroups.size();

    // also check provider
    if (evalGroupsProvider != null) {
      if (EvalConstants.PERM_BE_EVALUATED.equals(permission)
          || EvalConstants.PERM_TAKE_EVALUATION.equals(permission)
          || EvalConstants.PERM_ASSISTANT_ROLE.equals(permission)) {
        LOG.debug("Using eval groups provider: userId: " + userId + ", permission: " + permission);
        count +=
            evalGroupsProvider.countEvalGroupsForUser(
                userId, EvalExternalLogicImpl.translatePermission(permission));
      }
    }

    if (LOG.isDebugEnabled()) {
      LOG.debug("userId: " + userId + ", permission: " + permission + ", count: " + count);
    }
    return count;
  }
  @SuppressWarnings("rawtypes")
  public List<EvalGroup> getEvalGroupsForUser(String userId, String permission) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("userId: " + userId + ", permission: " + permission);
    }

    List<EvalGroup> l = new ArrayList<>();

    // get the groups from external
    l.addAll(externalLogic.getEvalGroupsForUser(userId, permission));

    // also check the internal groups
    List<EvalAdhocGroup> adhocGroups =
        adhocSupportLogic.getAdhocGroupsByUserAndPerm(userId, permission);
    for (EvalAdhocGroup adhocGroup : adhocGroups) {
      l.add(
          new EvalGroup(
              adhocGroup.getEvalGroupId(), adhocGroup.getTitle(), EvalConstants.GROUP_TYPE_ADHOC));
    }

    // also check provider
    if (evalGroupsProvider != null) {
      if (EvalConstants.PERM_BE_EVALUATED.equals(permission)
          || EvalConstants.PERM_TAKE_EVALUATION.equals(permission)
          || EvalConstants.PERM_ASSIGN_EVALUATION.equals(permission)
          || EvalConstants.PERM_ASSISTANT_ROLE.equals(permission)) {
        LOG.debug("Using eval groups provider: userId: " + userId + ", permission: " + permission);
        List eg =
            evalGroupsProvider.getEvalGroupsForUser(
                userId, EvalExternalLogicImpl.translatePermission(permission));
        for (Iterator iter = eg.iterator(); iter.hasNext(); ) {
          EvalGroup c = (EvalGroup) iter.next();
          c.type = EvalConstants.GROUP_TYPE_PROVIDED;
          l.add(c);
        }
      }
    }

    if (l.isEmpty()) {
      LOG.debug("Empty list of groups for user:"******", permission: " + permission);
    }
    return l;
  }
  public boolean isUserAllowedInEvalGroup(String userId, String permission, String evalGroupId) {

    /* NOTE: false checks end up being really costly and should probably be cached
     */

    if (evalGroupId == null) {
      // special check for the admin user
      return isUserAdmin(userId);
    }

    // try checking external first
    if (externalLogic.isUserAllowedInEvalGroup(userId, permission, evalGroupId)) {
      return true;
    }

    // check the internal groups next
    if (adhocSupportLogic.isUserAllowedInAdhocGroup(userId, permission, evalGroupId)) {
      return true;
    }

    // finally check provider
    if (evalGroupsProvider != null) {
      if (EvalConstants.PERM_BE_EVALUATED.equals(permission)
          || EvalConstants.PERM_TAKE_EVALUATION.equals(permission)
          || EvalConstants.PERM_ASSISTANT_ROLE.equals(permission)) {
        LOG.debug(
            "Using eval groups provider: userId: "
                + userId
                + ", permission: "
                + permission
                + ", evalGroupId: "
                + evalGroupId);
        if (evalGroupsProvider.isUserAllowedInGroup(
            userId, EvalExternalLogicImpl.translatePermission(permission), evalGroupId)) {
          return true;
        }
      }
    }

    return false;
  }
  /* (non-Javadoc)
   *
   * @see org.sakaiproject.evaluation.logic.externals.ExternalEvalGroups#getUserIdsForEvalGroup(java.lang.String, java.lang.String, java.lang.Boolean)
   */
  public Set<String> getUserIdsForEvalGroup(
      String evalGroupID, String permission, Boolean sectionAware) {
    Set<String> userIDs = new HashSet<>();
    if (sectionAware) {
      userIDs.addAll(externalLogic.getUserIdsForEvalGroup(evalGroupID, permission, sectionAware));
    }

    // If it's not section aware, or if we didn't find anything from external logic, do the normal
    // lookup call
    if (!sectionAware || userIDs.isEmpty()) {
      // Strip out the '/section/<section_id>' part of the evalGroupID if its there
      if (!sectionAware && evalGroupID.contains(EvalConstants.GROUP_ID_SECTION_PREFIX)) {
        evalGroupID =
            evalGroupID.substring(0, evalGroupID.indexOf(EvalConstants.GROUP_ID_SECTION_PREFIX));
      }

      /* NOTE: we are assuming there is not much chance that there will be some users stored in
       * multiple data stores for the same group id so we only check until we find at least one user,
       * this means checks for user in groups with no users in them end up being really costly
       */

      // Check external
      userIDs.addAll(externalLogic.getUserIdsForEvalGroup(evalGroupID, permission, sectionAware));

      // Only go on to check the internal adhocs if nothing was found
      if (userIDs.isEmpty()) {
        // Check internal adhoc groups
        if (EvalConstants.PERM_BE_EVALUATED.equals(permission)
            || EvalConstants.PERM_TAKE_EVALUATION.equals(permission)) {
          Long id = EvalAdhocGroup.getIdFromAdhocEvalGroupId(evalGroupID);
          if (id != null) {
            EvalAdhocGroup adhocGroup = adhocSupportLogic.getAdhocGroupById(id);
            if (adhocGroup != null) {
              String[] ids = null;
              if (EvalConstants.PERM_BE_EVALUATED.equals(permission)) {
                ids = adhocGroup.getEvaluateeIds();
              } else if (EvalConstants.PERM_TAKE_EVALUATION.equals(permission)) {
                ids = adhocGroup.getParticipantIds();
              }
              if (ids != null) {
                userIDs.addAll(Arrays.asList(ids));
              }
            }
          }
        }
      }

      // Check the provider if we still found nothing
      if (userIDs.isEmpty()) {
        // Also check provider
        if (evalGroupsProvider != null) {
          if (EvalConstants.PERM_BE_EVALUATED.equals(permission)
              || EvalConstants.PERM_TAKE_EVALUATION.equals(permission)
              || EvalConstants.PERM_ASSISTANT_ROLE.equals(permission)) {
            LOG.debug(
                "Using eval groups provider: evalGroupId: "
                    + evalGroupID
                    + ", permission: "
                    + permission);
            userIDs.addAll(
                evalGroupsProvider.getUserIdsForEvalGroups(
                    new String[] {evalGroupID},
                    EvalExternalLogicImpl.translatePermission(permission)));
          }
        }
      }
    }

    return userIDs;
  }