You are here

function simple_ldap_user_update_username_for_puid in Simple LDAP 7.2

Check the authmaps for the PUID. Update the {users} table if the PUID is present but the username is different.

Parameters

$puid The unique attribute for the user.:

$user_name The name of the user in LDAP:

Return value

FALSE if a name conflict exists NULL if no user has the PUID or no PUID Attr has been set TRUE if the user exists.

1 call to simple_ldap_user_update_username_for_puid()
simple_ldap_user_load_or_create_by_name in simple_ldap_user/simple_ldap_user.module
Create a valid LDAP user on this site if they don't already exist.

File

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

Code

function simple_ldap_user_update_username_for_puid($puid, $user_name) {
  $puid_attr = strtolower(simple_ldap_user_variable_get('simple_ldap_user_unique_attribute'));
  if (!$puid_attr) {
    return NULL;
  }

  // Look for the user in the authmaps.
  $drupal_user_map = db_query("SELECT u.name, u.uid FROM {users} u INNER JOIN {authmap} a ON u.uid = a.uid WHERE a.module='simple_ldap' AND a.authname=:authname", array(
    ':authname' => $puid,
  ))
    ->fetchAssoc();

  // No entry, return NULL
  if (empty($drupal_user_map)) {
    return NULL;
  }

  // If there is an authmap entry, make sure the username matches or is empty.
  if ($drupal_user_map['name'] != $user_name) {

    // Name has changed, make sure the new name isn't already in use.
    $conflicting_user = db_query("SELECT u.uid FROM {users} u WHERE u.name=:name", array(
      ':name' => $user_name,
    ))
      ->fetchAssoc();
    if ($conflicting_user) {
      watchdog('Simple LDAP', 'User %username (UID @uid) used to have login %oldname was renamed, but another user already has that name.', array(
        '%username' => $user_name,
        '@uid' => $conflicting_user['uid'],
        '%oldname' => $drupal_user_map['name'],
      ), WATCHDOG_ERROR);
      return FALSE;
    }

    // Update the username record directly so user_load_by_name() will find it.
    watchdog('Simple LDAP', 'User @uid was externally renamed from %oldname to %newname.', array(
      '@uid' => $drupal_user_map['uid'],
      '%oldname' => $drupal_user_map['name'],
      '%newname' => $user_name,
    ), WATCHDOG_NOTICE);
    db_query("UPDATE {users} SET name=:newname WHERE uid=:uid", array(
      ':newname' => $user_name,
      ':uid' => $drupal_user_map['uid'],
    ));
  }
  return TRUE;
}