You are here

function simple_ldap_user_drush_update_all_puids in Simple LDAP 7.2

1 string reference to 'simple_ldap_user_drush_update_all_puids'
simple_ldap_user_drush_command in simple_ldap_user/simple_ldap_user.drush.inc
Implementation of hook_drush_command()

File

simple_ldap_user/simple_ldap_user.drush.inc, line 52

Code

function simple_ldap_user_drush_update_all_puids() {

  // Check there is something to map.
  $puid_attr = strtolower(variable_get('simple_ldap_user_unique_attribute', ''));
  $attribute_name = strtolower(simple_ldap_user_variable_get('simple_ldap_user_attribute_name'));
  $attribute_mail = strtolower(simple_ldap_user_variable_get('simple_ldap_user_attribute_mail'));
  $found_count = $missing_count = $warning_count = 0;
  if (!$puid_attr) {
    drush_log(dt('No PUID attribute set.'), 'warning');
    return;
  }

  // For each user, fetch the LDAP record and update the authmap
  $result = db_query("SELECT uid,name,mail FROM {users}");
  while ($account = $result
    ->fetchAssoc()) {
    $ldap_user = SimpleLdapUser::singleton($account['name']);
    if (!$ldap_user->exists) {
      $missing_count++;
      continue;
    }
    if (strcasecmp($ldap_user->{$attribute_name}[0], $account['name'])) {
      drush_log(dt('User "@username" found, but not by name.  Skipping.', array(
        '@username' => $account['name'],
      )), 'warning');
      $warning_count++;
      continue;
    }
    if (strcasecmp($ldap_user->{$attribute_mail}[0], $account['mail'])) {
      drush_log(dt('Mail attribute for user "@username" does not match: @drupal_mail != @ldap_mail', array(
        '@username' => $account['name'],
        '@drupal_mail' => $account['mail'],
        '@ldap_mail' => $ldap_user->{$attribute_mail}[0],
      ), 'warning'));
      $warning_count++;
      continue;
    }

    // If no issues, write to the authmap
    db_merge('authmap')
      ->key(array(
      'uid' => $account['uid'],
      'module' => 'simple_ldap',
    ))
      ->fields(array(
      'authname' => $ldap_user->{$puid_attr}[0],
    ))
      ->execute();
    $found_count++;
  }
  if ($found_count) {
    drush_log(dt('Updated @found PUIDs from LDAP records.', array(
      '@found' => format_plural($found_count, '1 user', '@count users'),
    )), 'notice');
  }
  if ($missing_count) {
    drush_log(dt('Could not find @missing users in LDAP.', array(
      '@missing' => format_plural($missing_count, '1 user', '@count users'),
    )), 'notice');
  }
  if ($warning_count) {
    drush_log(dt('Detected @warning with anomalous entries.', array(
      '@warning' => format_plural($warning_count, '1 user', '@count users'),
    )), 'warning');
  }
}