1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Users\Authentication; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Interop\Container\ContainerInterface; |
7
|
|
|
|
8
|
|
|
class AuthenticationController |
9
|
|
|
{ |
10
|
|
|
private $container; |
11
|
|
|
|
12
|
|
|
public function __construct(ContainerInterface $container) |
13
|
|
|
{ |
14
|
|
|
$this->container = $container; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return bool |
19
|
|
|
*/ |
20
|
|
|
public function isAuthenticated() |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
|
|
$this->getAuthenticatedUser(); |
24
|
|
|
|
25
|
|
|
return true; |
26
|
|
|
} catch (Exception $e) { |
27
|
|
|
return false; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $parameters |
33
|
|
|
* @return UserEntity |
34
|
|
|
* @throws AuthenticationException |
35
|
|
|
*/ |
36
|
|
|
public function getUserByEmailAndPassword(array $parameters) |
37
|
|
|
{ |
38
|
|
|
$user = $this->getUserRepository() |
|
|
|
|
39
|
|
|
->findOneByEmail($parameters['email']); |
40
|
|
|
|
41
|
|
|
if ($user instanceof UserEntity && $user->testPassword($parameters['password'])) { |
|
|
|
|
42
|
|
|
return $user; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
throw new AuthenticationException(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function login(string $email, string $password, bool $isLongSession = false) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
if ($parameters instanceof UserEntity) { |
|
|
|
|
52
|
|
|
$user = $parameters; |
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
$user = $this->getUserRepository() |
|
|
|
|
55
|
|
|
->findOneByEmail($parameters['email']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!($user instanceof UserEntity) |
|
|
|
|
59
|
|
|
|| (is_array($parameters) |
60
|
|
|
&& !$user->testPassword($parameters['password'])) |
61
|
|
|
) { |
62
|
|
|
throw new AuthenticationException( |
63
|
|
|
$this->getDictionary() |
|
|
|
|
64
|
|
|
->get('user.authentication.error.invalid_credentials') |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!$user->isActive()) { |
69
|
|
|
$statusLabel = (new UserController($this->getContainer())) |
|
|
|
|
70
|
|
|
->getUserStatusLabel($user->getStatus()); |
71
|
|
|
|
72
|
|
|
$accountDisabledError = $this->getDictionary() |
|
|
|
|
73
|
|
|
->get('user.authentication.error.account_disabled'); |
74
|
|
|
|
75
|
|
|
throw new AuthenticationException( |
76
|
|
|
sprintf($accountDisabledError, $statusLabel) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$longSession = is_array($parameters) ? |
82
|
|
|
(bool)$parameters['is_long_session'] : false; |
83
|
|
|
|
84
|
|
|
$this->setSession( |
|
|
|
|
85
|
|
|
|
86
|
|
|
$this->getContainer()->getSessionController()->createSession( |
|
|
|
|
87
|
|
|
$user, |
88
|
|
|
$longSession |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->triggerEvent('user.login'); |
93
|
|
|
|
94
|
|
View Code Duplication |
if (!$user->isEmailConfirmed()) { |
|
|
|
|
95
|
|
|
throw new AuthenticationException( |
96
|
|
|
$this->getDictionary() |
|
|
|
|
97
|
|
|
->get('user.authentication.error.email_not_confirmed') |
98
|
|
|
. '<br><a href="#" data-action="resend-confirmation-email">' . $this->getDictionary() |
|
|
|
|
99
|
|
|
->get('user.authentication.error.resend_confirmation_email') |
100
|
|
|
. '</a>' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
return $user; |
104
|
|
|
} catch (AuthenticationException $e) { |
|
|
|
|
105
|
|
|
throw $e; |
106
|
|
|
} catch (SessionCreationException $e) { |
|
|
|
|
107
|
|
|
throw new AuthenticationException( |
108
|
|
|
$this->getDictionary() |
|
|
|
|
109
|
|
|
->get('user.authentication.error.internal_error') |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function disconnectUser() |
115
|
|
|
{ |
116
|
|
|
$this->getContainer()->getSessionController()->deleteSession(); |
|
|
|
|
117
|
|
|
$this->triggerEvent('user.logout'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return UserEntity |
122
|
|
|
* @throws Exception |
123
|
|
|
*/ |
124
|
|
|
public function getAuthenticatedUser() |
125
|
|
|
{ |
126
|
|
|
if (isset($this->user)) { |
127
|
|
|
return $this->user; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$session = $this->getSession(); |
|
|
|
|
131
|
|
|
if ($session instanceof SessionEntity) { |
|
|
|
|
132
|
|
|
|
133
|
|
View Code Duplication |
if (!$session->getUser()->isEmailConfirmed()) { |
|
|
|
|
134
|
|
|
throw new AuthenticationException( |
135
|
|
|
$this->getDictionary() |
|
|
|
|
136
|
|
|
->get('user.authentication.error.email_not_confirmed') |
137
|
|
|
. '<br><a href="#" data-action="resend-confirmation-email">' . $this->getDictionary() |
|
|
|
|
138
|
|
|
->get('user.authentication.error.resend_confirmation_email') |
139
|
|
|
. '</a>' |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->user = $session->getUser(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
throw new Exception( |
147
|
|
|
$this->getDictionary() |
|
|
|
|
148
|
|
|
->get('user.authentication.error.not_authenticated') |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return bool|UserEntity |
154
|
|
|
* @deprecated |
155
|
|
|
*/ |
156
|
|
|
public function getPendingUser() |
157
|
|
|
{ |
158
|
|
|
|
159
|
|
|
$session = $this->getSession(); |
|
|
|
|
160
|
|
|
if ($session instanceof SessionEntity) { |
|
|
|
|
161
|
|
|
|
162
|
|
|
if ($session->getUser()->isEmailConfirmed()) { |
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $session->getUser(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return UserEntity|null |
174
|
|
|
*/ |
175
|
|
|
public function getUser() |
176
|
|
|
{ |
177
|
|
|
try { |
178
|
|
|
return $this->getAuthenticatedUser(); |
179
|
|
|
} catch (Exception $e) { |
180
|
|
|
return null; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $event |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
|
|
private function triggerEvent($event) |
190
|
|
|
{ |
191
|
|
|
$this->getContainer()->getEvenementEmitter()->emit($event); |
|
|
|
|
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.