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