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@18
|
7 use Drupal\Core\Url;
|
Chris@0
|
8 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@18
|
9 use Symfony\Component\HttpFoundation\RedirectResponse;
|
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 /**
|
Chris@0
|
25 * The current user.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $account;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Constructs a new redirect subscriber.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
35 * The current user.
|
Chris@0
|
36 */
|
Chris@18
|
37 public function __construct(AccountInterface $account) {
|
Chris@0
|
38 $this->account = $account;
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * Redirects users when access is denied.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
Chris@0
|
45 * The event to process.
|
Chris@0
|
46 */
|
Chris@0
|
47 public function onException(GetResponseForExceptionEvent $event) {
|
Chris@0
|
48 $exception = $event->getException();
|
Chris@0
|
49 if ($exception instanceof AccessDeniedHttpException) {
|
Chris@0
|
50 $route_name = RouteMatch::createFromRequest($event->getRequest())->getRouteName();
|
Chris@18
|
51 $redirect_url = NULL;
|
Chris@0
|
52 if ($this->account->isAuthenticated()) {
|
Chris@0
|
53 switch ($route_name) {
|
Chris@0
|
54 case 'user.login';
|
Chris@0
|
55 // Redirect an authenticated user to the profile page.
|
Chris@18
|
56 $redirect_url = Url::fromRoute('entity.user.canonical', ['user' => $this->account->id()], ['absolute' => TRUE]);
|
Chris@0
|
57 break;
|
Chris@0
|
58
|
Chris@0
|
59 case 'user.register';
|
Chris@0
|
60 // Redirect an authenticated user to the profile form.
|
Chris@18
|
61 $redirect_url = Url::fromRoute('entity.user.edit_form', ['user' => $this->account->id()], ['absolute' => TRUE]);
|
Chris@0
|
62 break;
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65 elseif ($route_name === 'user.page') {
|
Chris@18
|
66 $redirect_url = Url::fromRoute('user.login', [], ['absolute' => TRUE]);
|
Chris@18
|
67 }
|
Chris@18
|
68
|
Chris@18
|
69 if ($redirect_url) {
|
Chris@18
|
70 $event->setResponse(new RedirectResponse($redirect_url->toString()));
|
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 }
|