annotate core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\user\EventSubscriber;
Chris@0 4
Chris@0 5 use Drupal\Core\Session\AccountInterface;
Chris@0 6 use Drupal\Core\Routing\RouteMatch;
Chris@0 7 use Drupal\Core\Routing\UrlGeneratorTrait;
Chris@0 8 use Drupal\Core\Routing\UrlGeneratorInterface;
Chris@0 9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 10 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Chris@0 11 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
Chris@0 12 use Symfony\Component\HttpKernel\KernelEvents;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Redirects users when access is denied.
Chris@0 16 *
Chris@0 17 * Anonymous users are taken to the login page when attempting to access the
Chris@0 18 * user profile pages. Authenticated users are redirected from the login form to
Chris@0 19 * their profile page and from the user registration form to their profile edit
Chris@0 20 * form.
Chris@0 21 */
Chris@0 22 class AccessDeniedSubscriber implements EventSubscriberInterface {
Chris@0 23
Chris@0 24 use UrlGeneratorTrait;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The current user.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\Session\AccountInterface
Chris@0 30 */
Chris@0 31 protected $account;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Constructs a new redirect subscriber.
Chris@0 35 *
Chris@0 36 * @param \Drupal\Core\Session\AccountInterface $account
Chris@0 37 * The current user.
Chris@0 38 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
Chris@0 39 * The URL generator.
Chris@0 40 */
Chris@0 41 public function __construct(AccountInterface $account, UrlGeneratorInterface $url_generator) {
Chris@0 42 $this->account = $account;
Chris@0 43 $this->setUrlGenerator($url_generator);
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Redirects users when access is denied.
Chris@0 48 *
Chris@0 49 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 50 * The event to process.
Chris@0 51 */
Chris@0 52 public function onException(GetResponseForExceptionEvent $event) {
Chris@0 53 $exception = $event->getException();
Chris@0 54 if ($exception instanceof AccessDeniedHttpException) {
Chris@0 55 $route_name = RouteMatch::createFromRequest($event->getRequest())->getRouteName();
Chris@0 56 if ($this->account->isAuthenticated()) {
Chris@0 57 switch ($route_name) {
Chris@0 58 case 'user.login';
Chris@0 59 // Redirect an authenticated user to the profile page.
Chris@0 60 $event->setResponse($this->redirect('entity.user.canonical', ['user' => $this->account->id()]));
Chris@0 61 break;
Chris@0 62
Chris@0 63 case 'user.register';
Chris@0 64 // Redirect an authenticated user to the profile form.
Chris@0 65 $event->setResponse($this->redirect('entity.user.edit_form', ['user' => $this->account->id()]));
Chris@0 66 break;
Chris@0 67 }
Chris@0 68 }
Chris@0 69 elseif ($route_name === 'user.page') {
Chris@0 70 $event->setResponse($this->redirect('user.login'));
Chris@0 71 }
Chris@0 72 }
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * {@inheritdoc}
Chris@0 77 */
Chris@0 78 public static function getSubscribedEvents() {
Chris@0 79 // Use a higher priority than
Chris@0 80 // \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber, because there's
Chris@0 81 // no need to log the exception if we can redirect.
Chris@0 82 $events[KernelEvents::EXCEPTION][] = ['onException', 75];
Chris@0 83 return $events;
Chris@0 84 }
Chris@0 85
Chris@0 86 }