UserManager.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Doctrine;
  11. use Doctrine\Common\Persistence\ObjectManager;
  12. use FOS\UserBundle\Model\UserInterface;
  13. use FOS\UserBundle\Model\UserManager as BaseUserManager;
  14. use FOS\UserBundle\Util\CanonicalizerInterface;
  15. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  16. class UserManager extends BaseUserManager
  17. {
  18. protected $objectManager;
  19. protected $class;
  20. protected $repository;
  21. /**
  22. * Constructor.
  23. *
  24. * @param EncoderFactoryInterface $encoderFactory
  25. * @param CanonicalizerInterface $usernameCanonicalizer
  26. * @param CanonicalizerInterface $emailCanonicalizer
  27. * @param ObjectManager $om
  28. * @param string $class
  29. */
  30. public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, ObjectManager $om, $class)
  31. {
  32. parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer);
  33. $this->objectManager = $om;
  34. $this->repository = $om->getRepository($class);
  35. $metadata = $om->getClassMetadata($class);
  36. $this->class = $metadata->getName();
  37. }
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function deleteUser(UserInterface $user)
  42. {
  43. $this->objectManager->remove($user);
  44. $this->objectManager->flush();
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function getClass()
  50. {
  51. return $this->class;
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function findUserBy(array $criteria)
  57. {
  58. return $this->repository->findOneBy($criteria);
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function findUsers()
  64. {
  65. return $this->repository->findAll();
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function reloadUser(UserInterface $user)
  71. {
  72. $this->objectManager->refresh($user);
  73. }
  74. /**
  75. * Updates a user.
  76. *
  77. * @param UserInterface $user
  78. * @param Boolean $andFlush Whether to flush the changes (default true)
  79. */
  80. public function updateUser(UserInterface $user, $andFlush = true)
  81. {
  82. $this->updateCanonicalFields($user);
  83. $this->updatePassword($user);
  84. $this->objectManager->persist($user);
  85. if ($andFlush) {
  86. $this->objectManager->flush();
  87. }
  88. }
  89. }