You are here

public function SimpleLdapUserManager::createDrupalUser in Simple LDAP 8

Create a corresponding Drupal user based on an LDAP user's attributes.

Parameters

SimpleLdapUser $user: The LDAP user to use to create a Drupal user.

string $password: The password to give the new user.

Return value

boolean|UserInterface A new user object with name and user populated. FALSE if the user could not be created.

Throws

\Drupal\Component\Plugin\Exception\PluginException

File

modules/simple_ldap_user/src/SimpleLdapUserManager.php, line 186

Class

SimpleLdapUserManager
Manages the loading and syncing of data between LDAP server and Drupal.

Namespace

Drupal\simple_ldap_user

Code

public function createDrupalUser(SimpleLdapUser $user, $password = NULL) {
  $attribute_values = $user
    ->getAttributes();
  $name_attribute = $this->config
    ->get('name_attribute');
  $mail_attribute = $this->config
    ->get('mail_attribute');

  /** @var \Drupal\user\UserInterface $new_user */
  $new_user = $this->entity_manager
    ->getStorage('user')
    ->create(array(
    'name' => $attribute_values[$name_attribute][0],
    'mail' => $attribute_values[$mail_attribute][0],
  ));
  if ($password) {
    $new_user
      ->setPassword($password);
  }
  $new_user
    ->enforceIsNew();
  $new_user
    ->activate();
  try {
    $new_user
      ->save();
  } catch (EntityStorageException $e) {
    return FALSE;
  }
  return $new_user;
}