Moving function to the UserRepository

skala
Julio Montoya 13 years ago
parent bd8fb748bf
commit 677a795a21
  1. 56
      main/inc/Entity/Repository/UserRepository.php
  2. 102
      main/inc/Entity/User.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')

@ -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

Loading…
Cancel
Save