Created
August 20, 2014 15:25
-
-
Save baptistedonaux/4e80f771b3de1aa15c95 to your computer and use it in GitHub Desktop.
UserProvider.php
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 Namespace\MyBundle\Security\User; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\Common\Collections\Criteria; | |
use Doctrine\Common\Collections\Expr\Comparison; | |
use Symfony\Component\Security\Core\Exception\DisabledException; | |
use Symfony\Component\Security\Core\Exception\LockedException; | |
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | |
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\Security\Core\User\UserProviderInterface; | |
class UserProvider implements UserProviderInterface | |
{ | |
private $em; | |
public function __construct(EntityManager $em) { | |
$this->em = $em; | |
} | |
public function loadUserByUsername($username) | |
{ | |
$criteria = new Criteria(); | |
$criteria->where(new Comparison("username", Comparison::EQ, $username)); | |
$criteria->orWhere(new Comparison("email", Comparison::EQ, $username)); | |
$criteria->setMaxResults(1); | |
$userData = $this->em->getRepository("NamespaceMyBundle:User")->matching($criteria)->first(); | |
if ($userData != null) { | |
switch ($userData->getActive()) { | |
case User::DISABLED: | |
throw new DisabledException("Your account is disabled. Please contact the administrator."); | |
break; | |
case User::_WAIT_VALIDATION: | |
throw new LockedException("Your account is locked. Check and valid your email account."); | |
break; | |
case User::ACTIVE: | |
return $userData; | |
break; | |
} | |
} | |
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); | |
} | |
public function refreshUser(UserInterface $user) | |
{ | |
if (!$user instanceof User) { | |
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); | |
} | |
return $this->loadUserByUsername($user->getUsername()); | |
} | |
public function supportsClass($class) | |
{ | |
return $class === 'Namespace\MyBundle\Entity\User'; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment