You are here

class LogintobogganController in LoginToboggan 8

Class LogintobogganController.

@package Drupal\logintoboggan\Controller

Hierarchy

Expanded class hierarchy of LogintobogganController

File

src/Controller/LogintobogganController.php, line 17

Namespace

Drupal\logintoboggan\Controller
View source
class LogintobogganController extends ControllerBase {

  /**
   * Basic create.
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('module_handler'));
  }

  /**
   * This will return the output of the page.
   */
  public function logintobogganValidateEmail($user, $timestamp, $hashed_pass, $operation) {
    $account = user::load($user);

    // $cur_account = \Drupal::currentUser();
    $cur_account = $this
      ->currentUser();
    $hash_2 = user_pass_rehash($account, $timestamp);

    // If you don't need to verify email (i.e. can set password), that's
    // effectively ok for immediate login.
    $must_verify = $this
      ->config('user.settings')
      ->get('verify_mail');

    // Does have to verify but has not logged in previously OR
    // the user can login without verifying email first
    // - the hashed password is correct.
    $stop = '';
    if ($must_verify && !$account
      ->getLastLoginTime() || !$must_verify && $hashed_pass == user_pass_rehash($account, $timestamp)) {
      $this
        ->getLogger('user')
        ->notice('E-mail validation URL used for %name with
      timestamp @timestamp.', [
        '%name' => $account
          ->getAccountName(),
        '@timestamp' => $timestamp,
      ]);

      // Add trusted role.
      LogintobogganUtility::processValidation($account);

      // Where do we redirect after confirming the account?
      $redirect_setting = $this
        ->config('logintoboggan.settings')
        ->get('redirect_on_confirm');
      $redirect_on_register = !empty($redirect_setting) ? $redirect_setting : '/';
      $redirect = LogintobogganUtility::processRedirect($redirect_on_register, $account);
      switch ($operation) {

        // Proceed with normal user login, as long as it's open registration and
        // account hasn't been blocked.
        case 'login':

          // Only show the validated message if there's a valid trusted role.
          if (!$must_verify) {
            $this
              ->messenger()
              ->addMessage($this
              ->t('You have successfully validated your e-mail address.'), 'status');
          }
          if ($account
            ->isBlocked()) {
            $this
              ->messenger()
              ->addMessage($this
              ->t('Your account is currently blocked -- login cancelled.'), 'error');
            return new RedirectResponse(Url::fromRoute('<front>')
              ->toString());
          }
          else {
            $redirect = logintoboggan_process_login($account, $redirect);
            return new RedirectResponse($redirect
              ->toString());
          }
          break;

        // Admin validation.
        case 'admin':
          if (!$must_verify) {
            _user_mail_notify('status_activated', $account);
          }
          $this
            ->messenger()
            ->addMessage($this
            ->t('You have successfully validated %user.', [
            '%user' => $account
              ->getUsername(),
          ]));
          if ($cur_account
            ->isAnonymous()) {
            return new RedirectResponse(Url::fromRoute('<front>', [
              'user' => $user,
            ])
              ->toString());
          }
          else {
            return new RedirectResponse(Url::fromRoute('entity.user.edit_form', [
              'user' => $user,
            ])
              ->toString());
          }
          break;

        // Catch all.
        default:
          $this
            ->messenger()
            ->addMessage($this
            ->t('You have successfully validated %user.', [
            '%user' => $account
              ->getUsername(),
          ]));
          return new RedirectResponse(Url::fromRoute('<front>')
            ->toString());
      }
    }
    else {
      $message = $this
        ->t('Sorry, you can only use your validation link once for security reasons.');

      // No one currently logged in, go straight to user login page.
      if ($cur_account
        ->isAnonymous()) {
        $message .= $this
          ->t('Please log in with your username and password instead now.');
        $goto = 'user.login';
      }
      else {
        $goto = 'user.page';
      }
      $this
        ->messenger()
        ->addMessage($message, 'error');
      return new RedirectResponse(Url::fromRoute($goto)
        ->toString());
    }
  }

  /**
   * This will return the output of the page.
   */
  public function logintobogganResendValidation($user) {
    $account = user::load($user);
    _user_mail_notify('register_no_approval_required', $account);

    // Notify admin or user that e-mail was sent and return to user edit form.
    if ($this
      ->currentUser()
      ->hasPermission('administer users')) {
      $this
        ->messenger()
        ->addMessage($this
        ->t("A validation e-mail has been sent to the user's e-mail address."));
    }
    else {
      $this
        ->messenger()
        ->addMessage($this
        ->t('A validation e-mail has been sent to your e-mail address. You will need to follow the instructions in that message in order to gain full access to the site.'));
    }
    return new RedirectResponse(URL::fromRoute('entity.user.edit_form', [
      'user' => $user,
    ])
      ->toString());
  }

  /**
   * This will return the output of the page.
   */
  public function logintobogganDenied() {
    $account = $this
      ->currentUser();
    if ($account
      ->isAnonymous()) {
      $page['#title'] = $this
        ->t('Access Denied / User log in');
    }
    else {
      $page = [
        '#title' => $this
          ->t('Access Denied'),
        '#theme' => 'lt_access_denied',
      ];
    }
    return $page;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
LogintobogganController::create public static function Basic create. Overrides ControllerBase::create
LogintobogganController::logintobogganDenied public function This will return the output of the page.
LogintobogganController::logintobogganResendValidation public function This will return the output of the page.
LogintobogganController::logintobogganValidateEmail public function This will return the output of the page.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.