diff options
author | Fabien Potencier <fabien.potencier@gmail.com> | 2014-07-25 10:49:22 +0200 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2014-07-25 10:49:22 +0200 |
commit | 598e58a42f42eaaf597afdef41137557f8c862fc (patch) | |
tree | 2098036f3a4f3d11e57de3c238fc57de3b1cc48d /Core/Encoder | |
parent | 8e84c8e0aa3a9dcc4560f7223636e394c235d839 (diff) | |
parent | 37b68961f133fc8dacc66dcbdaf03e1859122ae0 (diff) | |
download | symfony-security-598e58a42f42eaaf597afdef41137557f8c862fc.zip symfony-security-598e58a42f42eaaf597afdef41137557f8c862fc.tar.gz symfony-security-598e58a42f42eaaf597afdef41137557f8c862fc.tar.bz2 |
feature #11306 [DX] New service to simplify password encoding (aferrandini)
This PR was merged into the 2.6-dev branch.
Discussion
----------
[DX] New service to simplify password encoding
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #11299
| License | MIT
| Doc PR | https://github.com/symfony/symfony-docs/pull/3995
This new service siplifies the way to encode a password. Just get the `security.password_encoder` service and encode the `User` password.
```php
$encoded = $this->container->get('security.password_encoder')
->encodePassword($user, $plainPassword);
$user->setPassword($encoded);
```
Commits
-------
7bc190a New service to simplify password encoding
Diffstat (limited to 'Core/Encoder')
-rw-r--r-- | Core/Encoder/UserPasswordEncoder.php | 55 | ||||
-rw-r--r-- | Core/Encoder/UserPasswordEncoderInterface.php | 41 |
2 files changed, 96 insertions, 0 deletions
diff --git a/Core/Encoder/UserPasswordEncoder.php b/Core/Encoder/UserPasswordEncoder.php new file mode 100644 index 0000000..13ee835 --- /dev/null +++ b/Core/Encoder/UserPasswordEncoder.php @@ -0,0 +1,55 @@ +<?php + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien@symfony.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Core\Encoder; + +use Symfony\Component\Security\Core\User\UserInterface; + +/** + * A generic password encoder + * + * @author Ariel Ferrandini <arielferrandini@gmail.com> + */ +class UserPasswordEncoder implements UserPasswordEncoderInterface +{ + /** + * @var EncoderFactoryInterface + */ + private $encoderFactory; + + /** + * @param EncoderFactoryInterface $encoderFactory The encoder factory + */ + public function __construct(EncoderFactoryInterface $encoderFactory) + { + $this->encoderFactory = $encoderFactory; + } + + /** + * {@inheritdoc} + */ + public function encodePassword(UserInterface $user, $plainPassword) + { + $encoder = $this->encoderFactory->getEncoder($user); + + return $encoder->encodePassword($plainPassword, $user->getSalt()); + } + + /** + * {@inheritdoc} + */ + public function isPasswordValid(UserInterface $user, $raw) + { + $encoder = $this->encoderFactory->getEncoder($user); + + return $encoder->isPasswordValid($user->getPassword(), $raw, $user->getSalt()); + } +} diff --git a/Core/Encoder/UserPasswordEncoderInterface.php b/Core/Encoder/UserPasswordEncoderInterface.php new file mode 100644 index 0000000..39e906a --- /dev/null +++ b/Core/Encoder/UserPasswordEncoderInterface.php @@ -0,0 +1,41 @@ +<?php + +/* + * This file is part of the Symfony package. + * + * (c) Fabien Potencier <fabien@symfony.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Core\Encoder; + +use Symfony\Component\Security\Core\User\UserInterface; + +/** + * UserPasswordEncoderInterface is the interface for the password encoder service. + * + * @author Ariel Ferrandini <arielferrandini@gmail.com> + */ +interface UserPasswordEncoderInterface +{ + /** + * + * Encodes the plain password. + * + * @param UserInterface $user The user + * @param string $plainPassword The password to encode + * + * @return string The encoded password + */ + public function encodePassword(UserInterface $user, $plainPassword); + + /** + * @param UserInterface $user The user + * @param string $raw A raw password + * + * @return bool true if the password is valid, false otherwise + */ + public function isPasswordValid(UserInterface $user, $raw); +} |