Created
September 16, 2011 16:00
-
-
Save Problematic/1222436 to your computer and use it in GitHub Desktop.
Symfony2 User Factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\UserBundle\Model; | |
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
class UserFactory | |
{ | |
protected $em; | |
protected $encoder_factory; | |
public function __construct(EncoderFactoryInterface $encoder_factory) | |
{ | |
$this->encoder_factory = $encoder_factory; | |
} | |
/** | |
* @param UserInterface $user user to be encoded | |
* @param string $password password to encode for user (or null to use getPassword() method from user) | |
* @return string encoded password | |
*/ | |
public function encode(UserInterface $user, $password = null) | |
{ | |
$encoder = $this->encoder_factory->getEncoder($user); | |
$toEncode = null === $password ? $user->getPassword : $password; | |
$encoded = $encoder->encodePassword($toEncode, $user->getSalt()); | |
return $encoded; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment