forked from FriendsOfSymfony/FOSUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordValidator.php
More file actions
35 lines (27 loc) · 1.01 KB
/
Copy pathPasswordValidator.php
File metadata and controls
35 lines (27 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
namespace FOS\UserBundle\Validator;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class PasswordValidator extends ConstraintValidator
{
protected $encoderFactory;
public function setEncoderFactory(EncoderFactoryInterface $factory)
{
$this->encoderFactory = $factory;
}
public function isValid($object, Constraint $constraint)
{
if (!is_object($object)) {
throw new \RuntimeException('This is a class constraint.');
}
$raw = $object->{$constraint->passwordProperty};
$user = null === $constraint->userProperty ? $object : $object->{$constraint->userProperty};
$encoder = $this->encoderFactory->getEncoder($user);
if (!$encoder->isPasswordValid($user->getPassword(), $raw, $user->getSalt())) {
$this->setMessage($constraint->message);
return false;
}
return true;
}
}