diff options
author | Fabien Potencier <fabien.potencier@gmail.com> | 2012-06-10 21:58:31 +0200 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2012-06-10 21:58:31 +0200 |
commit | cfbb58936b3b9e9b5c31d191ed8056acd2932eb8 (patch) | |
tree | f42aa00bd4f2f0ee8d7e28df1f9af7e4080e927b /Core/Validator | |
parent | 0d0202e9af2e2530467dcbe716cf8e8e50df1ef3 (diff) | |
download | symfony-security-cfbb58936b3b9e9b5c31d191ed8056acd2932eb8.zip symfony-security-cfbb58936b3b9e9b5c31d191ed8056acd2932eb8.tar.gz symfony-security-cfbb58936b3b9e9b5c31d191ed8056acd2932eb8.tar.bz2 |
moved the UserPassword validator from the security bundle to the security component to make it reusable outside the full-stack framework
Diffstat (limited to 'Core/Validator')
-rw-r--r-- | Core/Validator/Constraint/UserPassword.php | 27 | ||||
-rw-r--r-- | Core/Validator/Constraint/UserPasswordValidator.php | 46 |
2 files changed, 73 insertions, 0 deletions
diff --git a/Core/Validator/Constraint/UserPassword.php b/Core/Validator/Constraint/UserPassword.php new file mode 100644 index 0000000..ef6e1ec --- /dev/null +++ b/Core/Validator/Constraint/UserPassword.php @@ -0,0 +1,27 @@ +<?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\Core\Validator\Constraint; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + */ +class UserPassword extends Constraint +{ + public $message = 'This value should be the user current password'; + + public function validatedBy() + { + return 'security.validator.user_password'; + } +} diff --git a/Core/Validator/Constraint/UserPasswordValidator.php b/Core/Validator/Constraint/UserPasswordValidator.php new file mode 100644 index 0000000..a54906b --- /dev/null +++ b/Core/Validator/Constraint/UserPasswordValidator.php @@ -0,0 +1,46 @@ +<?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\Core\Validator\Constraint; + +use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Security\Core\SecurityContextInterface; +use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; +use Symfony\Component\Validator\Exception\ConstraintDefinitionException; + +class UserPasswordValidator extends ConstraintValidator +{ + private $securityContext; + private $encoderFactory; + + public function __construct(SecurityContextInterface $securityContext, EncoderFactoryInterface $encoderFactory) + { + $this->securityContext = $securityContext; + $this->encoderFactory = $encoderFactory; + } + + public function validate($password, Constraint $constraint) + { + $user = $this->securityContext->getToken()->getUser(); + + if (!$user instanceof UserInterface) { + throw new ConstraintDefinitionException('The User must extend UserInterface'); + } + + $encoder = $this->encoderFactory->getEncoder($user); + + if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) { + $this->context->addViolation($constraint->message); + } + } +} |