Skip to content

Instantly share code, notes, and snippets.

@Problematic
Created September 16, 2011 16:00
Show Gist options
  • Save Problematic/1222436 to your computer and use it in GitHub Desktop.
Save Problematic/1222436 to your computer and use it in GitHub Desktop.
Symfony2 User Factory
<?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