<commit_before>package com.francelabs.datafari.ldap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.PartialResultException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.PagedResultsControl;
import javax.naming.ldap.PagedResultsResponseControl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LdapUtils {

  public static final String baseFilter = "(&(objectCategory=Person)(objectClass=User))";
  private static final String[] returnAttributes = { "sAMAccountName", "givenName", "userAccountControl" };
  private static final String departmentAttribute = "department";
  private static final String[] returnDepartmentAttributes = { "sAMAccountName", "givenName", "userAccountControl", departmentAttribute };
  // For paged results
  private static final int pageSize = 1000;

  private static final Logger logger = LogManager.getLogger(LdapUtils.class);

  /**
   * Gets the AD LDAP context of the provided url
   *
   * @param url      the url ("ldap://serverName:serverPort")
   * @param user     the user
   * @param password the <PASSWORD>
   * @return the LDAP context
   * @throws IOException
   * @throws NamingException .
   */
  public static LdapContext getLdapContext(final String url, final String user, final String password) throws NamingException, IOException {
    // create an initial directory context
    final Hashtable<String, Object> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("com.sun.jndi.ldap.connect.timeout", "1000");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_CREDENTIALS, password);

    // Create the initial directory context
    final LdapContext ldapCtx = new InitialLdapContext(env, null);
    ldapCtx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
    return ldapCtx;
  }

  public static boolean testUserBase(final LdapContext ldapContext, final String searchBase, final String userFilter) {
    final int scope = SearchControls.SUBTREE_SCOPE;
    // initializing search controls
    final SearchControls searchCtls = new SearchControls();
    searchCtls.setSearchScope(scope);
    searchCtls.setReturningAttributes(returnAttributes);

    try {
      logger.debug("Testing user base " + searchBase + ". Trying to find users with filter " + userFilter);
      final NamingEnumeration<SearchResult> users = ldapContext.search(searchBase, userFilter, searchCtls);

      while (users.hasMore()) {
        final Attributes attrs = users.next().getAttributes();

        // Check if the user is activated before considering we found a valid one
        final Attribute bitsAttribute = attrs.get("userAccountControl");
        if (bitsAttribute != null) {
          final long lng = Long.parseLong(bitsAttribute.get(0).toString());
          final long secondBit = lng & 2; // get bit 2
          if (secondBit == 0) { // User activated so return true as the userBase is valid
            logger.debug("Found users in user base " + searchBase);
            return true;
          }
        }
      }
    } catch (final NamingException e) {
      logger.error("Unable to search in user base " + searchBase, e);<commit_msg>Remove active users test in AD userbase test<commit_after>package com.francelabs.datafari.ldap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.PartialResultException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.PagedResultsControl;
import javax.naming.ldap.PagedResultsResponseControl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LdapUtils {

  public static final String baseFilter = "(&(objectCategory=Person)(objectClass=User))";
  private static final String[] returnAttributes = { "sAMAccountName", "givenName", "userAccountControl" };
  private static final String departmentAttribute = "department";
  private static final String[] returnDepartmentAttributes = { "sAMAccountName", "givenName", "userAccountControl", departmentAttribute };
  // For paged results
  private static final int pageSize = 1000;

  private static final Logger logger = LogManager.getLogger(LdapUtils.class);

  /**
   * Gets the AD LDAP context of the provided url
   *
   * @param url      the url ("ldap://serverName:serverPort")
   * @param user     the user
   * @param password the <PASSWORD>'s password
   * @return the LDAP context
   * @throws IOException
   * @throws NamingException .
   */
  public static LdapContext getLdapContext(final String url, final String user, final String password) throws NamingException, IOException {
    // create an initial directory context
    final Hashtable<String, Object> env = new Hashtable<>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("com.sun.jndi.ldap.connect.timeout", "1000");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_CREDENTIALS, password);

    // Create the initial directory context
    final LdapContext ldapCtx = new InitialLdapContext(env, null);
    ldapCtx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.NONCRITICAL) });
    return ldapCtx;
  }

  public static boolean testUserBase(final LdapContext ldapContext, final String searchBase, final String userFilter) {
    final int scope = SearchControls.SUBTREE_SCOPE;
    // initializing search controls
    final SearchControls searchCtls = new SearchControls();
    searchCtls.setSearchScope(scope);
    searchCtls.setReturningAttributes(returnAttributes);

    try {
      logger.debug("Testing user base " + searchBase + ". Trying to find users with filter " + userFilter);
      final NamingEnumeration<SearchResult> users = ldapContext.search(searchBase, userFilter, searchCtls);

      if (users.hasMore()) {
        logger.debug("Found users in user base " + searchBase);
        return true;
      }

      // This part was checking if there are active users in the found ones but depending on the AD type it does not work...
//      while (users.hasMore()) {
//        logger.debug("Found users in user base " + searchBase + "\r\nNow checking if there is at least one active user (userAccountControl attribute check)");
//        final Attributes attrs = users.next().getAttributes();
//
//        // Check if the user is activated before considering we found a valid one
//        final Attribute bitsAttribute = attrs.get("userAccountControl");
//        if (bitsAttribute != null) {
//          final long lng = Long.parseLong(bitsAttribute.get(0).toString());
//          final long secondBit = lng & 2; // get bit 2
//          if (secondBit == 0) { // User activated so return true as the userBase is valid
//            logger.debug("Found active users in user base " + searchBase);
//            return true;
//          }
//        } else {
//          logger.debug("Found a user without userAccountControl attribute in user base " + searchBase);
//        }
//      }
    } catch (final NamingException e) {
      logger.error("Unable to search in user base " + searchBase, e);
{'fraction_non_alphanumeric': 0.09977160716432264, 'fraction_numerical': 0.003365789157350643, 'mean_word_length': 3.92599171107164, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7251180', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2341, 'n_words': 651}