You are here

function auth0_create_user_from_auth0 in Auth0 Single Sign On 7.2

Create a new Drupal user for an authenticated Auth0 user.

1 call to auth0_create_user_from_auth0()
auth0_login_auth0_user in ./auth0.module
Log in an Auth0 authenticated user.

File

./auth0.module, line 610

Code

function auth0_create_user_from_auth0($user_info) {
  $user = new stdClass();
  if (isset($user_info['email']) && !empty($user_info['email'])) {
    $email = $user_info['email'];
  }
  else {
    $email = "";
  }
  $user->mail = $email;
  $user->init = $email;

  // If the username already exists, create a new random one.
  $username = $user_info['nickname'];
  function_exists('dd') && dd($username, 'checking if drupal user already exists with auth0 nickname');
  if (user_load_by_name($username)) {
    $username .= time();
    function_exists('dd') && dd($username, 'existing drupal user found, using new random name');
  }
  $user->name = $username;
  $user->is_new = TRUE;

  // If auto_register from auth0 is enabled, they are active immediately, otherwise check the site registration settings
  $auth0_auto_register = variable_get('auth0_auto_register', FALSE);
  if ($auth0_auto_register) {
    $user->status = TRUE;
  }
  else {
    $user->status = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS;
  }
  $user->pass = user_password();
  function_exists('dd') && dd($user, 'saving new drupal user');
  $new_user = user_save($user);
  if ($user) {
    watchdog('Auth0', 'Account created for %name', array(
      '%name' => $user->name,
    ), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $user->uid . '/edit'));
  }

  // Notify the user if they must have approval.
  if (!$user->status) {
    drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'));
  }
  return $new_user->uid;
}