UserManager.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Propel;
  11. use FOS\UserBundle\Model\UserInterface;
  12. use FOS\UserBundle\Model\UserManager as BaseUserManager;
  13. use FOS\UserBundle\Util\CanonicalizerInterface;
  14. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  15. class UserManager extends BaseUserManager
  16. {
  17. protected $class;
  18. /**
  19. * Constructor.
  20. *
  21. * @param EncoderFactoryInterface $encoderFactory
  22. * @param CanonicalizerInterface $usernameCanonicalizer
  23. * @param CanonicalizerInterface $emailCanonicalizer
  24. * @param string $class
  25. */
  26. public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, $class)
  27. {
  28. parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer);
  29. $this->class = $class;
  30. }
  31. /**
  32. * {@inheritDoc}
  33. */
  34. public function deleteUser(UserInterface $user)
  35. {
  36. if (!$user instanceof \Persistent) {
  37. throw new \InvalidArgumentException('This user instance is not supported by the Propel UserManager implementation');
  38. }
  39. $user->delete();
  40. }
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function getClass()
  45. {
  46. return $this->class;
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function findUserBy(array $criteria)
  52. {
  53. $query = $this->createQuery();
  54. foreach ($criteria as $field => $value) {
  55. $method = 'filterBy'.ucfirst($field);
  56. $query->$method($value);
  57. }
  58. return $query->findOne();
  59. }
  60. /**
  61. * {@inheritDoc}
  62. */
  63. public function findUsers()
  64. {
  65. return $this->createQuery()->find();
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function reloadUser(UserInterface $user)
  71. {
  72. if (!$user instanceof \Persistent) {
  73. throw new \InvalidArgumentException('This user instance is not supported by the Propel UserManager implementation');
  74. }
  75. $user->reload();
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function updateUser(UserInterface $user)
  81. {
  82. if (!$user instanceof \Persistent) {
  83. throw new \InvalidArgumentException('This user instance is not supported by the Propel UserManager implementation');
  84. }
  85. $this->updateCanonicalFields($user);
  86. $this->updatePassword($user);
  87. $user->save();
  88. }
  89. /**
  90. * Create the propel query class corresponding to your queryclass
  91. *
  92. * @return \ModelCriteria the queryClass
  93. */
  94. protected function createQuery()
  95. {
  96. return \PropelQuery::from($this->class);
  97. }
  98. }