You are here

Redirect.php in Open Social 8.8

File

modules/custom/social_gdpr/src/Subscriber/Redirect.php
View source
<?php

namespace Drupal\social_gdpr\Subscriber;

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Class Redirect.
 *
 * @package Drupal\social_gdpr\Subscriber
 */
class Redirect implements EventSubscriberInterface {

  /**
   * The current active route match object.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * RedirectSubscriber constructor.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current active route match object.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   */
  public function __construct(RouteMatchInterface $route_match, AccountProxyInterface $current_user) {
    $this->routeMatch = $route_match;
    $this->currentUser = $current_user;
  }

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

  /**
   * This method is called when the KernelEvents::REQUEST event is dispatched.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event.
   */
  public function checkForRedirection(GetResponseEvent $event) {
    if ($this->routeMatch
      ->getRouteName() != 'entity.data_policy.version_history') {
      return;
    }
    if ($this->currentUser
      ->id() == 1 || !$this->currentUser
      ->hasPermission('view all data policy revisions')) {
      return;
    }
    $url = Url::fromRoute('social_gdpr.data_policy.revisions');
    $response = new RedirectResponse($url
      ->toString());
    $event
      ->setResponse($response);
  }

}

Classes

Namesort descending Description
Redirect Class Redirect.