You are here

public function AccessDeniedSubscriber::onException in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php \Drupal\user\EventSubscriber\AccessDeniedSubscriber::onException()

Redirects users when access is denied.

Parameters

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

File

core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php, line 57
Contains \Drupal\user\EventSubscriber\AccessDeniedSubscriber.

Class

AccessDeniedSubscriber
Redirects users when access is denied.

Namespace

Drupal\user\EventSubscriber

Code

public function onException(GetResponseForExceptionEvent $event) {
  $exception = $event
    ->getException();
  if ($exception instanceof AccessDeniedHttpException) {
    $route_name = RouteMatch::createFromRequest($event
      ->getRequest())
      ->getRouteName();
    if ($this->account
      ->isAuthenticated()) {
      switch ($route_name) {
        case 'user.login':

          // Redirect an authenticated user to the profile page.
          $event
            ->setResponse($this
            ->redirect('entity.user.canonical', [
            'user' => $this->account
              ->id(),
          ]));
          break;
        case 'user.register':

          // Redirect an authenticated user to the profile form.
          $event
            ->setResponse($this
            ->redirect('entity.user.edit_form', [
            'user' => $this->account
              ->id(),
          ]));
          break;
      }
    }
    elseif ($route_name === 'user.page') {
      $event
        ->setResponse($this
        ->redirect('user.login'));
    }
  }
}