You are here

public function ExternalAuth::register in External Authentication 2.0.x

Same name and namespace in other branches
  1. 8 src/ExternalAuth.php \Drupal\externalauth\ExternalAuth::register()

Register a Drupal user based on an external authname.

The Drupal username of the account to be created defaults to the external authentication name prefixed with the provider ID. The caller may enforce a custom Drupal username by setting that value in $account_data['name'].

Parameters

string $authname: The unique, external authentication name provided by authentication provider.

string $provider: The module providing external authentication.

array $account_data: An array of additional properties to be saved with the user entity. If the array contains a 'name' string value, it will be used as local Drupal username, otherwise a default local Drupal username will be computed as "{$provider}_{$authname}".

mixed $authmap_data: Additional data to be stored in the authmap entry.

Return value

\Drupal\user\UserInterface The registered Drupal user.

Overrides ExternalAuthInterface::register

1 call to ExternalAuth::register()
ExternalAuth::loginRegister in src/ExternalAuth.php
Login and optionally register a Drupal user based on an external authname.

File

src/ExternalAuth.php, line 101

Class

ExternalAuth
Class ExternalAuth.

Namespace

Drupal\externalauth

Code

public function register($authname, $provider, array $account_data = [], $authmap_data = NULL) {
  if (!empty($account_data['name'])) {
    $username = $account_data['name'];
    unset($account_data['name']);
  }
  else {
    $username = $provider . '_' . $authname;
  }
  $authmap_event = $this->eventDispatcher
    ->dispatch(ExternalAuthEvents::AUTHMAP_ALTER, new ExternalAuthAuthmapAlterEvent($provider, $authname, $username, $authmap_data));
  $entity_storage = $this->entityTypeManager
    ->getStorage('user');
  $account_search = $entity_storage
    ->loadByProperties([
    'name' => $authmap_event
      ->getUsername(),
  ]);
  if ($account = reset($account_search)) {
    throw new ExternalAuthRegisterException(sprintf('User could not be registered. There is already an account with username "%s"', $authmap_event
      ->getUsername()));
  }

  // Set up the account data to be used for the user entity.
  $account_data = array_merge([
    'name' => $authmap_event
      ->getUsername(),
    'init' => $provider . '_' . $authmap_event
      ->getAuthname(),
    'status' => 1,
    'access' => 0,
  ], $account_data);
  $account = $entity_storage
    ->create($account_data);
  $account
    ->enforceIsNew();
  $account
    ->save();
  $this->authmap
    ->save($account, $provider, $authmap_event
    ->getAuthname(), $authmap_event
    ->getData());
  $this->eventDispatcher
    ->dispatch(ExternalAuthEvents::REGISTER, new ExternalAuthRegisterEvent($account, $provider, $authmap_event
    ->getAuthname(), $authmap_event
    ->getData()));
  $this->logger
    ->notice('External registration of user %name from provider %provider and authname %authname', [
    '%name' => $account
      ->getAccountName(),
    '%provider' => $provider,
    '%authname' => $authname,
  ]);
  return $account;
}