Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
fixed Symfony Security Component deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 20, 2015
1 parent 9d688a9 commit 12571d3
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/Silex/Application/SecurityTrait.php
Expand Up @@ -30,7 +30,7 @@ trait SecurityTrait
*/
public function user()
{
if (null === $token = $this['security']->getToken()) {
if (null === $token = $this['security.token_storage']->getToken()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Silex/Provider/RememberMeServiceProvider.php
Expand Up @@ -76,7 +76,7 @@ public function register(Application $app)
$app['security.authentication_listener.remember_me._proto'] = $app->protect(function ($providerKey) use ($app) {
return $app->share(function () use ($app, $providerKey) {
$listener = new RememberMeListener(
$app['security'],
$app['security.token_storage'],
$app['security.remember_me.service.'.$providerKey],
$app['security.authentication_manager'],
$app['logger'],
Expand Down
64 changes: 47 additions & 17 deletions src/Silex/Provider/SecurityServiceProvider.php
Expand Up @@ -15,6 +15,7 @@
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Symfony\Component\Security\Core\User\UserChecker;
Expand All @@ -27,6 +28,8 @@
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
Expand Down Expand Up @@ -72,9 +75,31 @@ public function register(Application $app)
$app['security.access_rules'] = array();
$app['security.hide_user_not_found'] = true;

$app['security'] = $app->share(function ($app) {
return new SecurityContext($app['security.authentication_manager'], $app['security.access_manager']);
});
$r = new \ReflectionMethod('Symfony\Component\Security\Http\Firewall\ContextListener', '__construct');
$params = $r->getParameters();
if ('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface' === $params[0]->getClass()->getName()) {
$app['security.authorization_checker'] = $app->share(function ($app) {
return new AuthorizationChecker($app['security.token_storage'], $app['security.authentication_manager'], $app['security.access_manager']);
});

$app['security.token_storage'] = $app->share(function ($app) {
return new TokenStorage();
});

$app['security'] = $app->share(function ($app) {
// Deprecated, to be removed in 2.0
return new SecurityContext($app['security.token_storage'], $app['security.authorization_checker']);
});
} else {
$app['security.token_storage'] = $app['security.authorization_checker'] = $app->share(function ($app) {
return $app['security'];
});

$app['security'] = $app->share(function ($app) {
// Deprecated, to be removed in 2.0
return new SecurityContext($app['security.authentication_manager'], $app['security.access_manager']);
});
}

$app['security.authentication_manager'] = $app->share(function ($app) {
$manager = new AuthenticationProviderManager($app['security.authentication_providers']);
Expand Down Expand Up @@ -271,7 +296,7 @@ public function register(Application $app)

$app['security.access_listener'] = $app->share(function ($app) {
return new AccessListener(
$app['security'],
$app['security.token_storage'],
$app['security.access_manager'],
$app['security.access_map'],
$app['security.authentication_manager'],
Expand Down Expand Up @@ -306,14 +331,19 @@ public function register(Application $app)
});

$app['security.last_error'] = $app->protect(function (Request $request) {
if ($request->attributes->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
return $request->attributes->get(SecurityContextInterface::AUTHENTICATION_ERROR)->getMessage();
if (class_exists('Symfony\Component\Security\Core\Security')) {
$error = Security::AUTHENTICATION_ERROR;
} else {
$error = SecurityContextInterface::AUTHENTICATION_ERROR;
}
if ($request->attributes->has($error)) {
return $request->attributes->get($error)->getMessage();
}

$session = $request->getSession();
if ($session && $session->has(SecurityContextInterface::AUTHENTICATION_ERROR)) {
$error = $session->get(SecurityContextInterface::AUTHENTICATION_ERROR)->getMessage();
$session->remove(SecurityContextInterface::AUTHENTICATION_ERROR);
if ($session && $session->has($error)) {
$error = $session->get($error)->getMessage();
$session->remove($error);

return $error;
}
Expand All @@ -324,7 +354,7 @@ public function register(Application $app)
$app['security.context_listener._proto'] = $app->protect(function ($providerKey, $userProviders) use ($app) {
return $app->share(function () use ($app, $userProviders, $providerKey) {
return new ContextListener(
$app['security'],
$app['security.token_storage'],
$userProviders,
$providerKey,
$app['logger'],
Expand All @@ -347,7 +377,7 @@ public function register(Application $app)
$app['security.exception_listener._proto'] = $app->protect(function ($entryPoint, $name) use ($app) {
return $app->share(function () use ($app, $entryPoint, $name) {
return new ExceptionListener(
$app['security'],
$app['security.token_storage'],
$app['security.trust_resolver'],
$app['security.http_utils'],
$name,
Expand Down Expand Up @@ -401,7 +431,7 @@ public function register(Application $app)
}

return new $class(
$app['security'],
$app['security.token_storage'],
$app['security.authentication_manager'],
isset($app['security.session_strategy.'.$name]) ? $app['security.session_strategy.'.$name] : $app['security.session_strategy'],
$app['security.http_utils'],
Expand All @@ -419,7 +449,7 @@ public function register(Application $app)
$app['security.authentication_listener.http._proto'] = $app->protect(function ($providerKey, $options) use ($app) {
return $app->share(function () use ($app, $providerKey, $options) {
return new BasicAuthenticationListener(
$app['security'],
$app['security.token_storage'],
$app['security.authentication_manager'],
$providerKey,
$app['security.entry_point.'.$providerKey.'.http'],
Expand All @@ -431,7 +461,7 @@ public function register(Application $app)
$app['security.authentication_listener.anonymous._proto'] = $app->protect(function ($providerKey, $options) use ($app) {
return $app->share(function () use ($app, $providerKey, $options) {
return new AnonymousAuthenticationListener(
$app['security'],
$app['security.token_storage'],
$providerKey,
$app['logger']
);
Expand Down Expand Up @@ -460,7 +490,7 @@ public function register(Application $app)
}

$listener = new LogoutListener(
$app['security'],
$app['security.token_storage'],
$app['security.http_utils'],
$app['security.authentication.logout_handler.'.$name],
$options,
Expand All @@ -476,7 +506,7 @@ public function register(Application $app)
$app['security.authentication_listener.switch_user._proto'] = $app->protect(function ($name, $options) use ($app, $that) {
return $app->share(function () use ($app, $name, $options, $that) {
return new SwitchUserListener(
$app['security'],
$app['security.token_storage'],
$app['security.user_provider.'.$name],
$app['security.user_checker'],
$name,
Expand Down Expand Up @@ -524,7 +554,7 @@ public function register(Application $app)

if (isset($app['validator'])) {
$app['security.validator.user_password_validator'] = $app->share(function ($app) {
return new UserPasswordValidator($app['security'], $app['security.encoder_factory']);
return new UserPasswordValidator($app['security.token_storage'], $app['security.encoder_factory']);
});

if (!isset($app['validator.validator_service_ids'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Silex/Provider/TwigServiceProvider.php
Expand Up @@ -60,8 +60,8 @@ public function register(Application $app)
$twig->addExtension(new TranslationExtension($app['translator']));
}

if (isset($app['security'])) {
$twig->addExtension(new SecurityExtension($app['security']));
if (isset($app['security.authorization_checker'])) {
$twig->addExtension(new SecurityExtension($app['security.authorization_checker']));
}

if (isset($app['fragment.handler'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Silex/Route/SecurityTrait.php
Expand Up @@ -23,7 +23,7 @@ trait SecurityTrait
public function secure($roles)
{
$this->before(function ($request, $app) use ($roles) {
if (!$app['security']->isGranted($roles)) {
if (!$app['security.authorization_checker']->isGranted($roles)) {
throw new AccessDeniedException();
}
});
Expand Down
44 changes: 13 additions & 31 deletions tests/Silex/Tests/Application/SecurityTraitTest.php
Expand Up @@ -13,6 +13,7 @@

use Silex\Provider\SecurityServiceProvider;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\HttpFoundation\Request;

/**
Expand All @@ -28,7 +29,9 @@ public function testUser()
{
$request = Request::create('/');

$app = $this->createApplication();
$app = $this->createApplication(array(
'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
));
$app->get('/', function () { return 'foo'; });
$app->handle($request);
$this->assertNull($app->user());
Expand All @@ -44,15 +47,7 @@ public function testUserWithNoToken()
{
$request = Request::create('/');

$app = new SecurityApplication();
$app['security'] = $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContext')
->disableOriginalConstructor()
->getMock();

$app['security']->expects($this->any())
->method('getToken')
->will($this->returnValue(null));

$app = $this->createApplication();
$app->get('/', function () { return 'foo'; });
$app->handle($request);
$this->assertNull($app->user());
Expand All @@ -62,22 +57,9 @@ public function testUserWithInvalidUser()
{
$request = Request::create('/');

$app = new SecurityApplication();
$app['security'] = $this->getMockBuilder('Symfony\Component\Security\Core\SecurityContext')
->disableOriginalConstructor()
->getMock();

$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken')
->disableOriginalConstructor()
->getMock();

$token->expects($this->once())
->method('getUser')
->will($this->returnValue(array()));

$app['security']->expects($this->any())
->method('getToken')
->will($this->returnValue($token));
$app = $this->createApplication();
$app->boot();
$app['security.token_storage']->setToken(new UsernamePasswordToken('foo', 'foo', 'foo'));

$app->get('/', function () { return 'foo'; });
$app->handle($request);
Expand All @@ -86,22 +68,22 @@ public function testUserWithInvalidUser()

public function testEncodePassword()
{
$app = $this->createApplication();
$app = $this->createApplication(array(
'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
));

$user = new User('foo', 'bar');
$this->assertEquals('5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg==', $app->encodePassword($user, 'foo'));
}

public function createApplication()
public function createApplication($users = array())
{
$app = new SecurityApplication();
$app->register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'http' => true,
'users' => array(
'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
),
'users' => $users,
),
),
));
Expand Down
4 changes: 2 additions & 2 deletions tests/Silex/Tests/Provider/RememberMeServiceProviderTest.php
Expand Up @@ -84,9 +84,9 @@ public function createApplication($authenticationMethod = 'form')
);

$app->get('/', function () use ($app) {
if ($app['security']->isGranted('IS_AUTHENTICATED_FULLY')) {
if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) {
return 'AUTHENTICATED_FULLY';
} elseif ($app['security']->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
} elseif ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return 'AUTHENTICATED_REMEMBERED';
} else {
return 'AUTHENTICATED_ANONYMOUSLY';
Expand Down
13 changes: 6 additions & 7 deletions tests/Silex/Tests/Provider/SecurityServiceProviderTest.php
Expand Up @@ -224,15 +224,15 @@ private function addFormAuthentication($app)
});

$app->get('/', function () use ($app) {
$user = $app['security']->getToken()->getUser();
$user = $app['security.token_storage']->getToken()->getUser();

$content = is_object($user) ? $user->getUsername() : 'ANONYMOUS';

if ($app['security']->isGranted('IS_AUTHENTICATED_FULLY')) {
if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) {
$content .= 'AUTHENTICATED';
}

if ($app['security']->isGranted('ROLE_ADMIN')) {
if ($app['security.authorization_checker']->isGranted('ROLE_ADMIN')) {
$content .= 'ADMIN';
}

Expand Down Expand Up @@ -269,15 +269,14 @@ private function addHttpAuthentication($app)
));

$app->get('/', function () use ($app) {
$user = $app['security']->getToken()->getUser();

$user = $app['security.token_storage']->getToken()->getUser();
$content = is_object($user) ? $user->getUsername() : 'ANONYMOUS';

if ($app['security']->isGranted('IS_AUTHENTICATED_FULLY')) {
if ($app['security.authorization_checker']->isGranted('IS_AUTHENTICATED_FULLY')) {
$content .= 'AUTHENTICATED';
}

if ($app['security']->isGranted('ROLE_ADMIN')) {
if ($app['security.authorization_checker']->isGranted('ROLE_ADMIN')) {
$content .= 'ADMIN';
}

Expand Down

0 comments on commit 12571d3

Please sign in to comment.