comparison core/modules/user/src/EventSubscriber/AccessDeniedSubscriber.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
2 2
3 namespace Drupal\user\EventSubscriber; 3 namespace Drupal\user\EventSubscriber;
4 4
5 use Drupal\Core\Session\AccountInterface; 5 use Drupal\Core\Session\AccountInterface;
6 use Drupal\Core\Routing\RouteMatch; 6 use Drupal\Core\Routing\RouteMatch;
7 use Drupal\Core\Routing\UrlGeneratorTrait; 7 use Drupal\Core\Url;
8 use Drupal\Core\Routing\UrlGeneratorInterface;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface; 8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9 use Symfony\Component\HttpFoundation\RedirectResponse;
10 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; 10 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
11 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 11 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
12 use Symfony\Component\HttpKernel\KernelEvents; 12 use Symfony\Component\HttpKernel\KernelEvents;
13 13
14 /** 14 /**
19 * their profile page and from the user registration form to their profile edit 19 * their profile page and from the user registration form to their profile edit
20 * form. 20 * form.
21 */ 21 */
22 class AccessDeniedSubscriber implements EventSubscriberInterface { 22 class AccessDeniedSubscriber implements EventSubscriberInterface {
23 23
24 use UrlGeneratorTrait;
25
26 /** 24 /**
27 * The current user. 25 * The current user.
28 * 26 *
29 * @var \Drupal\Core\Session\AccountInterface 27 * @var \Drupal\Core\Session\AccountInterface
30 */ 28 */
33 /** 31 /**
34 * Constructs a new redirect subscriber. 32 * Constructs a new redirect subscriber.
35 * 33 *
36 * @param \Drupal\Core\Session\AccountInterface $account 34 * @param \Drupal\Core\Session\AccountInterface $account
37 * The current user. 35 * The current user.
38 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
39 * The URL generator.
40 */ 36 */
41 public function __construct(AccountInterface $account, UrlGeneratorInterface $url_generator) { 37 public function __construct(AccountInterface $account) {
42 $this->account = $account; 38 $this->account = $account;
43 $this->setUrlGenerator($url_generator);
44 } 39 }
45 40
46 /** 41 /**
47 * Redirects users when access is denied. 42 * Redirects users when access is denied.
48 * 43 *
51 */ 46 */
52 public function onException(GetResponseForExceptionEvent $event) { 47 public function onException(GetResponseForExceptionEvent $event) {
53 $exception = $event->getException(); 48 $exception = $event->getException();
54 if ($exception instanceof AccessDeniedHttpException) { 49 if ($exception instanceof AccessDeniedHttpException) {
55 $route_name = RouteMatch::createFromRequest($event->getRequest())->getRouteName(); 50 $route_name = RouteMatch::createFromRequest($event->getRequest())->getRouteName();
51 $redirect_url = NULL;
56 if ($this->account->isAuthenticated()) { 52 if ($this->account->isAuthenticated()) {
57 switch ($route_name) { 53 switch ($route_name) {
58 case 'user.login'; 54 case 'user.login';
59 // Redirect an authenticated user to the profile page. 55 // Redirect an authenticated user to the profile page.
60 $event->setResponse($this->redirect('entity.user.canonical', ['user' => $this->account->id()])); 56 $redirect_url = Url::fromRoute('entity.user.canonical', ['user' => $this->account->id()], ['absolute' => TRUE]);
61 break; 57 break;
62 58
63 case 'user.register'; 59 case 'user.register';
64 // Redirect an authenticated user to the profile form. 60 // Redirect an authenticated user to the profile form.
65 $event->setResponse($this->redirect('entity.user.edit_form', ['user' => $this->account->id()])); 61 $redirect_url = Url::fromRoute('entity.user.edit_form', ['user' => $this->account->id()], ['absolute' => TRUE]);
66 break; 62 break;
67 } 63 }
68 } 64 }
69 elseif ($route_name === 'user.page') { 65 elseif ($route_name === 'user.page') {
70 $event->setResponse($this->redirect('user.login')); 66 $redirect_url = Url::fromRoute('user.login', [], ['absolute' => TRUE]);
67 }
68
69 if ($redirect_url) {
70 $event->setResponse(new RedirectResponse($redirect_url->toString()));
71 } 71 }
72 } 72 }
73 } 73 }
74 74
75 /** 75 /**