AccessDeniedSubscriber::onException

public AccessDeniedSubscriber::onException(GetResponseForExceptionEvent $event)

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 52

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'));
    }
  }
}
doc_Drupal
2016-10-29 08:42:23
Comments
Leave a Comment

Please login to continue.