Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35b62ac

Browse files
author
Hugo Hamon
committed
[Security] renamed Constraint namespace to Constraints for validator classes in order to be consistent with the whole current validator API.
1 parent df4188a commit 35b62ac

File tree

7 files changed

+142
-35
lines changed

7 files changed

+142
-35
lines changed

UPGRADE-2.2.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,55 @@
567567
trusted_proxies: ['127.0.0.1', '10.0.0.1'] # a list of proxy IPs you trust
568568
```
569569

570+
### Security
571+
572+
* The existing ``UserPassword`` validator constraint class has been modified.
573+
Its namespace has been changed to better fit the Symfony coding conventions.
574+
575+
Before:
576+
577+
```
578+
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
579+
```
580+
581+
After: (note the `s` at the end of `Constraint`)
582+
583+
```
584+
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
585+
```
586+
587+
* The new ``UserPassword`` validator constraint class now accepts a new
588+
``service`` option that allows to specify a custom validator service name in
589+
order to validate the current logged-in user's password.
590+
591+
```
592+
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
593+
594+
$constraint = new UserPassword(array(
595+
'service' => 'my.custom.validator.user_password',
596+
));
597+
```
598+
599+
#### Deprecations
600+
601+
* The two previous ``UserPassword`` and ``UserPasswordValidator`` classes in
602+
the ``Symfony\Component\Security\Core\Validator\Constraint`` namespace have
603+
been deprecated and will be removed in 2.3.
604+
605+
Before:
606+
607+
```
608+
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
609+
use Symfony\Component\Security\Core\Validator\Constraint\UserPasswordValidator;
610+
```
611+
612+
After:
613+
614+
```
615+
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
616+
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
617+
```
618+
570619
### Serializer
571620
572621
* All serializer interfaces (Serializer, Normalizer, Encoder) have been

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
<parameter key="security.http_utils.class">Symfony\Component\Security\Http\HttpUtils</parameter>
4343

44-
<parameter key="security.validator.user_password.class">Symfony\Component\Security\Core\Validator\Constraint\UserPasswordValidator</parameter>
44+
<parameter key="security.validator.user_password.class">Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator</parameter>
4545
</parameters>
4646

4747
<services>

src/Symfony/Component/Security/Core/Validator/Constraint/UserPassword.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111

1212
namespace Symfony\Component\Security\Core\Validator\Constraint;
1313

14-
use Symfony\Component\Validator\Constraint;
14+
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword as BaseUserPassword;
1515

1616
/**
1717
* @Annotation
18+
*
19+
* @deprecated Deprecated since version 2.2, to be removed in 2.3.
1820
*/
19-
class UserPassword extends Constraint
21+
class UserPassword extends BaseUserPassword
2022
{
21-
public $message = 'This value should be the user current password.';
22-
public $service = 'security.validator.user_password';
23-
24-
public function validatedBy()
23+
public function __construct($options = null)
2524
{
26-
return $this->service;
25+
trigger_error('UserPassword class in Symfony\Component\Security\Core\Validator\Constraint namespace is deprecated since version 2.2 and will be removed in 2.3. Use the Symfony\Component\Security\Core\Validator\Constraints\UserPassword class instead.', E_USER_DEPRECATED);
26+
27+
parent::__construct($options);
2728
}
2829
}

src/Symfony/Component/Security/Core/Validator/Constraint/UserPasswordValidator.php

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,19 @@
1111

1212
namespace Symfony\Component\Security\Core\Validator\Constraint;
1313

14-
use Symfony\Component\Security\Core\User\UserInterface;
1514
use Symfony\Component\Security\Core\SecurityContextInterface;
1615
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
17-
use Symfony\Component\Validator\Constraint;
18-
use Symfony\Component\Validator\ConstraintValidator;
19-
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
16+
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator as BaseUserPasswordValidator;
2017

21-
class UserPasswordValidator extends ConstraintValidator
18+
/**
19+
* @deprecated Deprecated since version 2.2, to be removed in 2.3.
20+
*/
21+
class UserPasswordValidator extends BaseUserPasswordValidator
2222
{
23-
private $securityContext;
24-
private $encoderFactory;
25-
2623
public function __construct(SecurityContextInterface $securityContext, EncoderFactoryInterface $encoderFactory)
2724
{
28-
$this->securityContext = $securityContext;
29-
$this->encoderFactory = $encoderFactory;
30-
}
31-
32-
public function validate($password, Constraint $constraint)
33-
{
34-
$user = $this->securityContext->getToken()->getUser();
35-
36-
if (!$user instanceof UserInterface) {
37-
throw new ConstraintDefinitionException('The User must extend UserInterface');
38-
}
39-
40-
$encoder = $this->encoderFactory->getEncoder($user);
25+
trigger_error('UserPasswordValidator class in Symfony\Component\Security\Core\Validator\Constraint namespace is deprecated since version 2.2 and will be removed in 2.3. Use the Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator class instead.', E_USER_DEPRECATED);
4126

42-
if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
43-
$this->context->addViolation($constraint->message);
44-
}
27+
parent::__construct($securityContext, $encoderFactory);
4528
}
4629
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Security\Core\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
*/
19+
class UserPassword extends Constraint
20+
{
21+
public $message = 'This value should be the user current password.';
22+
public $service = 'security.validator.user_password';
23+
24+
public function validatedBy()
25+
{
26+
return $this->service;
27+
}
28+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Security\Core\Validator\Constraints;
13+
14+
use Symfony\Component\Security\Core\User\UserInterface;
15+
use Symfony\Component\Security\Core\SecurityContextInterface;
16+
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
17+
use Symfony\Component\Validator\Constraint;
18+
use Symfony\Component\Validator\ConstraintValidator;
19+
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
20+
21+
class UserPasswordValidator extends ConstraintValidator
22+
{
23+
private $securityContext;
24+
private $encoderFactory;
25+
26+
public function __construct(SecurityContextInterface $securityContext, EncoderFactoryInterface $encoderFactory)
27+
{
28+
$this->securityContext = $securityContext;
29+
$this->encoderFactory = $encoderFactory;
30+
}
31+
32+
public function validate($password, Constraint $constraint)
33+
{
34+
$user = $this->securityContext->getToken()->getUser();
35+
36+
if (!$user instanceof UserInterface) {
37+
throw new ConstraintDefinitionException('The User object must implement the UserInterface interface.');
38+
}
39+
40+
$encoder = $this->encoderFactory->getEncoder($user);
41+
42+
if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
43+
$this->context->addViolation($constraint->message);
44+
}
45+
}
46+
}

src/Symfony/Component/Security/Tests/Core/Validator/Constraint/UserPasswordValidatorTest.php renamed to src/Symfony/Component/Security/Tests/Core/Validator/Constraints/UserPasswordValidatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Security\Tests\Core\Validator\Constraint;
12+
namespace Symfony\Component\Security\Tests\Core\Validator\Constraints;
1313

14-
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
15-
use Symfony\Component\Security\Core\Validator\Constraint\UserPasswordValidator;
14+
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
15+
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
1616

1717
class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
1818
{

0 commit comments

Comments
 (0)