UserManager.php 4.9KB

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