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 c6051e3

Browse filesBrowse files
committed
[SecurityBundle] register alias for argument for password hasher
1 parent 5955b14 commit c6051e3
Copy full SHA for c6051e3

File tree

Expand file treeCollapse file tree

3 files changed

+29
-3
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+29
-3
lines changed

‎src/Symfony/Bundle/SecurityBundle/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Register alias for argument for password hasher when the key is not a class name
8+
49
7.3
510
---
611

‎src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use Symfony\Component\PasswordHasher\Hasher\Pbkdf2PasswordHasher;
5050
use Symfony\Component\PasswordHasher\Hasher\PlaintextPasswordHasher;
5151
use Symfony\Component\PasswordHasher\Hasher\SodiumPasswordHasher;
52+
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
5253
use Symfony\Component\Routing\Loader\ContainerLoader;
5354
use Symfony\Component\Security\Core\Authorization\Strategy\AffirmativeStrategy;
5455
use Symfony\Component\Security\Core\Authorization\Strategy\ConsensusStrategy;
@@ -706,6 +707,17 @@ private function createHashers(array $hashers, ContainerBuilder $container): voi
706707
$hasherMap = [];
707708
foreach ($hashers as $class => $hasher) {
708709
$hasherMap[$class] = $this->createHasher($hasher);
710+
// The key is not a class, so we register an alias for argument to
711+
// ease getting the hasher
712+
if (!class_exists($class) && !interface_exists($class)) {
713+
$id = 'security.password_hasher.'.$class;
714+
$container
715+
->register($id, PasswordHasherInterface::class)
716+
->setFactory([new Reference('security.password_hasher_factory'), 'getPasswordHasher'])
717+
->setArgument(0, $class)
718+
;
719+
$container->registerAliasForArgument($id, PasswordHasherInterface::class, $class);
720+
}
709721
}
710722

711723
$container

‎src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\HttpFoundation\Request;
3030
use Symfony\Component\HttpFoundation\RequestMatcher\PathRequestMatcher;
3131
use Symfony\Component\HttpFoundation\Response;
32+
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
3233
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
3334
use Symfony\Component\Security\Core\Exception\AuthenticationException;
3435
use Symfony\Component\Security\Core\User\InMemoryUserChecker;
@@ -883,7 +884,7 @@ public function testCustomHasherWithMigrateFrom()
883884
$container->loadFromExtension('security', [
884885
'password_hashers' => [
885886
'legacy' => 'md5',
886-
'App\User' => [
887+
TestUserChecker::class => [
887888
'id' => 'App\Security\CustomHasher',
888889
'migrate_from' => 'legacy',
889890
],
@@ -895,11 +896,19 @@ public function testCustomHasherWithMigrateFrom()
895896

896897
$hashersMap = $container->getDefinition('security.password_hasher_factory')->getArgument(0);
897898

898-
$this->assertArrayHasKey('App\User', $hashersMap);
899-
$this->assertEquals($hashersMap['App\User'], [
899+
$this->assertArrayHasKey(TestUserChecker::class, $hashersMap);
900+
$this->assertEquals($hashersMap[TestUserChecker::class], [
900901
'instance' => new Reference('App\Security\CustomHasher'),
901902
'migrate_from' => ['legacy'],
902903
]);
904+
905+
$legacyAlias = \sprintf('%s $%s', PasswordHasherInterface::class, 'legacy');
906+
$this->assertTrue($container->hasAlias($legacyAlias));
907+
$definition = $container->getDefinition((string) $container->getAlias($legacyAlias));
908+
$this->assertSame(PasswordHasherInterface::class, $definition->getClass());
909+
910+
$this->assertFalse($container->hasAlias(\sprintf('%s $%s', PasswordHasherInterface::class, 'symfonyBundleSecurityBundleTestsDependencyInjectionTestUserChecker')));
911+
$this->assertFalse($container->hasAlias(\sprintf('.%s $%s', PasswordHasherInterface::class, TestUserChecker::class)));
903912
}
904913

905914
public function testAuthenticatorsDecoration()

0 commit comments

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