You are here

AccessDeniedSubscriber.php in SAML Authentication 8.2

File

src/EventSubscriber/AccessDeniedSubscriber.php
View source
<?php

namespace Drupal\samlauth\EventSubscriber;

use Drupal\Core\Routing\LocalRedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Routing\RouteMatch;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Redirects logged-in users when access is denied to /saml/login.
 */
class AccessDeniedSubscriber implements EventSubscriberInterface {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $account;

  /**
   * Constructs a new redirect subscriber.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The current user.
   */
  public function __construct(AccountInterface $account) {
    $this->account = $account;
  }

  /**
   * Redirects users when access is denied.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   *   The event to process.
   */
  public function onException(GetResponseForExceptionEvent $event) {
    $exception = $event
      ->getException();
    if ($exception instanceof AccessDeniedHttpException && $this->account
      ->isAuthenticated()) {
      $route_name = RouteMatch::createFromRequest($event
        ->getRequest())
        ->getRouteName();
      switch ($route_name) {
        case 'samlauth.saml_controller_login':
        case 'samlauth.saml_controller_acs':

          // Redirect an authenticated user to the profile page.
          $url = Url::fromRoute('entity.user.canonical', [
            'user' => $this->account
              ->id(),
          ])
            ->toString();
          $event
            ->setResponse(new LocalRedirectResponse($url));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Use a higher priority than
    // \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber, because there's
    // no need to log the exception if we can redirect.
    $events[KernelEvents::EXCEPTION][] = [
      'onException',
      75,
    ];
    return $events;
  }

}

Classes

Namesort descending Description
AccessDeniedSubscriber Redirects logged-in users when access is denied to /saml/login.