You are here

class LogintobogganUtility in LoginToboggan 8

Class for utility methods.

@package Drupal\logintoboggan\Utility

Hierarchy

Expanded class hierarchy of LogintobogganUtility

5 files declare their use of LogintobogganUtility
logintoboggan.module in ./logintoboggan.module
LoginToboggan module.
logintoboggan.tokens.inc in ./logintoboggan.tokens.inc
Builds placeholder replacement tokens for logintoboggan-related data.
LogintobogganController.php in src/Controller/LogintobogganController.php
LogintobogganProfileForm.php in src/Form/LogintobogganProfileForm.php
LogintobogganRegister.php in src/Form/LogintobogganRegister.php

File

src/Utility/LogintobogganUtility.php, line 14

Namespace

Drupal\logintoboggan\Utility
View source
class LogintobogganUtility {

  /**
   * Returns the trusted role setting.
   *
   * @return array|mixed|null
   *   Role set as trusted.
   */
  public static function trustedRole() {
    return \Drupal::config('logintoboggan.settings')
      ->get('trusted_role');
  }

  /**
   * Returns a redirect URL.
   *
   * @param string $redirect
   *   Redirect location.
   * @param object $account
   *   User account.
   *
   * @return \Drupal\Core\Url
   *   returns a URL object.
   */
  public static function processRedirect($redirect, $account) {
    $variables = [
      '%uid' => $account
        ->id(),
    ];
    $redirect = parse_url(urldecode(strtr($redirect, $variables)));
    $redirect = UrlHelper::parse($redirect['path']);

    // If there's a path set, override the destination parameter if necessary.
    if ($redirect['path'] && \Drupal::config('logintoboggan.settings')
      ->get('override_destination_parameter')) {
      \Drupal::request()->query
        ->remove('destination');
    }

    // Explicitly create query and fragment elements if not present already.
    $query = isset($redirect['query']) ? $redirect['query'] : [];
    $fragment = isset($redirect['fragment']) ? $redirect['fragment'] : '';
    return Url::fromUserInput($redirect['path'], [
      'query' => $query,
      'fragment' => $fragment,
    ]);
  }

  /**
   * Generates a url for an email token.
   *
   * @param object $account
   *   User account.
   * @param array $url_options
   *   Any extra URL options.
   */
  public static function emlValidateUrl($account, array $url_options) {
    $request_time = \Drupal::time()
      ->getRequestTime();
    return Url::fromUserInput('/user/validate/' . $account
      ->id() . '/' . $request_time . '/' . user_pass_rehash($account, $request_time), $url_options)
      ->toString();
  }

  /**
   * Add trusted role to new user when validating from an email link.
   *
   * @param object $account
   *   User account.
   */
  public static function processValidation($account) {
    $trusted_role = self::trustedRole();

    // Core mail verification not required and trusted <>
    // authenticated so add the role.
    $trusted_used = !\Drupal::config('user.settings')
      ->get('verify_mail') && $trusted_role != AccountInterface::AUTHENTICATED_ROLE;
    if (!$account
      ->isBlocked()) {
      if ($trusted_used) {
        $account
          ->addRole($trusted_role);
        $account
          ->save();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LogintobogganUtility::emlValidateUrl public static function Generates a url for an email token.
LogintobogganUtility::processRedirect public static function Returns a redirect URL.
LogintobogganUtility::processValidation public static function Add trusted role to new user when validating from an email link.
LogintobogganUtility::trustedRole public static function Returns the trusted role setting.