1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| <?php
namespace FOS\UserBundle\Entity;
use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManager as BaseUserManager;
use FOS\UserBundle\Util\CanonicalizerInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Constraint;
class UserManager extends BaseUserManager
{
protected $em;
protected $class;
protected $repository;
/**
* Constructor.
*
* @param EncoderFactoryInterface $encoderFactory
* @param string $algorithm
* @param CanonicalizerInterface $usernameCanonicalizer
* @param CanonicalizerInterface $emailCanonicalizer
* @param EntityManager $em
* @param string $class
*/
public function __construct(EncoderFactoryInterface $encoderFactory, $algorithm, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, EntityManager $em, $class)
{
parent::__construct($encoderFactory, $algorithm, $usernameCanonicalizer, $emailCanonicalizer);
$this->em = $em;
$this->repository = $em->getRepository($class);
$metadata = $em->getClassMetadata($class);
$this->class = $metadata->name;
}
/**
* {@inheritDoc}
*/
public function deleteUser(UserInterface $user)
{
$this->em->remove($user);
$this->em->flush();
}
/**
* {@inheritDoc}
*/
public function getClass()
{
return $this->class;
}
/**
* {@inheritDoc}
*/
public function findUserBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
}
/**
* {@inheritDoc}
*/
public function findUsers()
{
return $this->repository->findAll();
}
/**
* {@inheritDoc}
*/
public function updateUser(UserInterface $user)
{
$this->updateCanonicalFields($user);
$this->updatePassword($user);
$this->em->persist($user);
$this->em->flush();
}
/**
* {@inheritDoc}
*/
public function validateUnique($value, Constraint $constraint)
{
$fields = array_map('trim', explode(',', $constraint->property));
$users = $this->findConflictualUsers($value, $fields);
// there is no conflictual user
if (empty($users)) {
return true;
}
// there is no conflictual user which is not the same as the value
if ($this->anyIsUser($value, $users)) {
return true;
}
return false;
}
/**
* Indicates whether the given user and all compared objects correspond to the same record
*
* @param UserInterface $user
* @param array $comparisons
* @return boolean
*/
protected function anyIsUser($user, array $comparisons)
{
foreach ($comparisons as $comparison) {
if (!$user->isUser($comparison)) {
return false;
}
}
return true;
}
/**
* Gets conflictual users for the given user and constraint
*
* @param UserInterface $value
* @param array $fields
* @return array
*/
protected function findConflictualUsers($value, array $fields)
{
return $this->repository->findBy($this->getCriteria($value, $fields));
}
/**
* Gets the criteria used to find conflictual entities
*
* @param UserInterface $value
* @param array $constraint
* @return array
*/
protected function getCriteria($value, array $fields)
{
$classMetadata = $this->em->getClassMetadata($this->class);
$criteria = array();
foreach ($fields as $field) {
if (!$classMetadata->hasField($field)) {
throw new \InvalidArgumentException(sprintf('The "%s" class metadata does not have any "%s" field or association mapping.', $this->class, $field));
}
$criteria[$field] = $classMetadata->getFieldValue($value, $field);
}
return $criteria;
}
public function loadUserByAccount($account)
{
if (!$account instanceof $this->class) {
throw new UnsupportedAccountException(sprintf('Instances of "%s" are not supported.', get_class($account)));
}
return $this->loadUserByUsername($account->getUsername());
}
} |
Partager