You are here

function simple_ldap_user_user_presave in Simple LDAP 7.2

Same name and namespace in other branches
  1. 7 simple_ldap_user/simple_ldap_user.module \simple_ldap_user_user_presave()

Implements hook_user_presave().

Fires before an account is created or changed.

Parameters

array $edit: The form values submitted by the user.

File

simple_ldap_user/simple_ldap_user.module, line 228
Main simple_ldap_user module file.

Code

function simple_ldap_user_user_presave(&$edit, $account, $category) {

  // Skip for UID 1 .
  if (property_exists($account, 'uid') && $account->uid == 1) {
    return;
  }

  // Do not overwrite the user status in the database.
  if (isset($account->simple_ldap_user_drupal_status)) {

    // If status is in the edit array, we need to be sure we're not
    // unintentionally saving the LDAP value to the database. To check this, we
    // see if $edit['status'] matches $account->status. If it does, set
    // $edit['status'] to the simple_ldap_user_drupal_status value.
    if (isset($edit['status']) && $edit['status'] == $account->status) {
      $edit['status'] = $account->simple_ldap_user_drupal_status;
    }

    // Now set the $account->status back to the value in the database, just to
    // be safe. This ensures that if $edit['status'] is empty, we don't mess up
    // what's in the database with what is on the user object.
    $account->status = $account->simple_ldap_user_drupal_status;
  }

  // To make sure we've covered all our bases, we also set $account->original's
  // status back to what is in the database as well. This avoids Drupal sending
  // account activation emails in user_save(), which it will do if it detects
  // that the status has changed from $account->original to $account.
  if (isset($account->original) && isset($account->original->simple_ldap_user_drupal_status)) {
    $account->original->status = $account->original->simple_ldap_user_drupal_status;
  }
  if ($account->is_new && isset($edit['name'])) {
    $ldap_user = SimpleLdapUser::singleton($edit['name']);
    if ($ldap_user->exists) {

      // Force an initial sync from LDAP to drupal.
      $attribute_mail = simple_ldap_user_variable_get('simple_ldap_user_attribute_mail');

      // Get the user's email address.
      $edit['mail'] = $ldap_user->{$attribute_mail}[0];

      // Get the remaining mapped attributes.
      // Process the LDAP user to generate the edit array that gets passed to user_save();
      simple_ldap_user_generate_edit_ldap_to_drupal($edit, $ldap_user, $account);
    }
  }

  //If account updated and username changed, RDN default to drupal name and ldap

  //entry already exists from elsewhere, reset new changed username to its previous value.
  if (isset($account->original)) {

    //New name edited or programmatically set ?
    $new_username = '';
    if (!empty($edit['name']) && strcasecmp($account->original->name, $edit['name']) != 0) {
      $new_username = $edit['name'];
    }
    elseif (strcasecmp($account->original->name, $account->name) != 0) {
      $new_username = $account->name;
    }
    if ($new_username) {
      $ldap_user = SimpleLdapUser::singleton($new_username);
      $attribute_rdn = simple_ldap_user_variable_get('simple_ldap_user_attribute_rdn');
      if (empty($attribute_rdn) && $ldap_user->exists) {
        $account->name = $account->original->name;
        $edit['name'] = $account->original->name;
        drupal_set_message(t('The new username %name could not be changed because a conflicting LDAP entry already exists. Previous names have been kept and no LDAP modification has been done.', array(
          '%name' => $new_username,
        )), 'error');
      }
    }
  }
}