PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/friendsofsymfony/user-bundle/Model/UserManager.php

https://gitlab.com/mohamedAmineBaccouche/techsoul2016
PHP | 243 lines | 100 code | 29 blank | 114 comment | 6 complexity | ed513660e1bb8e06b04e16f6d5fd8774 MD5 | raw file
  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. /**
  26. * @var EncoderFactoryInterface
  27. */
  28. protected $encoderFactory;
  29. /**
  30. * @var CanonicalizerInterface
  31. */
  32. protected $usernameCanonicalizer;
  33. /**
  34. * @var CanonicalizerInterface
  35. */
  36. protected $emailCanonicalizer;
  37. /**
  38. * Constructor.
  39. *
  40. * @param EncoderFactoryInterface $encoderFactory
  41. * @param CanonicalizerInterface $usernameCanonicalizer
  42. * @param CanonicalizerInterface $emailCanonicalizer
  43. */
  44. public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer)
  45. {
  46. $this->encoderFactory = $encoderFactory;
  47. $this->usernameCanonicalizer = $usernameCanonicalizer;
  48. $this->emailCanonicalizer = $emailCanonicalizer;
  49. }
  50. /**
  51. * Returns an empty user instance
  52. *
  53. * @return UserInterface
  54. */
  55. public function createUser()
  56. {
  57. $class = $this->getClass();
  58. $user = new $class;
  59. return $user;
  60. }
  61. /**
  62. * Finds a user by email
  63. *
  64. * @param string $email
  65. *
  66. * @return UserInterface
  67. */
  68. public function findUserByEmail($email)
  69. {
  70. return $this->findUserBy(array('emailCanonical' => $this->canonicalizeEmail($email)));
  71. }
  72. /**
  73. * Finds a user by username
  74. *
  75. * @param string $username
  76. *
  77. * @return UserInterface
  78. */
  79. public function findUserByUsername($username)
  80. {
  81. return $this->findUserBy(array('usernameCanonical' => $this->canonicalizeUsername($username)));
  82. }
  83. /**
  84. * Finds a user either by email, or username
  85. *
  86. * @param string $usernameOrEmail
  87. *
  88. * @return UserInterface
  89. */
  90. public function findUserByUsernameOrEmail($usernameOrEmail)
  91. {
  92. if (filter_var($usernameOrEmail, FILTER_VALIDATE_EMAIL)) {
  93. return $this->findUserByEmail($usernameOrEmail);
  94. }
  95. return $this->findUserByUsername($usernameOrEmail);
  96. }
  97. /**
  98. * Finds a user either by confirmation token
  99. *
  100. * @param string $token
  101. *
  102. * @return UserInterface
  103. */
  104. public function findUserByConfirmationToken($token)
  105. {
  106. return $this->findUserBy(array('confirmationToken' => $token));
  107. }
  108. /**
  109. * Refreshed a user by User Instance
  110. *
  111. * Throws UnsupportedUserException if a User Instance is given which is not
  112. * managed by this UserManager (so another Manager could try managing it)
  113. *
  114. * It is strongly discouraged to use this method manually as it bypasses
  115. * all ACL checks.
  116. *
  117. * @deprecated Use FOS\UserBundle\Security\UserProvider instead
  118. *
  119. * @param SecurityUserInterface $user
  120. *
  121. * @return UserInterface
  122. */
  123. public function refreshUser(SecurityUserInterface $user)
  124. {
  125. @trigger_error('Using the UserManager as user provider is deprecated. Use FOS\UserBundle\Security\UserProvider instead.', E_USER_DEPRECATED);
  126. $class = $this->getClass();
  127. if (!$user instanceof $class) {
  128. throw new UnsupportedUserException('Account is not supported.');
  129. }
  130. if (!$user instanceof User) {
  131. throw new UnsupportedUserException(sprintf('Expected an instance of FOS\UserBundle\Model\User, but got "%s".', get_class($user)));
  132. }
  133. $refreshedUser = $this->findUserBy(array('id' => $user->getId()));
  134. if (null === $refreshedUser) {
  135. throw new UsernameNotFoundException(sprintf('User with ID "%d" could not be reloaded.', $user->getId()));
  136. }
  137. return $refreshedUser;
  138. }
  139. /**
  140. * Loads a user by username
  141. *
  142. * It is strongly discouraged to call this method manually as it bypasses
  143. * all ACL checks.
  144. *
  145. * @deprecated Use FOS\UserBundle\Security\UserProvider instead
  146. *
  147. * @param string $username
  148. *
  149. * @return UserInterface
  150. */
  151. public function loadUserByUsername($username)
  152. {
  153. @trigger_error('Using the UserManager as user provider is deprecated. Use FOS\UserBundle\Security\UserProvider instead.', E_USER_DEPRECATED);
  154. $user = $this->findUserByUsername($username);
  155. if (!$user) {
  156. throw new UsernameNotFoundException(sprintf('No user with name "%s" was found.', $username));
  157. }
  158. return $user;
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. public function updateCanonicalFields(UserInterface $user)
  164. {
  165. $user->setUsernameCanonical($this->canonicalizeUsername($user->getUsername()));
  166. $user->setEmailCanonical($this->canonicalizeEmail($user->getEmail()));
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. public function updatePassword(UserInterface $user)
  172. {
  173. if (0 !== strlen($password = $user->getPlainPassword())) {
  174. $encoder = $this->getEncoder($user);
  175. $user->setPassword($encoder->encodePassword($password, $user->getSalt()));
  176. $user->eraseCredentials();
  177. }
  178. }
  179. /**
  180. * Canonicalizes an email
  181. *
  182. * @param string $email
  183. *
  184. * @return string
  185. */
  186. protected function canonicalizeEmail($email)
  187. {
  188. return $this->emailCanonicalizer->canonicalize($email);
  189. }
  190. /**
  191. * Canonicalizes a username
  192. *
  193. * @param string $username
  194. *
  195. * @return string
  196. */
  197. protected function canonicalizeUsername($username)
  198. {
  199. return $this->usernameCanonicalizer->canonicalize($username);
  200. }
  201. protected function getEncoder(UserInterface $user)
  202. {
  203. return $this->encoderFactory->getEncoder($user);
  204. }
  205. /**
  206. * {@inheritDoc}
  207. * @deprecated Use FOS\UserBundle\Security\UserProvider instead
  208. */
  209. public function supportsClass($class)
  210. {
  211. @trigger_error('Using the UserManager as user provider is deprecated. Use FOS\UserBundle\Security\UserProvider instead.', E_USER_DEPRECATED);
  212. return $class === $this->getClass();
  213. }
  214. }