|
3
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the FOSUserBundle package. |
|
|
5 |
* |
|
|
6 |
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Validator; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
|
|
15 |
use Symfony\Component\Validator\Constraint; |
|
|
16 |
use Symfony\Component\Validator\ConstraintValidator; |
|
|
17 |
|
|
|
18 |
class PasswordValidator extends ConstraintValidator |
|
|
19 |
{ |
|
|
20 |
protected $encoderFactory; |
|
|
21 |
|
|
|
22 |
public function setEncoderFactory(EncoderFactoryInterface $factory) |
|
|
23 |
{ |
|
|
24 |
$this->encoderFactory = $factory; |
|
|
25 |
} |
|
|
26 |
|
|
|
27 |
public function isValid($object, Constraint $constraint) |
|
|
28 |
{ |
|
|
29 |
if (!is_object($object)) { |
|
|
30 |
throw new \RuntimeException('This is a class constraint.'); |
|
|
31 |
} |
|
|
32 |
$raw = $object->{$constraint->passwordProperty}; |
|
|
33 |
$user = null === $constraint->userProperty ? $object : $object->{$constraint->userProperty}; |
|
|
34 |
$encoder = $this->encoderFactory->getEncoder($user); |
|
|
35 |
if (!$encoder->isPasswordValid($user->getPassword(), $raw, $user->getSalt())) { |
|
|
36 |
$this->setMessage($constraint->message); |
|
|
37 |
|
|
|
38 |
return false; |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
return true; |
|
|
42 |
} |
|
|
43 |
} |