From 677a795a2122920662d1aceeef2c1ff3e75f4b84 Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Wed, 19 Jun 2013 20:41:46 +0200 Subject: [PATCH] Moving function to the UserRepository --- main/inc/Entity/Repository/UserRepository.php | 56 +++++++++- main/inc/Entity/User.php | 102 ++++-------------- 2 files changed, 74 insertions(+), 84 deletions(-) diff --git a/main/inc/Entity/Repository/UserRepository.php b/main/inc/Entity/Repository/UserRepository.php index a46c20825f..bc6c75c97a 100644 --- a/main/inc/Entity/Repository/UserRepository.php +++ b/main/inc/Entity/Repository/UserRepository.php @@ -3,14 +3,68 @@ namespace Entity\Repository; use Doctrine\ORM\EntityRepository; +use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Security\Core\User\UserProviderInterface; +use Doctrine\ORM\NoResultException; /** * UserRepository * */ -class UserRepository extends EntityRepository +class UserRepository extends EntityRepository implements UserProviderInterface { + /** + * @param string $username + * @return mixed + * @throws UsernameNotFoundException + */ + public function loadUserByUsername($username) + { + $q = $this + ->createQueryBuilder('u') + ->where('u.username = :username OR u.email = :email') + ->setParameter('username', $username) + ->setParameter('email', $username) + ->getQuery(); + + try { + $user = $q->getSingleResult(); + } catch (NoResultException $e) { + throw new UsernameNotFoundException( + sprintf('Unable to find an active admin User identified by "%s".', $username), + null, + 0, + $e + ); + } + return $user; + } + + /** + * @param UserInterface $user + * @return mixed + * @throws UnsupportedUserException + */ + public function refreshUser(UserInterface $user) + { + $class = get_class($user); + if (!$this->supportsClass($class)) { + throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class)); + } + + return $this->loadUserByUsername($user->getUsername()); + } + + /** + * @param string $class + * @return bool + */ + public function supportsClass($class) + { + return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName()); + } + public function getUsers($limit = null) { $qb = $this->createQueryBuilder('u') diff --git a/main/inc/Entity/User.php b/main/inc/Entity/User.php index 51af97ae53..16e7db8796 100644 --- a/main/inc/Entity/User.php +++ b/main/inc/Entity/User.php @@ -6,8 +6,6 @@ use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\Criteria; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Security\Core\User\AdvancedUserInterface; -use Symfony\Component\Security\Core\User\UserInterface; -use Symfony\Component\Security\Core\User\UserProviderInterface; /** * User @@ -15,7 +13,7 @@ use Symfony\Component\Security\Core\User\UserProviderInterface; * @ORM\Table(name="user") * @ORM\Entity(repositoryClass="Entity\Repository\UserRepository") */ -class User implements AdvancedUserInterface, UserProviderInterface +class User implements AdvancedUserInterface { /** * @var integer @@ -238,8 +236,6 @@ class User implements AdvancedUserInterface, UserProviderInterface */ private $salt; - private $em; - /** * */ @@ -253,83 +249,35 @@ class User implements AdvancedUserInterface, UserProviderInterface } /** - * Needed in order to use the security component - * @param \Doctrine\ORM\EntityManager $em - */ - public function setEntityManager($em) - { - $this->em = $em; - } - - /** - * @param string $username - * @return mixed - * @throws UsernameNotFoundException - */ - public function loadUserByUsername($username) - { - $q = $this->em - ->createQueryBuilder('u') - ->select('u') - ->from('Entity\User', 'u') - ->where('u.username = :username OR u.email = :email') - ->setParameter('username', $username) - ->setParameter('email', $username) - ->getQuery(); - - try { - $user = $q->getSingleResult(); - - } catch (NoResultException $e) { - throw new UsernameNotFoundException( - sprintf('Unable to find an active admin User identified by "%s".', $username), - null, - 0, - $e - ); - } - return $user; - } - - /** - * @param UserInterface $user - * @return mixed - * @throws UnsupportedUserException + * @inheritDoc */ - public function refreshUser(UserInterface $user) + public function isAccountNonExpired() { - $class = get_class($user); - if (!$this->supportsClass($class)) { - throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class)); - } - - return $this->loadUserByUsername($user->getUsername()); + return true; } /** - * @param string $class - * @return bool + * @inheritDoc */ - public function supportsClass($class) + public function isAccountNonLocked() { - return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName()); + return true; } /** * @inheritDoc */ - public function getRoles() + public function isCredentialsNonExpired() { - return $this->roles->toArray(); + return true; } /** - * - * @return ArrayCollection + * @inheritDoc */ - public function getRolesObj() + public function isEnabled() { - return $this->roles; + return $this->getActive() == 1; } /** @@ -339,37 +287,25 @@ class User implements AdvancedUserInterface, UserProviderInterface { } - /** - * @inheritDoc - */ - public function isAccountNonExpired() - { - return true; - } /** * @inheritDoc */ - public function isAccountNonLocked() + public function getRoles() { - return true; + return $this->roles->toArray(); } /** - * @inheritDoc + * + * @return ArrayCollection */ - public function isCredentialsNonExpired() + public function getRolesObj() { - return true; + return $this->roles; } - /** - * @inheritDoc - */ - public function isEnabled() - { - return $this->getActive() == 1; - } + /** * Set salt