1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Users\User; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Sinergi\Users\Container; |
7
|
|
|
use Sinergi\Users\Throttle\Throttle; |
8
|
|
|
use Sinergi\Users\User\Exception\EmailAlreadyConfirmedException; |
9
|
|
|
use Sinergi\Users\User\Exception\EmailConfirmationTokenBlockedException; |
10
|
|
|
use Sinergi\Users\User\Exception\EmailConfirmationTokenExpiredException; |
11
|
|
|
use Sinergi\Users\User\Exception\InvalidEmailConfirmationTokenException; |
12
|
|
|
use Sinergi\Users\User\Exception\InvalidPasswordResetTokenException; |
13
|
|
|
use Sinergi\Users\User\Exception\InvalidUserException; |
14
|
|
|
use Sinergi\Users\User\Exception\PasswordResetTokenBlockedException; |
15
|
|
|
use Sinergi\Users\User\Exception\PasswordResetTokenExpiredException; |
16
|
|
|
use Sinergi\Users\User\Exception\TooManyEmailConfirmationTokenAttemptsException; |
17
|
|
|
use DateInterval; |
18
|
|
|
use Sinergi\Users\User\Exception\UserNotFoundException; |
19
|
|
|
|
20
|
|
|
class UserController |
21
|
|
|
{ |
22
|
|
|
private $container; |
23
|
|
|
|
24
|
|
|
public function __construct(ContainerInterface $container) |
25
|
|
|
{ |
26
|
|
|
if ($container instanceof Container) { |
27
|
|
|
$this->container = $container; |
28
|
|
|
} else { |
29
|
|
|
$this->container = new Container($container); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function generateEmailConfirmationToken( |
34
|
|
|
UserEntityInterface $user, |
35
|
|
|
string $token = null, |
36
|
|
|
DateInterval $expiration = null, |
37
|
|
|
bool $save = true |
38
|
|
|
): UserEntityInterface { |
39
|
|
|
if ($user->isEmailConfirmed()) { |
40
|
|
|
throw new EmailAlreadyConfirmedException; |
41
|
|
|
} elseif ($user->getEmailConfirmationToken() && !$user->hasEmailConfirmationTokenCooldownExpired()) { |
42
|
|
|
throw new EmailConfirmationTokenBlockedException; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$user->generateEmailConfirmationToken($token, $expiration); |
46
|
|
|
if ($save) { |
47
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
48
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
49
|
|
|
$userRepository->save($user); |
50
|
|
|
} |
51
|
|
|
return $user; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function generatePassswordResetToken( |
55
|
|
|
string $email, |
56
|
|
|
string $token = null, |
57
|
|
|
DateInterval $expiration = null, |
58
|
|
|
string $ip = null, |
59
|
|
|
bool $save = true |
60
|
|
|
): UserEntityInterface { |
61
|
|
|
if ($ip) { |
|
|
|
|
62
|
|
|
Throttle::throttle($ip); |
63
|
|
|
} |
64
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
65
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
66
|
|
|
$user = $userRepository->findByEmail($email); |
67
|
|
|
if (!$user) { |
68
|
|
|
throw new UserNotFoundException; |
69
|
|
|
} elseif ($user->getPasswordResetToken() && !$user->hasPasswordResetTokenCooldownExpired()) { |
70
|
|
|
throw new PasswordResetTokenBlockedException; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$user->generatePasswordResetToken($token, $expiration); |
74
|
|
|
if ($save) { |
75
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
76
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
77
|
|
|
$userRepository->save($user); |
78
|
|
|
} |
79
|
|
|
return $user; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function resetPassword( |
83
|
|
|
string $token, |
84
|
|
|
string $password, |
85
|
|
|
string $ip = null, |
86
|
|
|
bool $save = true |
87
|
|
|
): UserEntityInterface { |
88
|
|
|
if ($ip) { |
|
|
|
|
89
|
|
|
Throttle::throttle($ip); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
93
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
94
|
|
|
$user = $userRepository->findByResetPasswordToken($token); |
95
|
|
|
|
96
|
|
|
if (!$user) { |
97
|
|
|
throw new InvalidPasswordResetTokenException; |
98
|
|
|
} elseif ($user->hasPasswordResetTokenExpired()) { |
99
|
|
|
throw new PasswordResetTokenExpiredException; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$user->setPassword($password); |
103
|
|
|
|
104
|
|
|
/** @var UserValidatorInterface $userValidator */ |
105
|
|
|
$userValidator = $this->container->get(UserValidatorInterface::class); |
106
|
|
|
$errors = $userValidator($user); |
107
|
|
|
|
108
|
|
|
if (count($errors)) { |
109
|
|
|
throw new InvalidUserException($errors); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$user->setPasswordResetToken(null); |
113
|
|
|
$user->setPasswordResetTokenExpirationDatetime(null); |
114
|
|
|
|
115
|
|
|
if ($save) { |
116
|
|
|
$userRepository->save($user); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $user; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function confirmEmail(UserEntityInterface $user, string $emailConfirmationToken): UserEntityInterface |
123
|
|
|
{ |
124
|
|
|
if ($user->isEmailConfirmed()) { |
125
|
|
|
throw new EmailAlreadyConfirmedException; |
126
|
|
|
} elseif ($user->hasEmailConfirmationTokenExpired()) { |
127
|
|
|
throw new EmailConfirmationTokenExpiredException; |
128
|
|
|
} elseif ($user->hasTooManyEmailConfirmationTokenAttempts()) { |
129
|
|
|
throw new TooManyEmailConfirmationTokenAttemptsException; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
133
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
134
|
|
|
|
135
|
|
|
if ($user->getEmailConfirmationToken() !== $emailConfirmationToken) { |
136
|
|
|
$user->setEmailConfirmationTokenAttempts($user->getEmailConfirmationTokenAttempts() + 1); |
137
|
|
|
$userRepository->save($user); |
138
|
|
|
throw new InvalidEmailConfirmationTokenException; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$user->setIsEmailConfirmed(true); |
142
|
|
|
$user->setEmailConfirmationToken(null); |
143
|
|
|
$user->setEmailConfirmationTokenAttempts(0); |
144
|
|
|
$user->setEmailConfirmationTokenExpirationDatetime(null); |
145
|
|
|
$userRepository->save($user); |
146
|
|
|
return $user; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function createUser(UserEntityInterface $user): UserEntityInterface |
150
|
|
|
{ |
151
|
|
|
/** @var UserValidatorInterface $userValidator */ |
152
|
|
|
$userValidator = $this->container->get(UserValidatorInterface::class); |
153
|
|
|
$errors = $userValidator($user); |
154
|
|
|
|
155
|
|
|
if (!empty($errors)) { |
156
|
|
|
throw new InvalidUserException($errors); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
160
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
161
|
|
|
$userRepository->save($user); |
162
|
|
|
|
163
|
|
|
$this->getEmailConfirmationController() |
|
|
|
|
164
|
|
|
->sendConfirmationEmail($user); |
165
|
|
|
|
166
|
|
|
return $user; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param UserEntity $userEntity |
171
|
|
|
* @param array $parameters |
172
|
|
|
*/ |
173
|
|
|
public function updateUser(UserEntity $userEntity, array $parameters) |
174
|
|
|
{ |
175
|
|
|
$options = [UserValidator::OPTION_SKIP_PASSWORD_VALIDATION]; |
176
|
|
|
|
177
|
|
|
//todo : refactored this : |
178
|
|
|
|
179
|
|
|
$parameters = array_merge($userEntity->toArray(), $parameters); |
180
|
|
|
|
181
|
|
|
if ($userEntity->getEmail() === $parameters['email']) { |
182
|
|
|
$options[] = UserValidator::OPTION_SKIP_EMAIL_UNIQUENESS; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->getUserValidator()->validateParameters($parameters, $options); |
|
|
|
|
186
|
|
|
|
187
|
|
|
if ($userEntity->getEmail() !== $parameters['email']) { |
188
|
|
|
$userEntity->setPendingEmail($parameters['email']); |
189
|
|
|
|
190
|
|
|
(new EmailConfirmationController($this->getContainer()))->emailUpdated($userEntity); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$userEntity |
194
|
|
|
->setName($parameters['name']) |
195
|
|
|
->setPhone($parameters['phone']); |
196
|
|
|
|
197
|
|
|
$this->getEntityManager()->persist($userEntity); |
|
|
|
|
198
|
|
|
$this->getEntityManager()->flush($userEntity); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param userEntity $userEntity |
203
|
|
|
* @param array $parameters |
204
|
|
|
* @throws AuthenticationException |
205
|
|
|
*/ |
206
|
|
|
public function updatePassword(userEntity $userEntity, array $parameters) |
207
|
|
|
{ |
208
|
|
|
if (!$userEntity->testPassword($parameters['current-password'])) { |
209
|
|
|
throw new AuthenticationException($this->getDictionary()->get('user.password.error.wrong_password')); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$this->getUserValidator()->validateParameters($parameters, |
|
|
|
|
213
|
|
|
[UserValidator::OPTION_JUST_PASSWORD]); |
214
|
|
|
|
215
|
|
|
$userEntity->setPassword($parameters['password']); |
216
|
|
|
$this->getEntityManager()->persist($userEntity); |
|
|
|
|
217
|
|
|
$this->getEntityManager()->flush($userEntity); |
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param userEntity $userEntity |
222
|
|
|
*/ |
223
|
|
View Code Duplication |
public function deleteUser(userEntity $userEntity) |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
$userEntity->setStatus(UserEntity::STATUS_DELETED); |
226
|
|
|
$userEntity->setDeletedEmail($userEntity->getEmail()); |
227
|
|
|
$userEntity->setEmail(null); |
228
|
|
|
$this->getContainer()->getEntityManager()->persist($userEntity); |
|
|
|
|
229
|
|
|
$this->getContainer()->getEntityManager()->flush($userEntity); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param userEntity $userEntity |
234
|
|
|
*/ |
235
|
|
View Code Duplication |
public function banUser(userEntity $userEntity) |
|
|
|
|
236
|
|
|
{ |
237
|
|
|
$userEntity->setStatus(UserEntity::STATUS_BANNED); |
238
|
|
|
$userEntity->setDeletedEmail($userEntity->getEmail()); |
239
|
|
|
$userEntity->setEmail(null); |
240
|
|
|
$this->getContainer()->getEntityManager()->persist($userEntity); |
|
|
|
|
241
|
|
|
$this->getContainer()->getEntityManager()->flush($userEntity); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param $status |
246
|
|
|
* |
247
|
|
|
* @return mixed |
248
|
|
|
*/ |
249
|
|
|
public function getUserStatusLabel($status) |
250
|
|
|
{ |
251
|
|
|
switch ($status) { |
252
|
|
|
case UserEntity::STATUS_ACTIVE: |
253
|
|
|
return $this->getDictionary()->get('user.status.active'); |
|
|
|
|
254
|
|
|
case UserEntity::STATUS_DELETED: |
255
|
|
|
return $this->getDictionary()->get('user.status.deleted'); |
|
|
|
|
256
|
|
|
break; |
|
|
|
|
257
|
|
|
case UserEntity::STATUS_BANNED: |
258
|
|
|
return $this->getDictionary()->get('user.status.banned'); |
|
|
|
|
259
|
|
|
break; |
|
|
|
|
260
|
|
|
default: |
261
|
|
|
return $this->getDictionary()->get('user.status.inactive'); |
|
|
|
|
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: