Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5790859

Browse filesBrowse files
committed
Rework firewall access denied rule
1 parent c8d6dec commit 5790859
Copy full SHA for 5790859

File tree

2 files changed

+57
-10
lines changed
Filter options

2 files changed

+57
-10
lines changed

‎src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php
+1-3Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
131131
} catch (\Exception $e) {
132132
$event->setException($e);
133133
}
134-
135-
return;
136134
}
137135

138136
if (null !== $this->logger) {
@@ -150,7 +148,7 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
150148
$subRequest = $this->httpUtils->createRequest($event->getRequest(), $this->errorPage);
151149
$subRequest->attributes->set(Security::ACCESS_DENIED_ERROR, $exception);
152150

153-
$event->setResponse($event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true));
151+
$event->setResponse($event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST));
154152
$event->allowCustomResponseCode();
155153
}
156154
} catch (\Exception $e) {

‎src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php
+56-7Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,8 @@ public function testAccessDeniedExceptionFullFledgedAndWithAccessDeniedHandlerAn
130130
{
131131
$event = $this->createEvent($exception);
132132

133-
$accessDeniedHandler = $this->getMockBuilder('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface')->getMock();
134-
$accessDeniedHandler->expects($this->once())->method('handle')->will($this->returnValue(new Response('error')));
133+
$listener = $this->createExceptionListener(null, $this->createTrustResolver(true), null, null, null, $this->createCustomAccessDeniedHandler(new Response('error')));
135134

136-
$listener = $this->createExceptionListener(null, $this->createTrustResolver(true), null, null, null, $accessDeniedHandler);
137135
$listener->onKernelException($event);
138136

139137
$this->assertEquals('error', $event->getResponse()->getContent());
@@ -147,16 +145,51 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
147145
{
148146
$event = $this->createEvent($exception);
149147

150-
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
151-
$tokenStorage->expects($this->once())->method('getToken')->will($this->returnValue($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()));
152-
153-
$listener = $this->createExceptionListener($tokenStorage, $this->createTrustResolver(false), null, $this->createEntryPoint());
148+
$listener = $this->createExceptionListener($this->createTokenStorage(), $this->createTrustResolver(false), null, $this->createEntryPoint());
154149
$listener->onKernelException($event);
155150

156151
$this->assertEquals('OK', $event->getResponse()->getContent());
157152
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
158153
}
159154

155+
/**
156+
* @dataProvider getAccessDeniedExceptionProvider
157+
*/
158+
public function testAccessDeniedExceptionNotFullFledgedAndWithAccessDeniedHandlerAndWithoutErrorPage(\Exception $exception, \Exception $eventException = null)
159+
{
160+
$event = $this->createEvent($exception);
161+
162+
$listener = $this->createExceptionListener($this->createTokenStorage(), $this->createTrustResolver(false), null, $this->createEntryPoint(), null, $this->createCustomAccessDeniedHandler(new Response('denied', 403)));
163+
$listener->onKernelException($event);
164+
165+
$this->assertEquals('denied', $event->getResponse()->getContent());
166+
$this->assertEquals(403, $event->getResponse()->getStatusCode());
167+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
168+
}
169+
170+
/**
171+
* @dataProvider getAccessDeniedExceptionProvider
172+
*/
173+
public function testAccessDeniedExceptionNotFullFledgedAndWithoutAccessDeniedHandlerAndWithErrorPage(\Exception $exception, \Exception $eventException = null)
174+
{
175+
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
176+
$kernel->expects($this->once())->method('handle')->will($this->returnValue(new Response('Unauthorized', 401)));
177+
178+
$event = $this->createEvent($exception, $kernel);
179+
180+
$httpUtils = $this->getMockBuilder('Symfony\Component\Security\Http\HttpUtils')->getMock();
181+
$httpUtils->expects($this->once())->method('createRequest')->will($this->returnValue(Request::create('/error')));
182+
183+
$listener = $this->createExceptionListener($this->createTokenStorage(), $this->createTrustResolver(true), $httpUtils, null, '/error');
184+
$listener->onKernelException($event);
185+
186+
$this->assertTrue($event->isAllowingCustomResponseCode());
187+
188+
$this->assertEquals('Unauthorized', $event->getResponse()->getContent());
189+
$this->assertEquals(401, $event->getResponse()->getStatusCode());
190+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
191+
}
192+
160193
public function getAccessDeniedExceptionProvider()
161194
{
162195
return [
@@ -168,6 +201,22 @@ public function getAccessDeniedExceptionProvider()
168201
];
169202
}
170203

204+
private function createTokenStorage()
205+
{
206+
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
207+
$tokenStorage->expects($this->once())->method('getToken')->will($this->returnValue($this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock()));
208+
209+
return $tokenStorage;
210+
}
211+
212+
private function createCustomAccessDeniedHandler(Response $response)
213+
{
214+
$accessDeniedHandler = $this->getMockBuilder('Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface')->getMock();
215+
$accessDeniedHandler->expects($this->once())->method('handle')->will($this->returnValue($response));
216+
217+
return $accessDeniedHandler;
218+
}
219+
171220
private function createEntryPoint(Response $response = null)
172221
{
173222
$entryPoint = $this->getMockBuilder('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')->getMock();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.