You are here

public function MainDeprecatedController::register in Bakery Single Sign-On System 8.2

Special Bakery register callback registers the user and returns to child.

File

src/Controller/MainDeprecatedController.php, line 63

Class

MainDeprecatedController

Namespace

Drupal\bakery\Controller

Code

public function register() {
  $cookie = $this->kitchen
    ->taste(Kitchen::OATMEAL);
  if ($cookie) {

    // Valid cookie.
    // Destroy the current oatmeal cookie,
    // we'll set a new one when we return to the slave.
    $this->kitchen
      ->eat(Kitchen::OATMEAL);

    // Users are allowed to register.
    if ($this
      ->config('user.settings')
      ->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
      $name = trim($cookie['data']['name']);
      $mail = trim($cookie['data']['mail']);
      $data = [
        'name' => $name,
      ];

      // Save errors.
      $errors = [];

      // Check if user exists with same email.
      if (user_load_by_mail($mail)) {
        $errors['mail'] = 1;
      }
      elseif (user_load_by_name($name)) {
        $errors['name'] = 1;
      }
      else {

        // Create user.
        if (!$cookie['data']['pass']) {
          $pass = user_password();
        }
        else {
          $pass = $cookie['data']['pass'];
        }
        $language = $this
          ->languageManager()
          ->getCurrentLanguage()
          ->getId();
        $account = User::create();

        // Mandatory settings.
        $account
          ->setPassword($pass);
        $account
          ->enforceIsNew();
        $account
          ->setEmail($mail);

        // This username must be unique and accept only a-Z,0-9, - _ @ .
        $account
          ->setUsername($name);

        // Optional settings.
        $account
          ->set("init", $mail);
        $account
          ->set("langcode", $language);
        $account
          ->set("preferred_langcode", $language);
        $account
          ->set("preferred_admin_langcode", $language);

        // $user->set("setting_name", 'setting_value');.
        $account
          ->activate();

        // Save user.
        $account
          ->save();

        // Set some info to return to the slave.
        $data['uid'] = $account
          ->id();
        $data['mail'] = $mail;
        $this
          ->getLogger('bakery')
          ->notice('New external user: %name using module bakery from slave !slave.', [
          '%name' => $account
            ->getAccountName(),
          '!slave' => $cookie['slave'],
        ]);

        // Redirect to slave.
        if (!$this
          ->config('user.settings')
          ->get('verify_mail')) {

          // Create identification cookie and log user in.
          $this->kitchen
            ->reBakeChocolateChipCookie($account);
          $this->bakeryService
            ->userExternalLogin($account);
        }
        else {

          // The user needs to validate their email, redirect back to slave to
          // inform them.
          $errors['validate'] = 1;
        }
      }
    }
    else {
      $this
        ->getLogger('bakery')
        ->error('Master Bakery site user registration is disabled but users are trying to register from a subsite.');
      $errors['register'] = 1;
    }
    if (!empty($errors)) {

      // There were errors.
      session_destroy();
    }

    // Redirect back to custom Bakery callback on slave.
    $data['errors'] = $errors;

    // Carry destination through return.
    if (isset($cookie['data']['destination'])) {
      $data['destination'] = $cookie['data']['destination'];
    }

    // Bake a new cookie for validation on the slave.
    $this->kitchen
      ->bake(new OatmealCookie($name, $data));
    return new TrustedRedirectResponse(Url::fromUri(rtrim($cookie['slave'], '/') . '/bakery')
      ->toString());
  }

  // Invalid request.
  throw new AccessDeniedHttpException();
}