You are here

public function EventSubscriber::onRequest in Janrain Registration 8

Redirect event.

Parameters

\Symfony\Component\HttpKernel\Event\RequestEvent $event: The Event to process.

File

src/EventSubscriber.php, line 34

Class

EventSubscriber
The event subscriber.

Namespace

Drupal\janrain_capture

Code

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));
      }
    }
  }
}