/**
   * Metodo que obtiene los ids de los grupos a los que pertenece el usuario
   *
   * @param conn
   * @param attributesUser
   * @param ldapDef
   * @param attributes
   * @param entidad
   * @return
   * @throws SecurityException
   */
  public List getListGroupOfUser(
      LdapConnection conn,
      LDAPAuthenticationUser attributesUser,
      LDAPDef ldapDef,
      String attributes[],
      String entidad)
      throws SecurityException {
    List result = new ArrayList();
    String attr[] = {attributes[0]};
    try {
      String group = null;
      String filter = null;
      LdapSearch search = null;
      LDAPAuthenticationUser attributesGroup = null;
      List groups = null;
      int scope =
          new Integer(
                  LDAPRBUtil.getInstance(null)
                      .getProperty(LDAP_SCOPEGROUP + ldapDef.getLdapEngine()))
              .intValue();
      groups = attributesUser.getGroupList();
      if (log.isDebugEnabled()) {
        log.debug("groups [" + groups + "] con el log [" + log + "]");
      }
      for (int i = 0; i < groups.size(); i++) {
        group = (String) groups.get(i);
        filter =
            LDAPRBUtil.getInstance(null)
                .getProperty(LDAP_SCOPE_BASESUBTREE_GROUP + ldapDef.getLdapEngine());
        search = new LdapSearch();
        search.initialize(conn, group, scope, filter, attr);
        search.execute();
        if (search.next()) {
          attributesGroup = getUserAttributes(search, attributes);
          attributesGroup.setGuidStringFormat(
              LdapBasicFns.formatGuid(conn, attributesGroup.getGuid()));

          Integer idGroup = getIdGroup(attributesGroup.getGuidStringFormat(), entidad);

          if (idGroup != null) {
            result.add(idGroup);
          }
        }
      }
    } catch (SecurityException e) {
      throw new SecurityException(SecurityException.ERROR_PASSWORD_INCORRECT);
    } catch (Exception e) {
      throw new SecurityException(SecurityException.ERROR_PASSWORD_INCORRECT);
    } finally {
      try {
        conn.close();
      } catch (Exception e) {
      }
    }
    return result;
  }
  public List getUserGroupGuidsIp(
      LdapConnection conn, String start, int scope, String filter, String attr[])
      throws SecurityException {
    List guids = null;
    String guid;
    LdapSearch search = null;
    String[] retAttrs = null;
    Object guidAv;

    retAttrs = attr;
    try {

      guids = new ArrayList();
      search = new LdapSearch();
      search.initialize(conn, start, scope, filter, retAttrs);
      search.execute();

      while (search.next()) {
        guidAv = search.getAttributeValue(retAttrs[0]);
        guid = LdapBasicFns.formatGuid(conn, guidAv);

        guids.add(guid);
      }

      search.release();

    } catch (Exception e) {
      throw new SecurityException(SecurityException.ERROR_CAN_NOT_FIND_USER_ATTRIBUTES_LDAP);
    }

    return guids;
  }
  public LdapSearch getSearchSSO(
      String dn, LdapConnection conn, LDAPDef ldapDef, String[] attributes)
      throws SecurityException {
    String filter = null;
    LdapSearch search = null;
    try {
      // busqueda por dn
      filter = LDAP_SCOPE_BASE_SSO;
      search = new LdapSearch();
      search.initialize(conn, dn, SearchControls.OBJECT_SCOPE, filter, attributes);
      search.execute();
      if (!search.next()) {
        throw new SecurityException(SecurityException.ERROR_NAME_INCORRECT);
      }
    } catch (SecurityException e) {
      throw e;
    } catch (Exception e) {
      throw new SecurityException(SecurityException.ERROR_CAN_NOT_FIND_USER_ATTRIBUTES_LDAP);
    }

    return search;
  }
  public List connectionVerification(
      LDAPAuthenticationUser attributesUser,
      String password,
      LDAPDef ldapDef,
      String attributes[],
      String entidad)
      throws SecurityException {

    Integer deptId = null;
    List deptList = new ArrayList();
    String attr[] = {attributes[0]};
    LdapConnection conn = null;
    try {
      String group = null;
      String filter = null;
      LdapSearch search = null;
      LDAPAuthenticationUser attributesGroup = null;
      if (log.isDebugEnabled()) {
        log.debug("dn [" + attributesUser.getDn() + "] con el log [" + log + "]");
      }
      conn = new LdapConnection();
      LdapConnCfg ldapConfig = LdapConfigUtils.createLdapConnConfig(ldapDef);
      ldapConfig.setProvider(1);
      // conn.open(ldapConfig, ldapDef.getLdapUser(), password, 1);
      conn.open(ldapConfig, ldapConfig.getUser(), password);

      List groups = null;
      int scope =
          new Integer(
                  LDAPRBUtil.getInstance(null)
                      .getProperty(LDAP_SCOPEGROUP + ldapDef.getLdapEngine()))
              .intValue();
      if (ldapDef.getLdapEngine() == 1) {
        groups = attributesUser.getGroupList();
        if (log.isDebugEnabled()) {
          log.debug(" groups [" + groups + "] con el log [" + log + "]");
        }

        filter =
            LDAPRBUtil.getInstance(null)
                .getProperty(LDAP_SCOPE_BASESUBTREE_GROUP + ldapDef.getLdapEngine());
        if (log.isDebugEnabled()) {
          log.debug(" filter [" + filter + "] con el log [" + log + "]");
        }

        for (int i = 0; i < groups.size(); i++) {

          group = (String) groups.get(i);

          search = new LdapSearch();
          search.initialize(conn, group, scope, filter, attr);
          search.execute();
          if (search.next()) {
            attributesGroup = getUserAttributes(search, attributes);
            attributesGroup.setGuidStringFormat(
                LdapBasicFns.formatGuid(conn, attributesGroup.getGuid()));
            deptId = getRegisterDeptOfic(attributesGroup.getGuidStringFormat(), entidad);

            if (deptId != null) {
              deptList.add(deptId);
              if (log.isDebugEnabled()) {
                log.debug(" deptId [" + deptId + "] con el log [" + log + "]");
              }
              // break;
            }
          }
        }
      } else {
        filter =
            MessageFormat.format(
                LDAPRBUtil.getInstance(null)
                    .getProperty(LDAP_SCOPE_BASESUBTREE_GROUP + ldapDef.getLdapEngine()),
                new String[] {attributesUser.getDn()});
        if (log.isDebugEnabled()) {
          log.debug(" filter [" + filter + "] con el log [" + log + "]");
        }
        List list = new ArrayList();
        list = getUserGroupGuidsIp(conn, ldapDef.getLdapRoot(), scope, filter, attr);
        if (log.isDebugEnabled()) {
          log.debug(" groups [" + list + "] con el log [" + log + "]");
        }

        String groupGuid = null;
        for (int i = 0; i < list.size(); i++) {
          groupGuid = (String) list.get(i);
          deptId = getRegisterDeptOfic(groupGuid, entidad);

          if (deptId != null) {
            if (log.isDebugEnabled()) {
              log.debug(" deptId [" + deptId + "] con el log [" + log + "]");
            }
            deptList.add(deptId);
            // break;
          }
        }
      }
    } catch (SecurityException e) {
      throw new SecurityException(SecurityException.ERROR_PASSWORD_INCORRECT);
    } catch (Exception e) {
      throw new SecurityException(SecurityException.ERROR_PASSWORD_INCORRECT);
    } finally {
      try {
        conn.close();
      } catch (Exception e) {
      }
    }
    return deptList;
  }
  public List connectionVerification(
      LdapConnection conn,
      LDAPAuthenticationUser attributesUser,
      LDAPDef ldapDef,
      String attributes[],
      String entidad,
      List groupList)
      throws SecurityException {
    Integer deptId = null;
    List deptList = new ArrayList();
    String attr[] = {attributes[0]};
    try {
      String group = null;
      String filter = null;
      LdapSearch search = null;
      LDAPAuthenticationUser attributesGroup = null;
      List groups = null;
      int scope =
          new Integer(
                  LDAPRBUtil.getInstance(null)
                      .getProperty(LDAP_SCOPEGROUP + ldapDef.getLdapEngine()))
              .intValue();
      groups = attributesUser.getGroupList();
      if (log.isDebugEnabled()) {
        log.debug("groups [" + groups + "] con el log [" + log + "]");
      }
      for (int i = 0; i < groups.size(); i++) {
        group = (String) groups.get(i);
        filter =
            LDAPRBUtil.getInstance(null)
                .getProperty(LDAP_SCOPE_BASESUBTREE_GROUP + ldapDef.getLdapEngine());
        search = new LdapSearch();
        search.initialize(conn, group, scope, filter, attr);
        search.execute();
        if (search.next()) {
          attributesGroup = getUserAttributes(search, attributes);
          attributesGroup.setGuidStringFormat(
              LdapBasicFns.formatGuid(conn, attributesGroup.getGuid()));

          // obtiene los grupos
          Integer idGroup = getIdGroup(attributesGroup.getGuidStringFormat(), entidad);

          if (idGroup != null) {
            groupList.add(idGroup);
          }

          deptId = getRegisterDeptOfic(attributesGroup.getGuidStringFormat(), entidad);

          if (deptId != null) {
            deptList.add(deptId);
          }
        }
      }
    } catch (SecurityException e) {
      log.error("Error connectionVerification: " + e.getMessage(), e);
      throw new SecurityException(SecurityException.ERROR_PASSWORD_INCORRECT);
    } catch (Exception e) {
      log.error("Error connectionVerification: " + e.getMessage(), e);
      throw new SecurityException(SecurityException.ERROR_PASSWORD_INCORRECT);
    }
    return deptList;
  }
  public LDAPAuthenticationUser getUserAttributes(LdapSearch search, String[] attributes)
      throws SecurityException {
    LDAPAuthenticationUser ldapAUser = new LDAPAuthenticationUser();
    if (search.getM_srAttrs() == null) {
      throw new SecurityException(SecurityException.ERROR_CAN_NOT_FIND_USER_ATTRIBUTES_LDAP);
    } else {
      try {
        Object object = null;
        for (NamingEnumeration enum1 = search.getM_srAttrs().getAll(); enum1.hasMore(); ) {
          Attribute attrib = (Attribute) enum1.next();

          for (NamingEnumeration e = attrib.getAll(); e.hasMore(); ) {
            object = e.next();
            try {
              if (attrib.getID().equals(attributes[0])) {
                ldapAUser.setGuid(object);
              }
              if (attrib.getID().equals(attributes[1])) {
                ldapAUser.setDn((String) object);
              }

              if (attrib.getID().equals(attributes[2])) {
                String obj = (String) object;
                if (attributes[2].equals("CN")) {
                  if (obj != null && !obj.equals("")) {
                    ldapAUser.setFullName((String) object);
                  }
                } else {
                  if (ldapAUser.getFullName() == null && obj != null && !obj.equals("")) {
                    ldapAUser.setFullName((String) object);
                  }
                }
              }

              if (attrib.getID().equals(attributes[3])) {
                String obj = (String) object;
                if (attributes[3].equals("cn")) {
                  if (obj != null && !obj.equals("")) {
                    ldapAUser.setFullName((String) object);
                  }
                } else {
                  if (ldapAUser.getFullName() == null && obj != null && !obj.equals("")) {
                    ldapAUser.setFullName((String) object);
                  }
                }
              }

              if (attrib.getID().equals(attributes[4])) {
                String obj = (String) object;
                if (ldapAUser.getFullName() == null && obj != null && !obj.equals("")) {
                  ldapAUser.setFullName((String) object);
                }
              }
              if (attrib.getID().equals(attributes[5])) {
                ldapAUser.setGroups((String) object);
              }
            } catch (Exception ex) {
            }
          }
        }

      } catch (NamingException e) {
        throw new SecurityException(SecurityException.ERROR_CAN_NOT_FIND_USER_ATTRIBUTES_LDAP);
      } catch (Exception e) {
        throw new SecurityException(SecurityException.ERROR_CAN_NOT_FIND_USER_ATTRIBUTES_LDAP);
      }
    }

    return ldapAUser;
  }
  public LdapSearch getSearch(
      String login, LdapConnection conn, LDAPDef ldapDef, String[] attributes)
      throws SecurityException {
    String filter = null;
    LdapSearch search = null;
    try {
      // busqueda por dn
      filter =
          MessageFormat.format(
              LDAPRBUtil.getInstance(null).getProperty(LDAP_SCOPE_BASE + ldapDef.getLdapEngine()),
              new String[] {login});
      if (log.isDebugEnabled()) {
        log.debug("filter [" + filter + "] con el log [" + log + "]");
        log.debug("dn [" + login + "] con el log [" + log + "]");
      }
      search = new LdapSearch();
      search.initialize(
          conn, ldapDef.getLdapRoot(), SearchControls.OBJECT_SCOPE, filter, attributes);
      search.execute();
      if (!search.next()) {
        // Busqueda por UniqueName
        filter =
            MessageFormat.format(
                LDAPRBUtil.getInstance(null)
                    .getProperty(LDAP_SCOPE_SUBTREE1 + ldapDef.getLdapEngine()),
                new String[] {login});
        if (log.isDebugEnabled()) {
          log.debug("filter [" + filter + "] con el log [" + log + "]");
          log.debug("UniqueName [" + login + "] con el log [" + log + "]");
        }
        search = new LdapSearch();
        search.initialize(
            conn, ldapDef.getLdapRoot(), SearchControls.SUBTREE_SCOPE, filter, attributes);
        search.execute();
        if (!search.next()) {
          // Busqueda por número de cuenta sAMAccountName
          filter =
              MessageFormat.format(
                  LDAPRBUtil.getInstance(null)
                      .getProperty(LDAP_SCOPE_SUBTREE2 + ldapDef.getLdapEngine()),
                  new String[] {login});
          if (log.isDebugEnabled()) {
            log.debug("filter [" + filter + "] con el log [" + log + "]");
            log.debug("sAMAccountName [" + login + "] con el log [" + log + "]");
          }
          if (!filter.equals("")) {
            search = new LdapSearch();
            search.initialize(
                conn, ldapDef.getLdapRoot(), SearchControls.SUBTREE_SCOPE, filter, attributes);
            search.execute();
            if (!search.next()) {
              throw new SecurityException(SecurityException.ERROR_NAME_INCORRECT);
            }
          }
        }
      }
      if (search.getM_srAttrs() == null) {
        throw new SecurityException(SecurityException.ERROR_NAME_INCORRECT);
      }
    } catch (SecurityException e) {
      throw e;
    } catch (Exception e) {
      throw new SecurityException(SecurityException.ERROR_CAN_NOT_FIND_USER_ATTRIBUTES_LDAP);
    }

    return search;
  }