8000 minor #12074 [DX] Moved Security constants to a final class instead o… · symfony/symfony@2a8fed6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a8fed6

Browse files
committed
minor #12074 [DX] Moved Security constants to a final class instead of a long named interface (iltar)
This PR was merged into the 2.6-dev branch. Discussion ---------- [DX] Moved Security constants to a final class instead of a long named interface | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | symfony/symfony-docs#4188 This PR is based on feedback from the documentation repository. The DX suggestion was to rename the new `SecuritySessionStorageInterface` to `Security`. This would make it easier to use the constants before 2.6 is released. In this PR I have also update all usages of this constant because an open PR is now merged which used those constants. List of changes: - SecurityBundle, usage of constants - Security Component (core & http), usage of constants - Tests, usage of constants - Added a test to verify the sync from `Security` to `SecurityContextInterface` for BC purposes Commits ------- b23084a [DX] Moved constants to a final class
2 parents 1c24006 + b23084a commit 2a8fed6

File tree

13 files changed

+76
-40
lines changed

13 files changed

+76
-40
lines changed

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/CsrfFormLoginBundle/Form/UserLoginFormType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use Symfony\Component\Form\FormEvents;
1818
use Symfony\Component\Form\FormEvent;
1919
use Symfony\Component\HttpFoundation\Request;
20-
use Symfony\Component\Security\Core\SecurityContextInterface;
2120
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
21+
use Symfony\Component\Security\Core\Security;
2222

2323
/**
2424
* Form type for use with the Security component's form-based authentication
@@ -58,18 +58,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5858
* session for an authentication error and last username.
5959
*/
6060
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($request) {
61-
if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
62-
$error = $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR);
61+
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
62+
$error = $request->attributes->get(Security::AUTHENTICATION_ERROR);
6363
} else {
64-
$error = $request->getSession()->get(SecurityContextInterface::AUTHENTICATION_ERROR);
64+
$error = $request->getSession()->get(Security::AUTHENTICATION_ERROR);
6565
}
6666

6767
if ($error) {
6868
$event->getForm()->addError(new FormError($error->getMessage()));
6969
}
7070

7171
$event->setData(array_replace((array) $event->getData(), array(
72-
'username' => $request->getSession()->get(SecurityContextInterface::LAST_USERNAME),
72+
'username' => $request->getSession()->get(Security::LAST_USERNAME),
7373
)));
7474
});
7575
}

src/Symfony/Component/Security/Core/SecuritySessionStorageInterface.php renamed to src/Symfony/Component/Security/Core/Security.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
namespace Symfony\Component\Security\Core;
1313

1414
/**
15-
* The SecuritySessionStorageInterface.
15+
* This class holds security information.
1616
*
1717
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
1818
*/
19-
interface SecuritySessionStorageInterface
19+
final class Security
2020
{
2121
const ACCESS_DENIED_ERROR = '_security.403_error';
2222
const AUTHENTICATION_ERROR = '_security.last_error';

src/Symfony/Component/Security/Core/SecurityContextInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
2121
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
2222
*/
23-
interface SecurityContextInterface extends TokenStorageInterface, AuthorizationCheckerInterface, SecuritySessionStorageInterface
23+
interface SecurityContextInterface extends TokenStorageInterface, AuthorizationCheckerInterface
2424
{
25+
const ACCESS_DENIED_ERROR = Security::ACCESS_DENIED_ERROR;
26+
const AUTHENTICATION_ERROR = Security::AUTHENTICATION_ERROR;
27+
const LAST_USERNAME = Security::LAST_USERNAME;
2528
}

src/Symfony/Component/Security/Http/Authentication/AuthenticationUtils.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Symfony\Component\Security\Http\Authentication;
1313

14+
use Symfony\Component\HttpFoundation\Request;
1415
use Symfony\Component\HttpFoundation\RequestStack;
1516
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16-
use Symfony\Component\Security\Core\SecurityContextInterface;
17-
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\Security\Core\Security;
1818

1919
/**
2020
* Extracts Security Errors from Request
@@ -46,13 +46,13 @@ public function getLastAuthenticationError($clearSession = true)
4646
$session = $request->getSession();
4747
$authenticationException = null;
4848

49-
if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
50-
$authenticationException = $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR);
51-
} elseif ($session !== null && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
52-
$authenticationException = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR);
49+
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
50+
$authenticationException = $request->attributes->get(Security::AUTHENTICATION_ERROR);
51+
} elseif ($session !== null && $session->has(Security::AUTHENTICATION_ERROR)) {
52+
$authenticationException = $session->get(Security::AUTHENTICATION_ERROR);
5353

5454
if ($clearSession) {
55-
$session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
55+
$session->remove(Security::AUTHENTICATION_ERROR);
5656
}
5757
}
5858

@@ -66,7 +66,7 @@ public function getLastUsername()
6666
{
6767
$session = $this->getRequest()->getSession();
6868

69-
return null === $session ? '' : $session->get(SecurityContextInterface::LAST_USERNAME);
69+
return null === $session ? '' : $session->get(Security::LAST_USERNAME);
7070
}
7171

7272
/**

src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationFailureHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\HttpKernel\HttpKernelInterface;
1616
use Psr\Log\LoggerInterface;
1717
use Symfony\Component\Security\Core\Exception\AuthenticationException;
18-
use Symfony\Component\Security\Core\SecurityContextInterface;
18+
use Symfony\Component\Security\Core\Security;
1919
use Symfony\Component\Security\Http\HttpUtils;
2020

2121
/**
@@ -96,7 +96,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
9696
}
9797

9898
$subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
99-
$subRequest->attributes->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
99+
$subRequest->attributes->set(Security::AUTHENTICATION_ERROR, $exception);
100100

101101
return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
102102
}
@@ -105,7 +105,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
105105
$this->logger->debug(sprintf('Redirecting to %s', $this->options['failure_path']));
106106
}
107107

108-
$request->getSession()->set(SecurityContextInterface::AUTHENTICATION_ERROR, $exception);
108+
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
109109

110110
return $this->httpUtils->createRedirectResponse($request, $this->options['failure_path']);
111111
}

src/Symfony/Component/Security/Http/Firewall/AbstractAuthenticationListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
1616
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
1717
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
18+
use Symfony\Component\Security\Core\Security;
1819
use Symfony\Component\Security\Core\SecurityContextInterface;
1920
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
2021
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
@@ -218,8 +219,8 @@ private function onSuccess(Request $request, TokenInterface $token)
218219
$this->securityContext->setToken($token);
219220

220221
$session = $request->getSession();
221-
$session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
222-
$session->remove(SecurityContextInterface::LAST_USERNAME);
222+
$session->remove(Security::AUTHENTICATION_ERROR);
223+
$session->remove(Security::LAST_USERNAME);
223224

224225
if (null !== $this->dispatcher) {
225226
$loginEvent = new InteractiveLoginEvent($request, $token);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\Response;
1515
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
16+
use Symfony\Component\Security\Core\Security;
1617
use Symfony\Component\Security\Core\SecurityContextInterface;
1718
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
1819
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
@@ -146,7 +147,7 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
146147
}
147148
} elseif (null !== $this->errorPage) {
148149
$subRequest = $this->httpUtils->createRequest($event->getRequest(), $this->errorPage);
149-
$subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception);
150+
$subRequest->attributes->set(Security::ACCESS_DENIED_ERROR, $exception);
150151

151152
$event->setResponse($event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true));
152153
}

src/Symfony/Component/Security/Http/Firewall/SimpleFormAuthenticationListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
2424
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
2525
use Symfony\Component\Security\Core\Authentication\SimpleFormAuthenticatorInterface;
26+
use Symfony\Component\Security\Core\Security;
2627
use Symfony\Component\Security\Core\SecurityContextInterface;
2728
use Symfony\Component\Security\Http\HttpUtils;
2829
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
@@ -114,7 +115,7 @@ protected function attemptAuthentication(Request $request)
114115
$password = $request->get($this->options['password_parameter'], null, true);
115116
}
116117

117-
$request->getSession()->set(SecurityContextInterface::LAST_USERNAME, $username);
118+
$request->getSession()->set(Security::LAST_USERNAME, $username);
118119

119120
$token = $this->simpleAuthenticator->createToken($request, $username, $password, $this->providerKey);
120121

src/Symfony/Component/Security/Http/Firewall/UsernamePasswordFormAuthenticationListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
2626
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
2727
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
28+
use Symfony\Component\Security\Core\Security;
2829
use Symfony\Component\Security\Core\SecurityContextInterface;
2930
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
3031

@@ -93,7 +94,7 @@ protected function attemptAuthentication(Request $request)
9394
$password = $request->get($this->options['password_parameter'], null, true);
9495
}
9596

96-
$request->getSession()->set(SecurityContextInterface::LAST_USERNAME, $username);
97+
$request->getSession()->set(Security::LAST_USERNAME, $username);
9798

9899
return $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $password, $this->providerKey));
99100
}

src/Symfony/Component/Security/Http/HttpUtils.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
namespace Symfony\Component\Security\Http;
1313

14-
use Symfony\Component\Security\Core\SecurityContextInterface;
15-
1614
use Symfony\Component\HttpFoundation\Request;
1715
use Symfony\Component\HttpFoundation\RedirectResponse;
1816
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
1917
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
2018
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2119
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
2220
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
21+
use Symfony\Component\Security\Core\Security;
2322

2423
/**
2524
* Encapsulates the logic needed to create sub-requests, redirect the user, and match URLs.
@@ -77,14 +76,14 @@ public function createRequest(Request $request, $path)
7776
$newRequest->setSession($request->getSession());
7877
}
7978

80-
if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
81-
$newRequest->attributes->set(SecurityContextInterface::AUTHENTICATION_ERROR, $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR));
79+
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
80+
$newRequest->attributes->set(Security::AUTHENTICATION_ERROR, $request->attributes->get(Security::AUTHENTICATION_ERROR));
8281
}
83-
if ($request->attributes->has(SecurityContextInterface::ACCESS_DENIED_ERROR)) {
84-
$newRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $request->attributes->get(SecurityContextInterface::ACCESS_DENIED_ERROR));
82+
if ($request->attributes->has(Security::ACCESS_DENIED_ERROR)) {
83+
$newRequest->attributes->set(Security::ACCESS_DENIED_ERROR, $request->attributes->get(Security::ACCESS_DENIED_ERROR));
8584
}
86-
if ($request->attributes->has(SecurityContextInterface::LAST_USERNAME)) {
87-
$newRequest->attributes->set(SecurityContextInterface::LAST_USERNAME, $request->attributes->get(SecurityContextInterface::LAST_USERNAME));
85+
if ($request->attributes->has(Security::LAST_USERNAME)) {
86+
$newRequest->attributes->set(Security::LAST_USERNAME, $request->attributes->get(Security::LAST_USERNAME));
8887
}
8988

9089
return $newRequest;

src/Symfony/Component/Security/Http/Tests/Authentication/DefaultAuthenticationFailureHandlerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Security\Http\Tests\Authentication;
1313

1414
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
15-
use Symfony\Component\Security\Core\SecurityContextInterface;
15+
use Symfony\Component\Security\Core\Security;
1616
use Symfony\Component\HttpKernel\HttpKernelInterface;
1717

1818
class DefaultAuthenticationFailureHandlerTest extends \PHPUnit_Framework_TestCase
@@ -47,7 +47,7 @@ public function testForward()
4747

4848
$subRequest = $this->getRequest();
4949
$subRequest->attributes->expects($this->once())
50-
->method('set')->with(SecurityContextInterface::AUTHENTICATION_ERROR, $this->exception);
50+
->method('set')->with(Security::AUTHENTICATION_ERROR, $this->exception);
5151
$this->httpUtils->expects($this->once())
5252
->method('createRequest')->with($this->request, '/login')
5353
->will($this->returnValue($subRequest));
@@ -79,7 +79,7 @@ public function testRedirect()
7979
public function testExceptionIsPersistedInSession()
8080
{
8181
$this->session->expects($this->once())
82-
->method('set')->with(SecurityContextInterface::AUTHENTICATION_ERROR, $this->exception);
82+
->method('set')->with(Security::AUTHENTICATION_ERROR, $this->exception);
8383

8484
$handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, array(), $this->logger);
8585
$handler->onAuthenticationFailure($this->request, $this->exception);
@@ -91,7 +91,7 @@ public function testExceptionIsPassedInRequestOnForward()
9191

9292
$subRequest = $this->getRequest();
9393
$subRequest->attributes->expects($this->once())
94-
->method('set')->with(SecurityContextInterface::AUTHENTICATION_ERROR, $this->exception);
94+
->method('set')->with(Security::AUTHENTICATION_ERROR, $this->exception);
9595

9696
$this->httpUtils->expects($this->once())
9797
->method('createRequest')->with($this->request, '/login')

src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
1616
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
17-
use Symfony\Component\Security\Core\SecurityContextInterface;
17+
use Symfony\Component\Security\Core\Security;
1818
use Symfony\Component\Security\Http\HttpUtils;
1919

2020
class HttpUtilsTest extends \PHPUnit_Framework_TestCase
@@ -126,9 +126,9 @@ public function testCreateRequestPassesSecurityContextAttributesToTheNewRequest(
126126
public function provideSecurityContextAttributes()
127127
{
128128
return array(
129-
array(SecurityContextInterface::AUTHENTICATION_ERROR),
130-
array(SecurityContextInterface::ACCESS_DENIED_ERROR),
131-
array(SecurityContextInterface::LAST_USERNAME),
129+
array(Security::AUTHENTICATION_ERROR),
130+
array(Security::ACCESS_DENIED_ERROR),
131+
array(Security::LAST_USERNAME),
132132
);
133133
}
134134

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Tests\Core;
13+
14+
use Symfony\Component\Security\Core\SecurityContextInterface;
15+
use Symfony\Component\Security\Core\Security;
16+
17+
class SecurityContextInterfaceTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* Test if the BC Layer is working as intended
21+
*
22+
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
23+
*/
24+
public function testConstantSync()
25+
{
26+
$this->assertSame(Security::ACCESS_DENIED_ERROR, SecurityContextInterface::ACCESS_DENIED_ERROR);
27+
$this->assertSame(Security::AUTHENTICATION_ERROR, Se 4473 curityContextInterface::AUTHENTICATION_ERROR);
28+
$this->assertSame(Security::LAST_USERNAME, SecurityContextInterface::LAST_USERNAME);
29+
}
30+
}

0 commit comments

Comments
 (0)
0