UserManager.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\Model;
  11. use FOS\UserBundle\Util\CanonicalizerInterface;
  12. use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
  13. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  14. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  15. use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface;
  16. use Symfony\Component\Security\Core\User\UserProviderInterface;
  17. /**
  18. * Abstract User Manager implementation which can be used as base class for your
  19. * concrete manager.
  20. *
  21. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22. */
  23. abstract class UserManager implements UserManagerInterface, UserProviderInterface
  24. {
  25. protected $encoderFactory;
  26. protected $usernameCanonicalizer;
  27. protected $emailCanonicalizer;
  28. /**
  29. * Constructor.
  30. *
  31. * @param EncoderFactoryInterface $encoderFactory
  32. * @param CanonicalizerInterface $usernameCanonicalizer
  33. * @param CanonicalizerInterface $emailCanonicalizer
  34. */
  35. public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer)
  36. {
  37. $this->encoderFactory = $encoderFactory;
  38. $this->usernameCanonicalizer = $usernameCanonicalizer;
  39. $this->emailCanonicalizer = $emailCanonicalizer;
  40. }
  41. /**
  42. * Returns an empty user instance
  43. *
  44. * @return UserInterface
  45. */
  46. public function createUser()
  47. {
  48. $class = $this->getClass();
  49. $user = new $class;
  50. return $user;
  51. }
  52. /**
  53. * Finds a user by email
  54. *
  55. * @param string $email
  56. *
  57. * @return UserInterface
  58. */
  59. public function findUserByEmail($email)
  60. {
  61. return $this->findUserBy(array('emailCanonical' => $this->canonicalizeEmail($email)));
  62. }
  63. /**
  64. * Finds a user by username
  65. *
  66. * @param string $username
  67. *
  68. * @return UserInterface
  69. */
  70. public function findUserByUsername($username)
  71. {
  72. return $this->findUserBy(array('usernameCanonical' => $this->canonicalizeUsername($username)));
  73. }
  74. /**
  75. * Finds a user either by email, or username
  76. *
  77. * @param string $usernameOrEmail
  78. *
  79. * @return UserInterface
  80. */
  81. public function findUserByUsernameOrEmail($usernameOrEmail)
  82. {
  83. if (filter_var($usernameOrEmail, FILTER_VALIDATE_EMAIL)) {
  84. return $this->findUserByEmail($usernameOrEmail);
  85. }
  86. return $this->findUserByUsername($usernameOrEmail);
  87. }
  88. /**
  89. * Finds a user either by confirmation token
  90. *
  91. * @param string $token
  92. *
  93. * @return UserInterface
  94. */
  95. public function findUserByConfirmationToken($token)
  96. {
  97. return $this->findUserBy(array('confirmationToken' => $token));
  98. }
  99. /**
  100. * Refreshed a user by User Instance
  101. *
  102. * Throws UnsupportedUserException if a User Instance is given which is not
  103. * managed by this UserManager (so another Manager could try managing it)
  104. *
  105. * It is strongly discouraged to use this method manually as it bypasses
  106. * all ACL checks.
  107. *
  108. * @param SecurityUserInterface $user
  109. *
  110. * @return UserInterface
  111. */
  112. public function refreshUser(SecurityUserInterface $user)
  113. {
  114. $class = $this->getClass();
  115. if (!$user instanceof $class) {
  116. throw new UnsupportedUserException('Account is not supported.');
  117. }
  118. if (!$user instanceof User) {
  119. throw new UnsupportedUserException(sprintf('Expected an instance of FOS\UserBundle\Model\User, but got "%s".', get_class($user)));
  120. }
  121. $refreshedUser = $this->findUserBy(array('id' => $user->getId()));
  122. if (null === $refreshedUser) {
  123. throw new UsernameNotFoundException(sprintf('User with ID "%d" could not be reloaded.', $user->getId()));
  124. }
  125. return $refreshedUser;
  126. }
  127. /**
  128. * Loads a user by username
  129. *
  130. * It is strongly discouraged to call this method manually as it bypasses
  131. * all ACL checks.
  132. *
  133. * @param string $username
  134. *
  135. * @return UserInterface
  136. */
  137. public function loadUserByUsername($username)
  138. {
  139. $user = $this->findUserByUsername($username);
  140. if (!$user) {
  141. throw new UsernameNotFoundException(sprintf('No user with name "%s" was found.', $username));
  142. }
  143. return $user;
  144. }
  145. /**
  146. * {@inheritDoc}
  147. */
  148. public function updateCanonicalFields(UserInterface $user)
  149. {
  150. $user->setUsernameCanonical($this->canonicalizeUsername($user->getUsername()));
  151. $user->setEmailCanonical($this->canonicalizeEmail($user->getEmail()));
  152. }
  153. /**
  154. * {@inheritDoc}
  155. */
  156. public function updatePassword(UserInterface $user)
  157. {
  158. if (0 !== strlen($password = $user->getPlainPassword())) {
  159. $encoder = $this->getEncoder($user);
  160. $user->setPassword($encoder->encodePassword($password, $user->getSalt()));
  161. $user->eraseCredentials();
  162. }
  163. }
  164. /**
  165. * Canonicalizes an email
  166. *
  167. * @param string $email
  168. *
  169. * @return string
  170. */
  171. protected function canonicalizeEmail($email)
  172. {
  173. return $this->emailCanonicalizer->canonicalize($email);
  174. }
  175. /**
  176. * Canonicalizes a username
  177. *
  178. * @param string $username
  179. *
  180. * @return string
  181. */
  182. protected function canonicalizeUsername($username)
  183. {
  184. return $this->usernameCanonicalizer->canonicalize($username);
  185. }
  186. protected function getEncoder(UserInterface $user)
  187. {
  188. return $this->encoderFactory->getEncoder($user);
  189. }
  190. /**
  191. * {@inheritDoc}
  192. */
  193. public function supportsClass($class)
  194. {
  195. return $class === $this->getClass();
  196. }
  197. }