You are here

public function AccessDeniedSubscriber::onException in SAML Authentication 8.2

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

Redirects users when access is denied.

Parameters

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

File

src/EventSubscriber/AccessDeniedSubscriber.php, line 42

Class

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

Namespace

Drupal\samlauth\EventSubscriber

Code

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