From d9085c63e35bb708f560722fff5f4f5ad322c27b Mon Sep 17 00:00:00 2001 From: Jeremy Date: Tue, 17 Feb 2015 21:03:23 +0100 Subject: Handle password change --- .../Encoder/WallabagPasswordEncoder.php | 8 ++-- .../Validator/WallabagUserPasswordValidator.php | 48 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Security/Validator/WallabagUserPasswordValidator.php (limited to 'src/Wallabag/CoreBundle/Security') diff --git a/src/Wallabag/CoreBundle/Security/Authentication/Encoder/WallabagPasswordEncoder.php b/src/Wallabag/CoreBundle/Security/Authentication/Encoder/WallabagPasswordEncoder.php index 56f1affe..fcfe418b 100644 --- a/src/Wallabag/CoreBundle/Security/Authentication/Encoder/WallabagPasswordEncoder.php +++ b/src/Wallabag/CoreBundle/Security/Authentication/Encoder/WallabagPasswordEncoder.php @@ -41,10 +41,6 @@ class WallabagPasswordEncoder extends BasePasswordEncoder */ public function encodePassword($raw, $salt) { - if (null === $this->username) { - throw new \LogicException('We can not check the password without a username.'); - } - if ($this->isPasswordTooLong($raw)) { throw new BadCredentialsException('Invalid password.'); } @@ -71,6 +67,10 @@ class WallabagPasswordEncoder extends BasePasswordEncoder */ protected function mergePasswordAndSalt($password, $salt) { + if (null === $this->username) { + throw new \LogicException('We can not check the password without a username.'); + } + if (empty($salt)) { return $password; } diff --git a/src/Wallabag/CoreBundle/Security/Validator/WallabagUserPasswordValidator.php b/src/Wallabag/CoreBundle/Security/Validator/WallabagUserPasswordValidator.php new file mode 100644 index 00000000..5586f976 --- /dev/null +++ b/src/Wallabag/CoreBundle/Security/Validator/WallabagUserPasswordValidator.php @@ -0,0 +1,48 @@ +securityContext = $securityContext; + $this->encoderFactory = $encoderFactory; + } + + /** + * {@inheritdoc} + */ + public function validate($password, Constraint $constraint) + { + if (!$constraint instanceof UserPassword) { + throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UserPassword'); + } + + $user = $this->securityContext->getToken()->getUser(); + + if (!$user instanceof UserInterface) { + throw new ConstraintDefinitionException('The User object must implement the UserInterface interface.'); + } + + // give username, it's used to hash the password + $encoder = $this->encoderFactory->getEncoder($user); + $encoder->setUsername($user->getUsername()); + + if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) { + $this->context->addViolation($constraint->message); + } + } +} -- cgit v1.2.3