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