|
| 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\Form\Extension\PasswordHasher\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\Form\Exception\InvalidConfigurationException; |
| 15 | +use Symfony\Component\Form\Extension\Core\Type\RepeatedType; |
| 16 | +use Symfony\Component\Form\FormEvent; |
| 17 | +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
| 18 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
| 19 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 20 | +use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Sébastien Alfaiate <s.alfaiate@webarea.fr> |
| 24 | + */ |
| 25 | +class PasswordHasherListener |
| 26 | +{ |
| 27 | + private array $passwords = []; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + private UserPasswordHasherInterface $passwordHasher, |
| 31 | + private ?PropertyAccessorInterface $propertyAccessor = null, |
| 32 | + ) { |
| 33 | + $this->propertyAccessor ??= PropertyAccess::createPropertyAccessor(); |
| 34 | + } |
| 35 | + |
| 36 | + public function registerPassword(FormEvent $event) |
| 37 | + { |
| 38 | + $form = $event->getForm(); |
| 39 | + $parentForm = $form->getParent(); |
| 40 | + $mapped = $form->getConfig()->getMapped(); |
| 41 | + |
| 42 | + if ($parentForm && $parentForm->getConfig()->getType()->getInnerType() instanceof RepeatedType) { |
| 43 | + $mapped = $parentForm->getConfig()->getMapped(); |
| 44 | + $parentForm = $parentForm->getParent(); |
| 45 | + } |
| 46 | + |
| 47 | + if ($mapped) { |
| 48 | + throw new InvalidConfigurationException('The "hash_property_path" option cannot be used on mapped field.'); |
| 49 | + } |
| 50 | + |
| 51 | + if (!($user = $parentForm?->getData()) || !$user instanceof PasswordAuthenticatedUserInterface) { |
| 52 | + throw new InvalidConfigurationException(sprintf('The "hash_property_path" option only supports "%s" objects, "%s" given.', PasswordAuthenticatedUserInterface::class, get_debug_type($user))); |
| 53 | + } |
| 54 | + |
| 55 | + $this->passwords[] = [ |
| 56 | + 'user' => $user, |
| 57 | + 'property_path' => $form->getConfig()->getOption('hash_property_path'), |
| 58 | + 'password' => $event->getData(), |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + public function hashPasswords(FormEvent $event) |
| 63 | + { |
| 64 | + $form = $event->getForm(); |
| 65 | + |
| 66 | + if (!$form->isRoot()) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if ($form->isValid()) { |
| 71 | + foreach ($this->passwords as $password) { |
| 72 | + $this->propertyAccessor->setValue( |
| 73 | + $password['user'], |
| 74 | + $password['property_path'], |
| 75 | + $this->passwordHasher->hashPassword($password['user'], $password['password']) |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $this->passwords = []; |
| 81 | + } |
| 82 | +} |
0 commit comments