You are here

class EventSubscriber in Janrain Registration 8

The event subscriber.

Hierarchy

  • class \Drupal\janrain_capture\EventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EventSubscriber

1 string reference to 'EventSubscriber'
janrain_capture.services.yml in ./janrain_capture.services.yml
janrain_capture.services.yml
1 service uses EventSubscriber
janrain_capture.event_subscriber in ./janrain_capture.services.yml
Drupal\janrain_capture\EventSubscriber

File

src/EventSubscriber.php, line 15

Namespace

Drupal\janrain_capture
View source
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));
        }
      }
    }
  }

}

Members