Skip to content

Instantly share code, notes, and snippets.

@polc
Created October 16, 2014 12:34
Show Gist options
  • Save polc/0db2245493669de6de41 to your computer and use it in GitHub Desktop.
Save polc/0db2245493669de6de41 to your computer and use it in GitHub Desktop.
<?php
namespace AppBundle\Security;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\DisabledException;
use AppBundle\Manager\UserManager;
class UserProvider implements UserProviderInterface
{
private $userManager;
public function __construct(UserManager $userManager)
{
$this->userManager = $userManager;
}
public function loadUserByUsername($username)
{
$user = $this->userManager->findOneByEmail($username);
if (empty($user) || true !== $user->getActivated()) {
throw new UsernameNotFoundException('Username not found.');
}
$user->setLastLogin(new \Datetime());
$this->userManager->save($user);
return $user;
}
public function refreshUser(UserInterface $user)
{
if (!$this->supportsClass($class = get_class($user))) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
}
$user = $this->userManager->findOneById($user->getId());
if (false === $user->getActivated()) {
$exception = new DisabledException('Disabled account.');
$exception->setUser($user);
throw $exception;
}
return $user;
}
public function supportsClass($class)
{
return $class === 'AppBundle\Entity\User';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment