You are here

EventSubscriber.php in Janrain Registration 8

File

src/EventSubscriber.php
View source
<?php

namespace Drupal\janrain_capture;

use Drupal\user\UserInterface;
use Drupal\user\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\Core\Url;

/**
 * The event subscriber.
 */
class EventSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    $events = [];
    $events[KernelEvents::REQUEST] = [
      'onRequest',
      1,
    ];
    return $events;
  }

  /**
   * Redirect event.
   *
   * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
   *   The Event to process.
   */
  public function onRequest($event) : void {
    $request = $event
      ->getRequest();
    if ($request->attributes
      ->get('_route') === 'user.page' || $request->attributes
      ->get('_route') === 'janrain_capture.view_profile') {
      $user = User::load(\Drupal::currentUser()
        ->id());
      if ($user instanceof UserInterface) {
        $uuid = $user
          ->uuid();

        // Restrict from passing the wrong UUID manually.
        if ($request->query
          ->get('uuid') !== $uuid) {

          // The "uuid" GET parameter must be presented in order to allow
          // Janrain loading the profile.
          $request->query
            ->set('uuid', $uuid);

          // Override globals to get the correct value from "getUri()".
          $request
            ->overrideGlobals();
          $event
            ->setResponse(new RedirectResponse(Url::fromRoute('janrain_capture.view_profile', [
            'uuid' => $uuid,
          ])
            ->toString(), RedirectResponse::HTTP_MOVED_PERMANENTLY));
        }
      }
    }
    if ($request->attributes
      ->get('_route') === 'entity.user.canonical' && $request->attributes
      ->get(RouteEnhancer::JANRAIN_ACCOUNT_PROPERTY)) {
      $user = $request->attributes
        ->get('user');
      if ($user instanceof UserInterface) {
        $uuid = $user
          ->uuid();

        // Restrict from passing the wrong UUID manually.
        if ($request->query
          ->get('uuid') !== $uuid) {

          // The "uuid" GET parameter must be presented in order to allow
          // Janrain loading the profile.
          $request->query
            ->set('uuid', $uuid);

          // Override globals to get the correct value from "getUri()".
          $request
            ->overrideGlobals();
          $event
            ->setResponse(new RedirectResponse($request
            ->getUri(), RedirectResponse::HTTP_MOVED_PERMANENTLY));
        }
      }
    }
  }

}

Classes

Namesort descending Description
EventSubscriber The event subscriber.