You are here

public function SimpleLdapUserSync::importIntoDrupal in Simple LDAP 8

Imports the LDAP information into Drupal if necessary.

Parameters

\Drupal\simple_ldap_user\SimpleLdapUser $user: The LDAP user.

string|null $password: The password.

Return value

bool|\Drupal\user\UserInterface The Drupal user.

Throws

\Drupal\Component\Plugin\Exception\PluginException

File

modules/simple_ldap_user/src/SimpleLdapUserSync.php, line 78

Class

SimpleLdapUserSync

Namespace

Drupal\simple_ldap_user

Code

public function importIntoDrupal(SimpleLdapUser $user, $password = NULL) {
  $needs_saving = FALSE;
  $account = $this->ldapManager
    ->loadDrupalUser($user);
  if ($account instanceof UserInterface) {
    if ($password) {
      $same_password = $this->password
        ->check($password, $account
        ->getPassword());
      if (!$same_password) {
        $account
          ->setPassword($password);
        $needs_saving = TRUE;
      }
    }
  }
  else {
    $account = $this->ldapManager
      ->createDrupalUser($user, $password);
    $this->messenger
      ->addStatus($this
      ->t('New user created for %name.', [
      '%name' => $account
        ->getAccountName(),
    ]));
  }
  $this
    ->updateDrupalUser($user, $account, $needs_saving);
  return $account;
}