You are here

protected function AuthController::signupUser in Auth0 Single Sign On 8.2

Same name and namespace in other branches
  1. 8 src/Controller/AuthController.php \Drupal\auth0\Controller\AuthController::signupUser()

Create or link a new user based on the auth0 profile.

Parameters

array $userInfo: The user info data array.

string $idToken: ID token returned during login.

Return value

bool|mixed The user object.

Throws

\Drupal\auth0\Exception\EmailNotVerifiedException The email not verified exception.

\Exception

1 call to AuthController::signupUser()
AuthController::processUserLogin in src/Controller/AuthController.php
Process the Auth0 user profile and sign in or sign the user up.

File

src/Controller/AuthController.php, line 635
Contains \Drupal\auth0\Controller\AuthController.

Class

AuthController
Controller routines for auth0 authentication.

Namespace

Drupal\auth0\Controller

Code

protected function signupUser(array $userInfo, $idToken = '') {

  // If the user doesn't exist we need to either create a new one,
  // or assign them to an existing one.
  $isDatabaseUser = FALSE;
  $user_sub_arr = explode('|', $userInfo['user_id']);
  $provider = $user_sub_arr[0];
  if ('auth0' === $provider) {
    $isDatabaseUser = TRUE;
  }
  $joinUser = FALSE;
  $user_name_claim = $this->config
    ->get('auth0_username_claim') ?: AUTH0_DEFAULT_USERNAME_CLAIM;

  // Drupal usernames do not allow pipe characters.
  $user_name_used = !empty($userInfo[$user_name_claim]) ? $userInfo[$user_name_claim] : str_replace('|', '_', $userInfo['user_id']);
  if ($this->config
    ->get('auth0_join_user_by_mail_enabled') && !empty($userInfo['email'])) {
    $this->auth0Logger
      ->notice($userInfo['email'] . ' join user by mail is enabled, looking up user by email');

    // If the user has a verified email or is a database user try to see if
    // there is a user to join with. The isDatabase is because we don't want
    // to allow database user creation if there is an existing one with no
    // verified email.
    if ($userInfo['email_verified'] || $isDatabaseUser) {
      $joinUser = user_load_by_mail($userInfo['email']);
    }
  }
  else {
    $this->auth0Logger
      ->notice($user_name_used . ' join user by username');
    if (!empty($userInfo['email_verified']) || $isDatabaseUser) {
      $joinUser = user_load_by_name($user_name_used);
    }
  }
  if ($joinUser) {
    $this->auth0Logger
      ->notice($joinUser
      ->id() . ' Drupal user found by email with uid');

    // If we are here, we have a potential join user.
    // Don't allow creation or assignation of user if the email is not
    // verified, that would be hijacking.
    if (!$userInfo['email_verified']) {
      throw new EmailNotVerifiedException();
    }
    $user = $joinUser;
  }
  else {
    $this->auth0Logger
      ->notice($user_name_used . ' creating new Drupal user from Auth0 user');

    // If we are here, we need to create the user.
    $user = $this
      ->createDrupalUser($userInfo);

    // Update field and role mappings.
    $this
      ->auth0UpdateFieldsAndRoles($userInfo, $user);
  }
  return $user;
}