You are here

class AccessDeniedSubscriber in SAML Authentication 8.2

Same name and namespace in other branches
  1. 8.3 src/EventSubscriber/AccessDeniedSubscriber.php \Drupal\samlauth\EventSubscriber\AccessDeniedSubscriber
  2. 4.x src/EventSubscriber/AccessDeniedSubscriber.php \Drupal\samlauth\EventSubscriber\AccessDeniedSubscriber

Redirects logged-in users when access is denied to /saml/login.

Hierarchy

  • class \Drupal\samlauth\EventSubscriber\AccessDeniedSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of AccessDeniedSubscriber

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

File

src/EventSubscriber/AccessDeniedSubscriber.php, line 17

Namespace

Drupal\samlauth\EventSubscriber
View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessDeniedSubscriber::$account protected property The current user.
AccessDeniedSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
AccessDeniedSubscriber::onException public function Redirects users when access is denied.
AccessDeniedSubscriber::__construct public function Constructs a new redirect subscriber.