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

Skip to content

[Security] renamed Constraint namespace to Constraints for validator classes in order to be consistent with the whole current validator API. #6960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions UPGRADE-2.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,55 @@
trusted_proxies: ['127.0.0.1', '10.0.0.1'] # a list of proxy IPs you trust
```

### Security

* The existing ``UserPassword`` validator constraint class has been modified.
Its namespace has been changed to better fit the Symfony coding conventions.

Before:

```
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
```

After: (note the `s` at the end of `Constraint`)

```
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
```

* The new ``UserPassword`` validator constraint class now accepts a new
``service`` option that allows to specify a custom validator service name in
order to validate the current logged-in user's password.

```
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;

$constraint = new UserPassword(array(
'service' => 'my.custom.validator.user_password',
));
```

#### Deprecations

* The two previous ``UserPassword`` and ``UserPasswordValidator`` classes in
the ``Symfony\Component\Security\Core\Validator\Constraint`` namespace have
been deprecated and will be removed in 2.3.

Before:

```
use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
use Symfony\Component\Security\Core\Validator\Constraint\UserPasswordValidator;
```

After:

```
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;
```

### Serializer

* All serializer interfaces (Serializer, Normalizer, Encoder) have been
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

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

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

<services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@

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

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword as BaseUserPassword;

/**
* @Annotation
*
* @deprecated Deprecated since version 2.2, to be removed in 2.3.
*/
class UserPassword extends Constraint
class UserPassword extends BaseUserPassword
{
public $message = 'This value should be the user current password.';
public $service = 'security.validator.user_password';

public function validatedBy()
public function __construct($options = null)
{
return $this->service;
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);

parent::__construct($options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,19 @@

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

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;
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator as BaseUserPasswordValidator;

class UserPasswordValidator extends ConstraintValidator
/**
* @deprecated Deprecated since version 2.2, to be removed in 2.3.
*/
class UserPasswordValidator extends BaseUserPasswordValidator
{
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 must extend UserInterface');
}

$encoder = $this->encoderFactory->getEncoder($user);
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);

if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
$this->context->addViolation($constraint->message);
}
parent::__construct($securityContext, $encoderFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Security\Tests\Core\Validator\Constraint;
namespace Symfony\Component\Security\Tests\Core\Validator\Constraints;

use Symfony\Component\Security\Core\Validator\Constraint\UserPassword;
use Symfony\Component\Security\Core\Validator\Constraint\UserPasswordValidator;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Security\Core\Validator\Constraints\UserPasswordValidator;

class UserPasswordValidatorTest extends \PHPUnit_Framework_TestCase
{
Expand Down