diff options
author | Hugo Hamon <hugo.hamon@sensio.com> | 2013-02-04 13:20:14 +0100 |
---|---|---|
committer | Hugo Hamon <hugo.hamon@sensio.com> | 2013-02-04 13:20:14 +0100 |
commit | c4fb5a816e5d46b52908a0ff1b84b4ce9d147cba (patch) | |
tree | 854f63714f1c4d59db5106af837d75a4700ab5f1 /Core/Validator/Constraints | |
parent | e642286ad42e113e0ed8e3343ad852121d2f467a (diff) | |
download | symfony-security-c4fb5a816e5d46b52908a0ff1b84b4ce9d147cba.zip symfony-security-c4fb5a816e5d46b52908a0ff1b84b4ce9d147cba.tar.gz symfony-security-c4fb5a816e5d46b52908a0ff1b84b4ce9d147cba.tar.bz2 |
[Security] renamed Constraint namespace to Constraints for validator classes in order to be consistent with the whole current validator API.
Diffstat (limited to 'Core/Validator/Constraints')
-rw-r--r-- | Core/Validator/Constraints/UserPassword.php | 28 | ||||
-rw-r--r-- | Core/Validator/Constraints/UserPasswordValidator.php | 46 |
2 files changed, 74 insertions, 0 deletions
diff --git a/Core/Validator/Constraints/UserPassword.php b/Core/Validator/Constraints/UserPassword.php new file mode 100644 index 0000000..ed29b0c --- /dev/null +++ b/Core/Validator/Constraints/UserPassword.php @@ -0,0 +1,28 @@ +<?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\Constraints; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + */ +class UserPassword extends Constraint +{ + public $message = 'This value should be the user current password.'; + public $service = 'security.validator.user_password'; + + public function validatedBy() + { + return $this->service; + } +} diff --git a/Core/Validator/Constraints/UserPasswordValidator.php b/Core/Validator/Constraints/UserPasswordValidator.php new file mode 100644 index 0000000..a4e0f90 --- /dev/null +++ b/Core/Validator/Constraints/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\Constraints; + +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 object must implement the UserInterface interface.'); + } + + $encoder = $this->encoderFactory->getEncoder($user); + + if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) { + $this->context->addViolation($constraint->message); + } + } +} |