Skip to content

Commit

Permalink
[DX] Moved constants to a final class
Browse files Browse the repository at this point in the history
  • Loading branch information
Iltar van der Berg committed Sep 29, 2014
1 parent 1c24006 commit b23084a
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 40 deletions.
Expand Up @@ -17,8 +17,8 @@
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Security\Core\Security;

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

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

$event->setData(array_replace((array) $event->getData(), array(
'username' => $request->getSession()->get(SecurityContextInterface::LAST_USERNAME),
'username' => $request->getSession()->get(Security::LAST_USERNAME),
)));
});
}
Expand Down
Expand Up @@ -12,11 +12,11 @@
namespace Symfony\Component\Security\Core;

/**
* The SecuritySessionStorageInterface.
* This class holds security information.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface SecuritySessionStorageInterface
final class Security
{
const ACCESS_DENIED_ERROR = '_security.403_error';
const AUTHENTICATION_ERROR = '_security.last_error';
Expand Down
Expand Up @@ -20,6 +20,9 @@
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
interface SecurityContextInterface extends TokenStorageInterface, AuthorizationCheckerInterface, SecuritySessionStorageInterface
interface SecurityContextInterface extends TokenStorageInterface, AuthorizationCheckerInterface
{
const ACCESS_DENIED_ERROR = Security::ACCESS_DENIED_ERROR;
const AUTHENTICATION_ERROR = Security::AUTHENTICATION_ERROR;
const LAST_USERNAME = Security::LAST_USERNAME;
}
Expand Up @@ -11,10 +11,10 @@

namespace Symfony\Component\Security\Http\Authentication;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;

/**
* Extracts Security Errors from Request
Expand Down Expand Up @@ -46,13 +46,13 @@ public function getLastAuthenticationError($clearSession = true)
$session = $request->getSession();
$authenticationException = null;

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

if ($clearSession) {
$session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
$session->remove(Security::AUTHENTICATION_ERROR);
}
}

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

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

/**
Expand Down
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\HttpUtils;

/**
Expand Down Expand Up @@ -96,7 +96,7 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
}

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

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

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

return $this->httpUtils->createRedirectResponse($request, $this->options['failure_path']);
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
Expand Down Expand Up @@ -218,8 +219,8 @@ private function onSuccess(Request $request, TokenInterface $token)
$this->securityContext->setToken($token);

$session = $request->getSession();
$session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
$session->remove(SecurityContextInterface::LAST_USERNAME);
$session->remove(Security::AUTHENTICATION_ERROR);
$session->remove(Security::LAST_USERNAME);

if (null !== $this->dispatcher) {
$loginEvent = new InteractiveLoginEvent($request, $token);
Expand Down
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
Expand Down Expand Up @@ -146,7 +147,7 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
}
} elseif (null !== $this->errorPage) {
$subRequest = $this->httpUtils->createRequest($event->getRequest(), $this->errorPage);
$subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception);
$subRequest->attributes->set(Security::ACCESS_DENIED_ERROR, $exception);

$event->setResponse($event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true));
}
Expand Down
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\SimpleFormAuthenticatorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
Expand Down Expand Up @@ -114,7 +115,7 @@ protected function attemptAuthentication(Request $request)
$password = $request->get($this->options['password_parameter'], null, true);
}

$request->getSession()->set(SecurityContextInterface::LAST_USERNAME, $username);
$request->getSession()->set(Security::LAST_USERNAME, $username);

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

Expand Down
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

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

$request->getSession()->set(SecurityContextInterface::LAST_USERNAME, $username);
$request->getSession()->set(Security::LAST_USERNAME, $username);

return $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $password, $this->providerKey));
}
Expand Down
15 changes: 7 additions & 8 deletions src/Symfony/Component/Security/Http/HttpUtils.php
Expand Up @@ -11,15 +11,14 @@

namespace Symfony\Component\Security\Http;

use Symfony\Component\Security\Core\SecurityContextInterface;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Security\Core\Security;

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

if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
$newRequest->attributes->set(SecurityContextInterface::AUTHENTICATION_ERROR, $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR));
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
$newRequest->attributes->set(Security::AUTHENTICATION_ERROR, $request->attributes->get(Security::AUTHENTICATION_ERROR));
}
if ($request->attributes->has(SecurityContextInterface::ACCESS_DENIED_ERROR)) {
$newRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $request->attributes->get(SecurityContextInterface::ACCESS_DENIED_ERROR));
if ($request->attributes->has(Security::ACCESS_DENIED_ERROR)) {
$newRequest->attributes->set(Security::ACCESS_DENIED_ERROR, $request->attributes->get(Security::ACCESS_DENIED_ERROR));
}
if ($request->attributes->has(SecurityContextInterface::LAST_USERNAME)) {
$newRequest->attributes->set(SecurityContextInterface::LAST_USERNAME, $request->attributes->get(SecurityContextInterface::LAST_USERNAME));
if ($request->attributes->has(Security::LAST_USERNAME)) {
$newRequest->attributes->set(Security::LAST_USERNAME, $request->attributes->get(Security::LAST_USERNAME));
}

return $newRequest;
Expand Down
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Component\Security\Http\Tests\Authentication;

use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class DefaultAuthenticationFailureHandlerTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -47,7 +47,7 @@ public function testForward()

$subRequest = $this->getRequest();
$subRequest->attributes->expects($this->once())
->method('set')->with(SecurityContextInterface::AUTHENTICATION_ERROR, $this->exception);
->method('set')->with(Security::AUTHENTICATION_ERROR, $this->exception);
$this->httpUtils->expects($this->once())
->method('createRequest')->with($this->request, '/login')
->will($this->returnValue($subRequest));
Expand Down Expand Up @@ -79,7 +79,7 @@ public function testRedirect()
public function testExceptionIsPersistedInSession()
{
$this->session->expects($this->once())
->method('set')->with(SecurityContextInterface::AUTHENTICATION_ERROR, $this->exception);
->method('set')->with(Security::AUTHENTICATION_ERROR, $this->exception);

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

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

$this->httpUtils->expects($this->once())
->method('createRequest')->with($this->request, '/login')
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Security/Http/Tests/HttpUtilsTest.php
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\HttpUtils;

class HttpUtilsTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -126,9 +126,9 @@ public function testCreateRequestPassesSecurityContextAttributesToTheNewRequest(
public function provideSecurityContextAttributes()
{
return array(
array(SecurityContextInterface::AUTHENTICATION_ERROR),
array(SecurityContextInterface::ACCESS_DENIED_ERROR),
array(SecurityContextInterface::LAST_USERNAME),
array(Security::AUTHENTICATION_ERROR),
array(Security::ACCESS_DENIED_ERROR),
array(Security::LAST_USERNAME),
);
}

Expand Down
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Tests\Core;

use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\Security;

class SecurityContextInterfaceTest extends \PHPUnit_Framework_TestCase
{
/**
* Test if the BC Layer is working as intended
*
* @deprecated Deprecated since version 2.6, to be removed in 3.0.
*/
public function testConstantSync()
{
$this->assertSame(Security::ACCESS_DENIED_ERROR, SecurityContextInterface::ACCESS_DENIED_ERROR);
$this->assertSame(Security::AUTHENTICATION_ERROR, SecurityContextInterface::AUTHENTICATION_ERROR);
$this->assertSame(Security::LAST_USERNAME, SecurityContextInterface::LAST_USERNAME);
}
}

0 comments on commit b23084a

Please sign in to comment.