You are here

class MagicUrl in Open Social 10.1.x

Same name and namespace in other branches
  1. 8.9 modules/custom/social_magic_login/src/Service/MagicUrl.php \Drupal\social_magic_login\Service\MagicUrl
  2. 8.5 modules/custom/social_magic_login/src/Service/MagicUrl.php \Drupal\social_magic_login\Service\MagicUrl
  3. 8.6 modules/custom/social_magic_login/src/Service/MagicUrl.php \Drupal\social_magic_login\Service\MagicUrl
  4. 8.7 modules/custom/social_magic_login/src/Service/MagicUrl.php \Drupal\social_magic_login\Service\MagicUrl
  5. 8.8 modules/custom/social_magic_login/src/Service/MagicUrl.php \Drupal\social_magic_login\Service\MagicUrl
  6. 10.3.x modules/custom/social_magic_login/src/Service/MagicUrl.php \Drupal\social_magic_login\Service\MagicUrl
  7. 10.0.x modules/custom/social_magic_login/src/Service/MagicUrl.php \Drupal\social_magic_login\Service\MagicUrl
  8. 10.2.x modules/custom/social_magic_login/src/Service/MagicUrl.php \Drupal\social_magic_login\Service\MagicUrl

Service to generate one-time login links (a.k.a 'magic' #lama's).

Hierarchy

Expanded class hierarchy of MagicUrl

1 string reference to 'MagicUrl'
social_magic_login.services.yml in modules/custom/social_magic_login/social_magic_login.services.yml
modules/custom/social_magic_login/social_magic_login.services.yml
1 service uses MagicUrl
social_magic_login.create_url in modules/custom/social_magic_login/social_magic_login.services.yml
\Drupal\social_magic_login\Service\MagicUrl

File

modules/custom/social_magic_login/src/Service/MagicUrl.php, line 12

Namespace

Drupal\social_magic_login\Service
View source
class MagicUrl implements MagicUrlInterface {

  /**
   * The path validator service.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * MagicUrlCreate constructor.
   *
   * @param \Drupal\Core\Path\PathValidatorInterface $pathValidator
   *   The path validator.
   */
  public function __construct(PathValidatorInterface $pathValidator) {
    $this->pathValidator = $pathValidator;
  }

  /**
   * {@inheritdoc}
   */
  public function create(UserInterface $account, string $destination, array $options) : ?Url {
    if (!isset($account, $destination)) {
      return NULL;
    }

    // Check if path isn't external.
    if (!$this->pathValidator
      ->getUrlIfValidWithoutAccessCheck($destination)) {
      return NULL;
    }

    // Get url options and prerequisites.
    $timestamp = \Drupal::time()
      ->getRequestTime();
    $lang_code = $options['langcode'] ?? $account
      ->getPreferredLangcode();
    $url_options = [
      'absolute' => TRUE,
      'language' => \Drupal::languageManager()
        ->getLanguage($lang_code),
    ];

    // Create url from route with the destination if it's set.
    return Url::fromRoute('social_magic_login.login', [
      'uid' => $account
        ->id(),
      'timestamp' => $timestamp,
      'hash' => user_pass_rehash($account, $timestamp),
      'destination' => base64_encode($destination),
    ], $url_options);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MagicUrl::$pathValidator protected property The path validator service.
MagicUrl::create public function Create a magic login link. Overrides MagicUrlInterface::create
MagicUrl::__construct public function MagicUrlCreate constructor.