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
|
|
|
* Create new instance |
34
|
|
|
* @param Lang $lang |
35
|
|
|
* @param AuthenticationInterface $authentication |
36
|
|
|
* @param Template $template |
37
|
|
|
* @param Flash $flash |
38
|
|
|
* @param RouteHelper $routeHelper |
39
|
|
|
* @param LoggerInterface $logger |
40
|
|
|
* @param StatusList $statusList |
41
|
|
|
* @param HashInterface $hash |
42
|
|
|
* @param UserRepository $userRepository |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
protected Lang $lang, |
46
|
|
|
protected AuthenticationInterface $authentication, |
47
|
|
|
protected Template $template, |
48
|
|
|
protected Flash $flash, |
49
|
|
|
protected RouteHelper $routeHelper, |
50
|
|
|
protected LoggerInterface $logger, |
51
|
|
|
protected StatusList $statusList, |
52
|
|
|
protected HashInterface $hash, |
53
|
|
|
protected UserRepository $userRepository |
54
|
|
|
) { |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* User profile |
59
|
|
|
* @param ServerRequestInterface $request |
60
|
|
|
* @return ResponseInterface |
61
|
|
|
*/ |
62
|
|
|
public function detail(ServerRequestInterface $request): ResponseInterface |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$context = []; |
65
|
|
|
$id = $this->authentication->getUser()->getId(); |
66
|
|
|
|
67
|
|
|
/** @var User|null $user */ |
68
|
|
|
$user = $this->userRepository->find($id); |
69
|
|
|
|
70
|
|
|
if ($user === null) { |
71
|
|
|
$this->authentication->logout(); |
72
|
|
|
|
73
|
|
|
$this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
74
|
|
|
|
75
|
|
|
return new RedirectResponse( |
76
|
|
|
$this->routeHelper->generateUrl('user_login') |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$context['user'] = $user; |
81
|
|
|
$context['user_status'] = $this->statusList->getUserStatus(); |
82
|
|
|
|
83
|
|
|
return new TemplateResponse( |
84
|
|
|
$this->template, |
85
|
|
|
'user/profile', |
86
|
|
|
$context |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Update user profile |
92
|
|
|
* @param ServerRequestInterface $request |
93
|
|
|
* @return ResponseInterface |
94
|
|
|
*/ |
95
|
|
|
public function update(ServerRequestInterface $request): ResponseInterface |
96
|
|
|
{ |
97
|
|
|
$context = []; |
98
|
|
|
$param = new RequestData($request); |
99
|
|
|
|
100
|
|
|
$id = $this->authentication->getUser()->getId(); |
101
|
|
|
|
102
|
|
|
/** @var User|null $user */ |
103
|
|
|
$user = $this->userRepository->find($id); |
104
|
|
|
|
105
|
|
|
if ($user === null) { |
106
|
|
|
$this->authentication->logout(); |
107
|
|
|
|
108
|
|
|
$this->flash->setError($this->lang->tr('This record doesn\'t exist')); |
109
|
|
|
|
110
|
|
|
return new RedirectResponse( |
111
|
|
|
$this->routeHelper->generateUrl('user_login') |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
$context['user'] = $user; |
115
|
|
|
$context['param'] = (new UserParam())->fromEntity($user); |
116
|
|
|
|
117
|
|
|
if ($request->getMethod() === 'GET') { |
118
|
|
|
return new TemplateResponse( |
119
|
|
|
$this->template, |
120
|
|
|
'user/update_profile', |
121
|
|
|
$context |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
$formParam = new UserParam($param->posts()); |
125
|
|
|
$formParam->setStatus($user->status); |
126
|
|
|
|
127
|
|
|
$context['param'] = $formParam; |
128
|
|
|
|
129
|
|
|
$validator = new UserValidator($formParam, $this->lang, true); |
130
|
|
|
if ($validator->validate() === false) { |
131
|
|
|
$context['errors'] = $validator->getErrors(); |
132
|
|
|
|
133
|
|
|
return new TemplateResponse( |
134
|
|
|
$this->template, |
135
|
|
|
'user/update_profile', |
136
|
|
|
$context |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$usernameExist = $this->userRepository->findBy([ |
141
|
|
|
'username' => $formParam->getUsername(), |
142
|
|
|
]); |
143
|
|
|
|
144
|
|
|
if ($usernameExist !== null && $usernameExist->id !== $id) { |
145
|
|
|
$this->flash->setError($this->lang->tr('This username already exist')); |
146
|
|
|
|
147
|
|
|
return new TemplateResponse( |
148
|
|
|
$this->template, |
149
|
|
|
'user/update_profile', |
150
|
|
|
$context |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$emailExist = $this->userRepository->findBy([ |
155
|
|
|
'email' => $formParam->getEmail(), |
156
|
|
|
]); |
157
|
|
|
|
158
|
|
|
if ($emailExist !== null && $emailExist->id !== $id) { |
159
|
|
|
$this->flash->setError($this->lang->tr('This email already exist')); |
160
|
|
|
|
161
|
|
|
return new TemplateResponse( |
162
|
|
|
$this->template, |
163
|
|
|
'user/update_profile', |
164
|
|
|
$context |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$user->username = $formParam->getUsername(); |
169
|
|
|
$user->lastname = $formParam->getLastname(); |
170
|
|
|
$user->firstname = $formParam->getFirstname(); |
171
|
|
|
$user->email = $formParam->getEmail(); |
172
|
|
|
$user->status = $formParam->getStatus(); |
173
|
|
|
|
174
|
|
|
$password = $formParam->getPassword(); |
175
|
|
|
if (!empty($password)) { |
176
|
|
|
$passwordHash = $this->hash->hash($password); |
177
|
|
|
|
178
|
|
|
$user->password = $passwordHash; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
try { |
182
|
|
|
$this->userRepository->save($user); |
183
|
|
|
|
184
|
|
|
$this->flash->setSuccess($this->lang->tr('Data successfully updated')); |
185
|
|
|
|
186
|
|
|
return new RedirectResponse( |
187
|
|
|
$this->routeHelper->generateUrl('user_profile') |
188
|
|
|
); |
189
|
|
|
} catch (Exception $ex) { |
190
|
|
|
$this->logger->error('Error when saved the data {error}', ['error' => $ex->getMessage()]); |
191
|
|
|
|
192
|
|
|
$this->flash->setError($this->lang->tr('Data processing error')); |
193
|
|
|
|
194
|
|
|
return new TemplateResponse( |
195
|
|
|
$this->template, |
196
|
|
|
'user/update_profile', |
197
|
|
|
$context |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.