comparison core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
8 use Drupal\Core\Session\AccountProxyInterface; 8 use Drupal\Core\Session\AccountProxyInterface;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface; 9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10 use Symfony\Component\HttpKernel\Event\GetResponseEvent; 10 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
11 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; 11 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
12 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 12 use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
13 use Symfony\Component\HttpKernel\HttpKernelInterface;
14 use Symfony\Component\HttpKernel\KernelEvents; 13 use Symfony\Component\HttpKernel\KernelEvents;
15 14
16 /** 15 /**
17 * Authentication subscriber. 16 * Authentication subscriber.
18 * 17 *
70 * The request event. 69 * The request event.
71 * 70 *
72 * @see \Drupal\Core\Authentication\AuthenticationProviderInterface::authenticate() 71 * @see \Drupal\Core\Authentication\AuthenticationProviderInterface::authenticate()
73 */ 72 */
74 public function onKernelRequestAuthenticate(GetResponseEvent $event) { 73 public function onKernelRequestAuthenticate(GetResponseEvent $event) {
75 if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { 74 if ($event->isMasterRequest()) {
76 $request = $event->getRequest(); 75 $request = $event->getRequest();
77 if ($this->authenticationProvider->applies($request)) { 76 if ($this->authenticationProvider->applies($request)) {
78 $account = $this->authenticationProvider->authenticate($request); 77 $account = $this->authenticationProvider->authenticate($request);
79 if ($account) { 78 if ($account) {
80 $this->accountProxy->setAccount($account); 79 $this->accountProxy->setAccount($account);
91 * 90 *
92 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event 91 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
93 * The request event. 92 * The request event.
94 */ 93 */
95 public function onKernelRequestFilterProvider(GetResponseEvent $event) { 94 public function onKernelRequestFilterProvider(GetResponseEvent $event) {
96 if (isset($this->filter) && $event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { 95 if (isset($this->filter) && $event->isMasterRequest()) {
97 $request = $event->getRequest(); 96 $request = $event->getRequest();
98 if ($this->authenticationProvider->applies($request) && !$this->filter->appliesToRoutedRequest($request, TRUE)) { 97 if ($this->authenticationProvider->applies($request) && !$this->filter->appliesToRoutedRequest($request, TRUE)) {
99 throw new AccessDeniedHttpException('The used authentication method is not allowed on this route.'); 98 throw new AccessDeniedHttpException('The used authentication method is not allowed on this route.');
100 } 99 }
101 } 100 }
110 * 109 *
111 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event 110 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
112 * The exception event. 111 * The exception event.
113 */ 112 */
114 public function onExceptionSendChallenge(GetResponseForExceptionEvent $event) { 113 public function onExceptionSendChallenge(GetResponseForExceptionEvent $event) {
115 if (isset($this->challengeProvider) && $event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) { 114 if (isset($this->challengeProvider) && $event->isMasterRequest()) {
116 $request = $event->getRequest(); 115 $request = $event->getRequest();
117 $exception = $event->getException(); 116 $exception = $event->getException();
118 if ($exception instanceof AccessDeniedHttpException && !$this->authenticationProvider->applies($request) && (!isset($this->filter) || $this->filter->appliesToRoutedRequest($request, FALSE))) { 117 if ($exception instanceof AccessDeniedHttpException && !$this->authenticationProvider->applies($request) && (!isset($this->filter) || $this->filter->appliesToRoutedRequest($request, FALSE))) {
119 $challenge_exception = $this->challengeProvider->challengeException($request, $exception); 118 $challenge_exception = $this->challengeProvider->challengeException($request, $exception);
120 if ($challenge_exception) { 119 if ($challenge_exception) {
127 /** 126 /**
128 * Detect disallowed authentication methods on access denied exceptions. 127 * Detect disallowed authentication methods on access denied exceptions.
129 * 128 *
130 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event 129 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
131 */ 130 */
132 public function _onExceptionAccessDenied(GetResponseForExceptionEvent $event) { 131 public function onExceptionAccessDenied(GetResponseForExceptionEvent $event) {
133 if (isset($this->filter) && $event->isMasterRequest()) { 132 if (isset($this->filter) && $event->isMasterRequest()) {
134 $request = $event->getRequest(); 133 $request = $event->getRequest();
135 $exception = $event->getException(); 134 $exception = $event->getException();
136 if ($exception instanceof AccessDeniedHttpException && $this->authenticationProvider->applies($request) && !$this->filter->appliesToRoutedRequest($request, TRUE)) { 135 if ($exception instanceof AccessDeniedHttpException && $this->authenticationProvider->applies($request) && !$this->filter->appliesToRoutedRequest($request, TRUE)) {
137 $event->setException(new AccessDeniedHttpException('The used authentication method is not allowed on this route.', $exception)); 136 $event->setException(new AccessDeniedHttpException('The used authentication method is not allowed on this route.', $exception));
150 $events[KernelEvents::REQUEST][] = ['onKernelRequestAuthenticate', 300]; 149 $events[KernelEvents::REQUEST][] = ['onKernelRequestAuthenticate', 300];
151 150
152 // Access check must be performed after routing. 151 // Access check must be performed after routing.
153 $events[KernelEvents::REQUEST][] = ['onKernelRequestFilterProvider', 31]; 152 $events[KernelEvents::REQUEST][] = ['onKernelRequestFilterProvider', 31];
154 $events[KernelEvents::EXCEPTION][] = ['onExceptionSendChallenge', 75]; 153 $events[KernelEvents::EXCEPTION][] = ['onExceptionSendChallenge', 75];
155 $events[KernelEvents::EXCEPTION][] = ['_onExceptionAccessDenied', 80]; 154 $events[KernelEvents::EXCEPTION][] = ['onExceptionAccessDenied', 80];
156 return $events; 155 return $events;
157 } 156 }
158 157
159 } 158 }