123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- <?php
-
- /*
- *
- */
-
- namespace Muzich\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;
-
- // Slug stuff
- $evm = new \Doctrine\Common\EventManager();
- // ORM and ODM
- $sluggableListener = new \Gedmo\Sluggable\SluggableListener();
- $evm->addEventSubscriber($sluggableListener);
- // now this event manager should be passed to entity manager constructor
- $this->em->getEventManager()->addEventSubscriber($sluggableListener);
- }
-
- /**
- * {@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 reloadUser(UserInterface $user)
- {
- $this->em->refresh($user);
- }
-
- /**
- * Updates a user.
- *
- * @param UserInterface $user
- * @param Boolean $andFlush Whether to flush the changes (default true)
- */
- public function updateUser(UserInterface $user, $andFlush = true)
- {
- $this->updateCanonicalFields($user);
- $this->updatePassword($user);
-
- $this->em->persist($user);
- if ($andFlush) {
- $this->em->flush();
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public function validateUnique(UserInterface $value, Constraint $constraint)
- {
- // Since we probably want to validate the canonical fields,
- // we'd better make sure we have them.
- $this->updateCanonicalFields($value);
-
- $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;
- }
- }
-
-
- ?>
|