You are here

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

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

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

1 call to AuthController::signupUser()
AuthController::processUserLogin in src/Controller/AuthController.php
Process the auth0 user profile and signin or signup the user.

File

src/Controller/AuthController.php, line 360

Class

AuthController
Controller routines for auth0 authentication.

Namespace

Drupal\auth0\Controller

Code

protected function signupUser($userInfo, $idToken) {

  // If the user doesn't exist we need to either create a new one, or assign him to an existing one.
  $isDatabaseUser = FALSE;

  /* Make sure we have the identities array, if not, fetch it from the user endpoint */
  $hasIdentities = is_object($userInfo) && $userInfo
    ->has('identities') || is_array($userInfo) && array_key_exists('identities', $userInfo);
  if (!$hasIdentities) {
    $mgmtClient = new Management($idToken, $this->domain);
    $user = $mgmtClient->users
      ->get($userInfo['user_id']);
    $userInfo['identities'] = $user['identities'];
  }
  foreach ($userInfo['identities'] as $identity) {
    if ($identity['provider'] == "auth0") {
      $isDatabaseUser = TRUE;
    }
  }
  $joinUser = FALSE;

  // 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']);
  }
  if ($joinUser) {

    // 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 {

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