Skip to content

Commit

Permalink
[AdminBundle] Allow new password hashing component to fix sf5 depreca…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
acrobat committed Oct 13, 2021
1 parent cd182fc commit 1108f4d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Kunstmaan\AdminBundle\DependencyInjection\Compiler;

use Kunstmaan\AdminBundle\Service\UserManager;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class InjectPasswordHasherPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->has('security.password_hasher_factory')) {
return;
}

$container->getDefinition(UserManager::class)->setArgument('$hasherFactory', $container->getDefinition('security.password_hasher_factory'));
}
}
2 changes: 2 additions & 0 deletions src/Kunstmaan/AdminBundle/KunstmaanAdminBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Kunstmaan\AdminBundle\DependencyInjection\Compiler\AdminPanelCompilerPass;
use Kunstmaan\AdminBundle\DependencyInjection\Compiler\DataCollectorPass;
use Kunstmaan\AdminBundle\DependencyInjection\Compiler\EnablePermissionsPass;
use Kunstmaan\AdminBundle\DependencyInjection\Compiler\InjectPasswordHasherPass;
use Kunstmaan\AdminBundle\DependencyInjection\Compiler\MenuCompilerPass;
use Kunstmaan\AdminBundle\DependencyInjection\KunstmaanAdminExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -28,6 +29,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new AddLogProcessorsCompilerPass());
$container->addCompilerPass(new DataCollectorPass());
$container->addCompilerPass(new EnablePermissionsPass());
$container->addCompilerPass(new InjectPasswordHasherPass());

$container->registerExtension(new KunstmaanAdminExtension());
}
Expand Down
24 changes: 18 additions & 6 deletions src/Kunstmaan/AdminBundle/Service/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Kunstmaan\AdminBundle\Entity\UserInterface;
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;

class UserManager
Expand All @@ -13,14 +14,19 @@ class UserManager
private $em;
/** @var string */
private $class;
/** @var string */
/** @var EncoderFactoryInterface|PasswordHasherFactoryInterface */
private $encoderFactory;

public function __construct(EncoderFactoryInterface $encoderFactory, EntityManagerInterface $em, string $class)
public function __construct($hasherFactory, EntityManagerInterface $em, string $class)
{
//NEXT_MAJOR When symfony <5.3 is removed, add PasswordHasherFactoryInterface typehint and remove BC layer and compiler pass
if (!$hasherFactory instanceof EncoderFactoryInterface && !$hasherFactory instanceof PasswordHasherFactoryInterface) {
throw new \InvalidArgumentException(sprintf('The "$hasherFactory" parameter should be instance of "%s" or "%s"', EncoderFactoryInterface::class, PasswordHasherFactoryInterface::class));
}

$this->em = $em;
$this->class = $class;
$this->encoderFactory = $encoderFactory;
$this->encoderFactory = $hasherFactory;
}

public function deleteUser(UserInterface $user): void
Expand Down Expand Up @@ -66,10 +72,16 @@ public function updatePassword(UserInterface $user, bool $andFlush = true): void
return;
}

$encoder = $this->encoderFactory->getEncoder($user);
$user->setSalt(null);
if ($this->encoderFactory instanceof EncoderFactoryInterface) {
$encoder = $this->encoderFactory->getEncoder($user);
$user->setSalt(null);

$hashedPassword = $encoder->encodePassword($plainPassword, $user->getSalt());
} else {
$hasher = $this->encoderFactory->getPasswordHasher($user);
$hashedPassword = $hasher->hash($hasher);
}

$hashedPassword = $encoder->encodePassword($plainPassword, $user->getSalt());
$user->setPassword($hashedPassword);
$user->setPasswordChanged(true);
$user->setConfirmationToken(null);
Expand Down
3 changes: 2 additions & 1 deletion src/Kunstmaan/AdminBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@
"kunstmaan/user-management-bundle": "^5.9|^6.0",
"kunstmaan/media-bundle": "^5.9|^6.0",
"doctrine/doctrine-migrations-bundle": "^1.3|^2.1",
"symfony/mailer": "For symfony mailer support in the authentication mailer (^4.4)"
"symfony/mailer": "For symfony mailer support in the authentication mailer (^4.4)",
"symfony/password-hasher": "Use the new password-hasher component in the authentication system"
},
"require-dev": {
"kunstmaan/media-bundle": "^5.9|^6.0",
Expand Down

0 comments on commit 1108f4d

Please sign in to comment.