1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Users\User; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Sinergi\Users\Container; |
7
|
|
|
use Sinergi\Users\User\Exception\EmailAlreadyConfirmedException; |
8
|
|
|
use Sinergi\Users\User\Exception\EmailConfirmationTokenBlockedException; |
9
|
|
|
use Sinergi\Users\User\Exception\EmailConfirmationTokenExpiredException; |
10
|
|
|
use Sinergi\Users\User\Exception\InvalidEmailConfirmationTokenException; |
11
|
|
|
use Sinergi\Users\User\Exception\InvalidUserException; |
12
|
|
|
use Sinergi\Users\User\Exception\TooManyEmailConfirmationTokenAttemptsException; |
13
|
|
|
use DateInterval; |
14
|
|
|
|
15
|
|
|
class UserController |
16
|
|
|
{ |
17
|
|
|
private $container; |
18
|
|
|
|
19
|
|
|
public function __construct(ContainerInterface $container) |
20
|
|
|
{ |
21
|
|
|
if ($container instanceof Container) { |
22
|
|
|
$this->container = $container; |
23
|
|
|
} else { |
24
|
|
|
$this->container = new Container($container); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function generateEmailConfirmationToken( |
29
|
|
|
UserEntityInterface $user, |
30
|
|
|
string $token = null, |
31
|
|
|
DateInterval $expiration = null, |
32
|
|
|
bool $save = true |
33
|
|
|
): UserEntityInterface { |
34
|
|
|
if ($user->isEmailConfirmed()) { |
35
|
|
|
throw new EmailAlreadyConfirmedException; |
36
|
|
|
} elseif ($user->getEmailConfirmationToken() && !$user->hasEmailConfirmationTokenCooldownExpired()) { |
37
|
|
|
throw new EmailConfirmationTokenBlockedException; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$user->generateEmailConfirmationToken($token, $expiration); |
41
|
|
|
if ($save) { |
42
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
43
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
44
|
|
|
$userRepository->save($user); |
45
|
|
|
} |
46
|
|
|
return $user; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function confirmEmail(UserEntityInterface $user, string $emailConfirmationToken): UserEntityInterface |
50
|
|
|
{ |
51
|
|
|
if ($user->isEmailConfirmed()) { |
52
|
|
|
throw new EmailAlreadyConfirmedException; |
53
|
|
|
} elseif ($user->hasEmailConfirmationTokenExpired()) { |
54
|
|
|
throw new EmailConfirmationTokenExpiredException; |
55
|
|
|
} elseif ($user->hasTooManyEmailConfirmationTokenAttempts()) { |
56
|
|
|
throw new TooManyEmailConfirmationTokenAttemptsException; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
60
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
61
|
|
|
|
62
|
|
|
if ($user->getEmailConfirmationToken() !== $emailConfirmationToken) { |
63
|
|
|
$user->setEmailConfirmationTokenAttempts($user->getEmailConfirmationTokenAttempts() + 1); |
64
|
|
|
$userRepository->save($user); |
65
|
|
|
throw new InvalidEmailConfirmationTokenException; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$user->setIsEmailConfirmed(true); |
69
|
|
|
$user->setEmailConfirmationToken(null); |
70
|
|
|
$user->setEmailConfirmationTokenAttempts(0); |
71
|
|
|
$user->setEmailConfirmationTokenExpirationDatetime(null); |
72
|
|
|
$userRepository->save($user); |
73
|
|
|
return $user; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function createUser(UserEntityInterface $user): UserEntityInterface |
77
|
|
|
{ |
78
|
|
|
/** @var UserValidatorInterface $userValidator */ |
79
|
|
|
$userValidator = $this->container->get(UserValidatorInterface::class); |
80
|
|
|
$errors = $userValidator($user); |
81
|
|
|
|
82
|
|
|
if (!empty($errors)) { |
83
|
|
|
throw new InvalidUserException($errors); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** @var UserRepositoryInterface $userRepository */ |
87
|
|
|
$userRepository = $this->container->get(UserRepositoryInterface::class); |
88
|
|
|
$userRepository->save($user); |
89
|
|
|
|
90
|
|
|
$this->getEmailConfirmationController() |
|
|
|
|
91
|
|
|
->sendConfirmationEmail($user); |
92
|
|
|
|
93
|
|
|
return $user; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param UserEntity $userEntity |
98
|
|
|
* @param array $parameters |
99
|
|
|
*/ |
100
|
|
|
public function updateUser(UserEntity $userEntity, array $parameters) |
101
|
|
|
{ |
102
|
|
|
$options = [UserValidator::OPTION_SKIP_PASSWORD_VALIDATION]; |
103
|
|
|
|
104
|
|
|
//todo : refactored this : |
105
|
|
|
|
106
|
|
|
$parameters = array_merge($userEntity->toArray(), $parameters); |
107
|
|
|
|
108
|
|
|
if ($userEntity->getEmail() === $parameters['email']) { |
109
|
|
|
$options[] = UserValidator::OPTION_SKIP_EMAIL_UNIQUENESS; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->getUserValidator()->validateParameters($parameters, $options); |
|
|
|
|
113
|
|
|
|
114
|
|
|
if ($userEntity->getEmail() !== $parameters['email']) { |
115
|
|
|
$userEntity->setPendingEmail($parameters['email']); |
116
|
|
|
|
117
|
|
|
(new EmailConfirmationController($this->getContainer()))->emailUpdated($userEntity); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$userEntity |
121
|
|
|
->setName($parameters['name']) |
122
|
|
|
->setPhone($parameters['phone']); |
123
|
|
|
|
124
|
|
|
$this->getEntityManager()->persist($userEntity); |
|
|
|
|
125
|
|
|
$this->getEntityManager()->flush($userEntity); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param userEntity $userEntity |
130
|
|
|
* @param array $parameters |
131
|
|
|
* @throws AuthenticationException |
132
|
|
|
*/ |
133
|
|
|
public function updatePassword(userEntity $userEntity, array $parameters) |
134
|
|
|
{ |
135
|
|
|
if (!$userEntity->testPassword($parameters['current-password'])) { |
136
|
|
|
throw new AuthenticationException($this->getDictionary()->get('user.password.error.wrong_password')); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->getUserValidator()->validateParameters($parameters, |
|
|
|
|
140
|
|
|
[UserValidator::OPTION_JUST_PASSWORD]); |
141
|
|
|
|
142
|
|
|
$userEntity->setPassword($parameters['password']); |
143
|
|
|
$this->getEntityManager()->persist($userEntity); |
|
|
|
|
144
|
|
|
$this->getEntityManager()->flush($userEntity); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param userEntity $userEntity |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
public function deleteUser(userEntity $userEntity) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$userEntity->setStatus(UserEntity::STATUS_DELETED); |
153
|
|
|
$userEntity->setDeletedEmail($userEntity->getEmail()); |
154
|
|
|
$userEntity->setEmail(null); |
155
|
|
|
$this->getContainer()->getEntityManager()->persist($userEntity); |
|
|
|
|
156
|
|
|
$this->getContainer()->getEntityManager()->flush($userEntity); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param userEntity $userEntity |
161
|
|
|
*/ |
162
|
|
View Code Duplication |
public function banUser(userEntity $userEntity) |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
$userEntity->setStatus(UserEntity::STATUS_BANNED); |
165
|
|
|
$userEntity->setDeletedEmail($userEntity->getEmail()); |
166
|
|
|
$userEntity->setEmail(null); |
167
|
|
|
$this->getContainer()->getEntityManager()->persist($userEntity); |
|
|
|
|
168
|
|
|
$this->getContainer()->getEntityManager()->flush($userEntity); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $status |
173
|
|
|
* |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
|
|
public function getUserStatusLabel($status) |
177
|
|
|
{ |
178
|
|
|
switch ($status) { |
179
|
|
|
case UserEntity::STATUS_ACTIVE: |
180
|
|
|
return $this->getDictionary()->get('user.status.active'); |
|
|
|
|
181
|
|
|
case UserEntity::STATUS_DELETED: |
182
|
|
|
return $this->getDictionary()->get('user.status.deleted'); |
|
|
|
|
183
|
|
|
break; |
|
|
|
|
184
|
|
|
case UserEntity::STATUS_BANNED: |
185
|
|
|
return $this->getDictionary()->get('user.status.banned'); |
|
|
|
|
186
|
|
|
break; |
|
|
|
|
187
|
|
|
default: |
188
|
|
|
return $this->getDictionary()->get('user.status.inactive'); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.