You are here

function simple_ldap_user_drush_scan_all in Simple LDAP 7.2

2 calls to simple_ldap_user_drush_scan_all()
simple_ldap_user_drush_check_all in simple_ldap_user/simple_ldap_user.drush.inc
simple_ldap_user_drush_export_all in simple_ldap_user/simple_ldap_user.drush.inc

File

simple_ldap_user/simple_ldap_user.drush.inc, line 212

Code

function simple_ldap_user_drush_scan_all($found_callback = NULL, $not_found_callback = NULL) {

  // Get the default search params.
  $name_attr = variable_get('simple_ldap_user_attribute_name', 'uid');
  $scope = variable_get('simple_ldap_user_scope', 'sub');
  $base_dn = variable_get('simple_ldap_user_basedn', NULL);
  if (!$base_dn) {
    drush_log(dt('No base DN set.'), 'error');
    return;
  }
  $attrs = array(
    $name_attr,
  );
  $server = SimpleLdapServer::singleton();
  if (!$server) {
    drush_log(dt('Could not connect to server.'), 'error');
    return;
  }

  // Get ready for the run.
  $found_limit = drush_get_option('found-count');
  $missing_limit = drush_get_option('missing-count');
  if ($found_limit) {
    drush_log(dt('Will exit after @count records are found.', array(
      '@count' => $found_limit,
    )), 'notice');
  }
  if ($missing_limit) {
    drush_log(dt('Will exit after @count records are not found.', array(
      '@count' => $missing_limit,
    )), 'notice');
  }
  $user_total_count = db_query("SELECT COUNT(*) AS count FROM {users}")
    ->fetchAssoc();
  $user_total_count = $user_total_count['count'];
  $puid_attr = strtolower(variable_get('simple_ldap_user_unique_attribute', ''));
  drush_log(dt('Using PUID Attr: @puid', array(
    '@puid' => empty($puid_attr) ? '(none)' : $puid_attr,
  )), 'notice');
  $sql = "SELECT u.*,a.authname FROM {users} u LEFT JOIN {authmap} a ON a.uid=u.uid AND a.module='simple_ldap'";
  $result = db_query($sql);
  if (!$result) {
    drush_log(dt('Failed to fetch list of users from Drupal database.'), 'error');
    return;
  }
  $bad_count = $user_count = $found_count = $missing_count = 0;
  while ($user = $result
    ->fetchAssoc()) {
    if ($user['uid'] < 2) {
      continue;
    }
    $user_count++;
    if ($user_count % 1024 == 0) {

      // Clear caches we won't need.
      entity_get_controller('file')
        ->resetCache();
      entity_get_controller('user')
        ->resetCache();

      // Clean up memory.
      gc_collect_cycles();
      printf("  %d%% complete (%s m: %d, f: %d)...\r", (int) (100 * $user_count / $user_total_count), _formatBytes(memory_get_usage()), $missing_count, $found_count);
    }
    if (strpos($user['name'], ',') !== FALSE) {
      drush_log(dt('User %username contains illegal characters.  Skipping.', array(
        '%username' => $user['name'],
      )), 'error');
      $bad_count++;
      continue;
    }
    $ldap_user = SimpleLdapUser::singleton($user['name']);
    $filter = 'mail=' . $user['mail'];
    $ldap_users_by_mail = $server
      ->search($base_dn, $filter, $scope, $attrs);
    if ($ldap_users_by_mail['count'] > 1) {
      drush_log(dt('Multiple LDAP users have the email address @email.  Skipping.', array(
        '@email' => $user['mail'],
      )), 'error');
      continue;
    }
    if ($ldap_users_by_mail['count'] && strtolower($ldap_users_by_mail[0][$name_attr][0]) !== strtolower($user['name'])) {
      drush_log(dt('Drupal User @drupal_user and LDAP record @ldap_name use the same email @email. Skipping.', array(
        '@drupal_user' => $user['name'],
        '@ldap_name' => $ldap_users_by_mail[0][$name_attr][0],
        '@email' => $user['mail'],
      )), 'error');
      continue;
    }
    if ($ldap_user->exists) {
      drush_log(dt("Found @user (@dn)", array(
        '@user' => $user['name'],
        '@dn' => $ldap_user->dn,
      )), 'debug');
      if (!empty($puid_attr) && $user['authname'] != $ldap_user->{$puid_attr}[0]) {
        drush_log(dt("    User @user: Authmap entry does not match PUID: Authmap: @authname LDAP: @puid", array(
          '@authname' => empty($user['authname']) ? "(empty)" : $user['authname'],
          '@puid' => empty($ldap_user->{$puid_attr}[0]) ? "(empty)" : $ldap_user->{$puid_attr}[0],
          '@user' => $user['name'],
        )), 'warning');
      }
      if (!empty($ldap_user->mail[0]) && strtolower($ldap_user->mail[0]) !== strtolower($user['mail'])) {
        drush_log(dt('    User @user has Drupal email @drupal_mail and LDAP email @ldap_mail.  Skipping.', array(
          '@user' => $user['name'],
          '@drupal_mail' => $user['mail'],
          '@ldap_mail' => $ldap_user->mail[0],
        )), 'warning');
        continue;
      }
      if (!empty($found_callback)) {
        $found_callback($user['authname'], $user, $ldap_user);
      }
      $found_count++;
      if ($found_limit && $found_count >= $found_limit) {
        drush_log(dt('Exiting after finding @found records.', array(
          '@found' => $found_limit,
        )), 'warning');
        break;
      }
    }
    else {
      if (!empty($not_found_callback)) {
        $not_found_callback($user['authname'], $user);
      }
      else {
        drush_log(dt("Missing @user (@dn)", array(
          '@user' => $user['name'],
          '@dn' => $user['authname'],
        )), 'notice');
      }
      $missing_count++;
      if ($missing_limit && $missing_count >= $missing_limit) {
        drush_log(dt('Exiting after not finding @missing records.', array(
          '@missing' => $missing_limit,
        )), 'warning');
        break;
      }
    }
    SimpleLdapUser::reset();
    unset($user);
    unset($ldap_user);
  }
  printf("100%% complete (%s / %s)...\nDone\n", _formatBytes(memory_get_usage()), _formatBytes(memory_get_peak_usage()));
  if ($missing_count > 0) {
    drush_log(dt('Found @found with LDAP records.', array(
      '@found' => format_plural($found_count, '1 user', '@count users'),
    )), 'warning');
    drush_log(dt('Missing @missing.', array(
      '@missing' => format_plural($missing_count, '1 user', '@count users'),
    )), 'warning');
    drush_log(dt('Found @bad with unusable usernames.', array(
      '@bad' => format_plural($bad_count, '1 user', '@count users'),
    )), 'warning');
    drush_log(dt("Run with --verbose to see the list of missing users."), 'warning');
  }
  else {
    drush_log(dt('Found all @found', array(
      '@found' => format_plural($found_count, '1 user', '@count users'),
    )), 'ok');
  }
}