|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Platine\App\Http\Action\User; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Platine\App\Helper\StatusList; |
|
9
|
|
|
use Platine\App\Param\UserParam; |
|
10
|
|
|
use Platine\App\Validator\UserValidator; |
|
11
|
|
|
use Platine\Framework\Auth\AuthenticationInterface; |
|
12
|
|
|
use Platine\Framework\Auth\Entity\User; |
|
13
|
|
|
use Platine\Framework\Auth\Repository\UserRepository; |
|
14
|
|
|
use Platine\Framework\Helper\Flash; |
|
15
|
|
|
use Platine\Framework\Http\RequestData; |
|
16
|
|
|
use Platine\Framework\Http\Response\RedirectResponse; |
|
17
|
|
|
use Platine\Framework\Http\Response\TemplateResponse; |
|
18
|
|
|
use Platine\Framework\Http\RouteHelper; |
|
19
|
|
|
use Platine\Http\ResponseInterface; |
|
20
|
|
|
use Platine\Http\ServerRequestInterface; |
|
21
|
|
|
use Platine\Lang\Lang; |
|
22
|
|
|
use Platine\Logger\LoggerInterface; |
|
23
|
|
|
use Platine\Security\Hash\HashInterface; |
|
24
|
|
|
use Platine\Template\Template; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @class UserProfileAction |
|
28
|
|
|
* @package Platine\App\Http\Action\User |
|
29
|
|
|
*/ |
|
30
|
|
|
class UserProfileAction |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* The Lang instance |
|
34
|
|
|
* @var Lang |
|
35
|
|
|
*/ |
|
36
|
|
|
protected Lang $lang; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The Template instance |
|
40
|
|
|
* @var Template |
|
41
|
|
|
*/ |
|
42
|
|
|
protected Template $template; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The Flash instance |
|
46
|
|
|
* @var Flash |
|
47
|
|
|
*/ |
|
48
|
|
|
protected Flash $flash; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The RouteHelper instance |
|
52
|
|
|
* @var RouteHelper |
|
53
|
|
|
*/ |
|
54
|
|
|
protected RouteHelper $routeHelper; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The LoggerInterface instance |
|
58
|
|
|
* @var LoggerInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
protected LoggerInterface $logger; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The StatusList instance |
|
64
|
|
|
* @var StatusList |
|
65
|
|
|
*/ |
|
66
|
|
|
protected StatusList $statusList; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* The HashInterface instance |
|
70
|
|
|
* @var HashInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
protected HashInterface $hash; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The UserRepository instance |
|
76
|
|
|
* @var UserRepository |
|
77
|
|
|
*/ |
|
78
|
|
|
protected UserRepository $userRepository; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The AuthenticationInterface instance |
|
82
|
|
|
* @var AuthenticationInterface |
|
83
|
|
|
*/ |
|
84
|
|
|
protected AuthenticationInterface $authentication; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create new instance |
|
88
|
|
|
* @param Lang $lang |
|
89
|
|
|
* @param AuthenticationInterface $authentication |
|
90
|
|
|
* @param Template $template |
|
91
|
|
|
* @param Flash $flash |
|
92
|
|
|
* @param RouteHelper $routeHelper |
|
93
|
|
|
* @param LoggerInterface $logger |
|
94
|
|
|
* @param StatusList $statusList |
|
95
|
|
|
* @param HashInterface $hash |
|
96
|
|
|
* @param UserRepository $userRepository |
|
97
|
|
|
*/ |
|
98
|
|
|
public function __construct( |
|
99
|
|
|
Lang $lang, |
|
100
|
|
|
AuthenticationInterface $authentication, |
|
101
|
|
|
Template $template, |
|
102
|
|
|
Flash $flash, |
|
103
|
|
|
RouteHelper $routeHelper, |
|
104
|
|
|
LoggerInterface $logger, |
|
105
|
|
|
StatusList $statusList, |
|
106
|
|
|
HashInterface $hash, |
|
107
|
|
|
UserRepository $userRepository |
|
108
|
|
|
) { |
|
109
|
|
|
$this->lang = $lang; |
|
110
|
|
|
$this->authentication = $authentication; |
|
111
|
|
|
$this->template = $template; |
|
112
|
|
|
$this->flash = $flash; |
|
113
|
|
|
$this->routeHelper = $routeHelper; |
|
114
|
|
|
$this->logger = $logger; |
|
115
|
|
|
$this->statusList = $statusList; |
|
116
|
|
|
$this->hash = $hash; |
|
117
|
|
|
$this->userRepository = $userRepository; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* User profile |
|
122
|
|
|
* @param ServerRequestInterface $request |
|
123
|
|
|
* @return ResponseInterface |
|
124
|
|
|
*/ |
|
125
|
|
|
public function detail(ServerRequestInterface $request): ResponseInterface |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$context = []; |
|
128
|
|
|
$id = $this->authentication->getUser()->getId(); |
|
129
|
|
|
|
|
130
|
|
|
/** @var User|null $user */ |
|
131
|
|
|
$user = $this->userRepository->find($id); |
|
132
|
|
|
|
|
133
|
|
|
if ($user === null) { |
|
134
|
|
|
$this->authentication->logout(); |
|
135
|
|
|
|
|
136
|
|
|
$this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
|
137
|
|
|
|
|
138
|
|
|
return new RedirectResponse( |
|
139
|
|
|
$this->routeHelper->generateUrl('user_login') |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$context['user'] = $user; |
|
144
|
|
|
$context['user_status'] = $this->statusList->getUserStatus(); |
|
145
|
|
|
|
|
146
|
|
|
return new TemplateResponse( |
|
147
|
|
|
$this->template, |
|
148
|
|
|
'user/profile', |
|
149
|
|
|
$context |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Update user profile |
|
155
|
|
|
* @param ServerRequestInterface $request |
|
156
|
|
|
* @return ResponseInterface |
|
157
|
|
|
*/ |
|
158
|
|
|
public function update(ServerRequestInterface $request): ResponseInterface |
|
159
|
|
|
{ |
|
160
|
|
|
$context = []; |
|
161
|
|
|
$param = new RequestData($request); |
|
162
|
|
|
|
|
163
|
|
|
$id = $this->authentication->getUser()->getId(); |
|
164
|
|
|
|
|
165
|
|
|
/** @var User|null $user */ |
|
166
|
|
|
$user = $this->userRepository->find($id); |
|
167
|
|
|
|
|
168
|
|
|
if ($user === null) { |
|
169
|
|
|
$this->authentication->logout(); |
|
170
|
|
|
|
|
171
|
|
|
$this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
|
172
|
|
|
|
|
173
|
|
|
return new RedirectResponse( |
|
174
|
|
|
$this->routeHelper->generateUrl('user_login') |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
$context['user'] = $user; |
|
178
|
|
|
$context['param'] = (new UserParam())->fromEntity($user); |
|
179
|
|
|
|
|
180
|
|
|
if ($request->getMethod() === 'GET') { |
|
181
|
|
|
return new TemplateResponse( |
|
182
|
|
|
$this->template, |
|
183
|
|
|
'user/update_profile', |
|
184
|
|
|
$context |
|
185
|
|
|
); |
|
186
|
|
|
} |
|
187
|
|
|
$formParam = new UserParam($param->posts()); |
|
188
|
|
|
$formParam->setStatus($user->status); |
|
189
|
|
|
|
|
190
|
|
|
$context['param'] = $formParam; |
|
191
|
|
|
|
|
192
|
|
|
$validator = new UserValidator($formParam, $this->lang, true); |
|
193
|
|
|
if ($validator->validate() === false) { |
|
194
|
|
|
$context['errors'] = $validator->getErrors(); |
|
195
|
|
|
|
|
196
|
|
|
return new TemplateResponse( |
|
197
|
|
|
$this->template, |
|
198
|
|
|
'user/update_profile', |
|
199
|
|
|
$context |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$usernameExist = $this->userRepository->findBy([ |
|
204
|
|
|
'username' => $formParam->getUsername(), |
|
205
|
|
|
]); |
|
206
|
|
|
|
|
207
|
|
|
if ($usernameExist !== null && $usernameExist->id !== $id) { |
|
208
|
|
|
$this->flash->setError($this->lang->tr('This username already exist')); |
|
209
|
|
|
|
|
210
|
|
|
return new TemplateResponse( |
|
211
|
|
|
$this->template, |
|
212
|
|
|
'user/update_profile', |
|
213
|
|
|
$context |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$emailExist = $this->userRepository->findBy([ |
|
218
|
|
|
'email' => $formParam->getEmail(), |
|
219
|
|
|
]); |
|
220
|
|
|
|
|
221
|
|
|
if ($emailExist !== null && $emailExist->id !== $id) { |
|
222
|
|
|
$this->flash->setError($this->lang->tr('This email already exist')); |
|
223
|
|
|
|
|
224
|
|
|
return new TemplateResponse( |
|
225
|
|
|
$this->template, |
|
226
|
|
|
'user/update_profile', |
|
227
|
|
|
$context |
|
228
|
|
|
); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
$user->username = $formParam->getUsername(); |
|
232
|
|
|
$user->lastname = $formParam->getLastname(); |
|
233
|
|
|
$user->firstname = $formParam->getFirstname(); |
|
234
|
|
|
$user->email = $formParam->getEmail(); |
|
235
|
|
|
$user->status = $formParam->getStatus(); |
|
236
|
|
|
|
|
237
|
|
|
$password = $formParam->getPassword(); |
|
238
|
|
|
if (!empty($password)) { |
|
239
|
|
|
$passwordHash = $this->hash->hash($password); |
|
240
|
|
|
|
|
241
|
|
|
$user->password = $passwordHash; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
try { |
|
245
|
|
|
$this->userRepository->save($user); |
|
246
|
|
|
|
|
247
|
|
|
$this->flash->setSuccess($this->lang->tr('Data successfully updated')); |
|
248
|
|
|
|
|
249
|
|
|
return new RedirectResponse( |
|
250
|
|
|
$this->routeHelper->generateUrl('user_profile') |
|
251
|
|
|
); |
|
252
|
|
|
} catch (Exception $ex) { |
|
253
|
|
|
$this->logger->error('Error when saved the data {error}', ['error' => $ex->getMessage()]); |
|
254
|
|
|
|
|
255
|
|
|
$this->flash->setError($this->lang->tr('Data processing error')); |
|
256
|
|
|
|
|
257
|
|
|
return new TemplateResponse( |
|
258
|
|
|
$this->template, |
|
259
|
|
|
'user/update_profile', |
|
260
|
|
|
$context |
|
261
|
|
|
); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.