|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Flextype; |
|
6
|
|
|
|
|
7
|
|
|
use Flextype\Component\Filesystem\Filesystem; |
|
|
|
|
|
|
8
|
|
|
use Flextype\Component\Session\Session; |
|
|
|
|
|
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
|
|
|
|
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
|
|
|
|
|
11
|
|
|
use Ramsey\Uuid\Uuid; |
|
|
|
|
|
|
12
|
|
|
use function count; |
|
13
|
|
|
use function date; |
|
14
|
|
|
use function Flextype\Component\I18n\__; |
|
|
|
|
|
|
15
|
|
|
use function password_hash; |
|
16
|
|
|
use function password_verify; |
|
17
|
|
|
use function time; |
|
18
|
|
|
use function trim; |
|
19
|
|
|
use const PASSWORD_BCRYPT; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @property View $view |
|
23
|
|
|
* @property Router $router |
|
24
|
|
|
* @property Slugify $slugify |
|
25
|
|
|
* @property Flash $flash |
|
26
|
|
|
*/ |
|
27
|
|
|
class UsersController extends Container |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Login page |
|
31
|
|
|
* |
|
32
|
|
|
* @param Request $request PSR7 request |
|
33
|
|
|
* @param Response $response PSR7 response |
|
34
|
|
|
*/ |
|
35
|
|
|
public function login(Request $request, Response $response) : Response |
|
36
|
|
|
{ |
|
37
|
|
|
$users = $this->getUsersList(); |
|
38
|
|
|
|
|
39
|
|
|
if ((Session::exists('role') && Session::get('role') === 'admin')) { |
|
40
|
|
|
return $response->withRedirect($this->router->pathFor('admin.entries.index')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (count($users) > 0) { |
|
44
|
|
|
return $this->container->get('twig')->render( |
|
45
|
|
|
$response, |
|
46
|
|
|
'plugins/admin/templates/users/login.html' |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $response->withRedirect($this->router->pathFor('admin.users.installation')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Login page process |
|
55
|
|
|
* |
|
56
|
|
|
* @param Request $request PSR7 request |
|
57
|
|
|
* @param Response $response PSR7 response |
|
58
|
|
|
*/ |
|
59
|
|
|
public function loginProcess(Request $request, Response $response) : Response |
|
60
|
|
|
{ |
|
61
|
|
|
$data = $request->getParsedBody(); |
|
62
|
|
|
|
|
63
|
|
|
if (Filesystem::has($_user_file = PATH['site'] . '/accounts/' . $data['username'] . '/profile.yaml')) { |
|
|
|
|
|
|
64
|
|
|
$user_file = $this->parser->decode(Filesystem::read($_user_file), 'yaml', false); |
|
65
|
|
|
if (password_verify(trim($data['password']), $user_file['hashed_password'])) { |
|
66
|
|
|
Session::set('username', $user_file['username']); |
|
67
|
|
|
Session::set('role', $user_file['role']); |
|
68
|
|
|
Session::set('uuid', $user_file['uuid']); |
|
69
|
|
|
|
|
70
|
|
|
return $response->withRedirect($this->router->pathFor('admin.entries.index')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->flash->addMessage('error', __('admin_message_wrong_username_password')); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
return $response->withRedirect($this->router->pathFor('admin.users.login')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$this->flash->addMessage('error', __('admin_message_wrong_username_password')); |
|
79
|
|
|
|
|
80
|
|
|
return $response->withRedirect($this->router->pathFor('admin.users.login')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Installation page |
|
85
|
|
|
* |
|
86
|
|
|
* @param Request $request PSR7 request |
|
87
|
|
|
* @param Response $response PSR7 response |
|
88
|
|
|
*/ |
|
89
|
|
|
public function installation(Request $request, Response $response) : Response |
|
90
|
|
|
{ |
|
91
|
|
|
$users = $this->getUsersList(); |
|
92
|
|
|
|
|
93
|
|
|
if (count($users) > 0) { |
|
94
|
|
|
return $response->withRedirect($this->router->pathFor('admin.users.login')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if ((Session::exists('role') && Session::get('role') === 'admin')) { |
|
98
|
|
|
return $response->withRedirect($this->router->pathFor('admin.entries.index')); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->twig->render( |
|
102
|
|
|
$response, |
|
103
|
|
|
'plugins/admin/templates/users/installation.html' |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Installation page process |
|
109
|
|
|
* |
|
110
|
|
|
* @param Request $request PSR7 request |
|
111
|
|
|
* @param Response $response PSR7 response |
|
112
|
|
|
*/ |
|
113
|
|
|
public function installationProcess(Request $request, Response $response) : Response |
|
114
|
|
|
{ |
|
115
|
|
|
// Get POST data |
|
116
|
|
|
$data = $request->getParsedBody(); |
|
117
|
|
|
|
|
118
|
|
|
if (! Filesystem::has($_user_file = PATH['site'] . '/accounts/' . $this->slugify->slugify($data['username']) . '/profile.yaml')) { |
|
|
|
|
|
|
119
|
|
|
// Generate UUID |
|
120
|
|
|
$uuid = Uuid::uuid4()->toString(); |
|
121
|
|
|
|
|
122
|
|
|
// Get time |
|
123
|
|
|
$time = date($this->registry->get('flextype.settings.date_format'), time()); |
|
124
|
|
|
|
|
125
|
|
|
// Create accounts directory and account |
|
126
|
|
|
Filesystem::createDir(PATH['site'] . '/accounts/' . $this->slugify->slugify($data['username'])); |
|
127
|
|
|
|
|
128
|
|
|
// Create admin account |
|
129
|
|
|
if (Filesystem::write( |
|
130
|
|
|
PATH['site'] . '/accounts/' . $this->slugify->slugify($data['username']) . '/profile.yaml', |
|
131
|
|
|
$this->parser->encode([ |
|
132
|
|
|
'username' => $this->slugify->slugify($data['username']), |
|
133
|
|
|
'hashed_password' => password_hash($data['password'], PASSWORD_BCRYPT), |
|
134
|
|
|
'email' => $data['email'], |
|
135
|
|
|
'role' => 'admin', |
|
136
|
|
|
'state' => 'enabled', |
|
137
|
|
|
'uuid' => $uuid, |
|
138
|
|
|
], 'yaml') |
|
139
|
|
|
)) { |
|
140
|
|
|
|
|
141
|
|
|
// Update default entry |
|
142
|
|
|
$this->entries->update('home', ['created_by' => $uuid, 'published_by' => $uuid, 'published_at' => $time, 'created_at' => $time]); |
|
143
|
|
|
|
|
144
|
|
|
// Create default entries delivery token |
|
145
|
|
|
$api_delivery_entries_token = bin2hex(random_bytes(16)); |
|
146
|
|
|
$api_delivery_entries_token_dir_path = PATH['site'] . '/tokens' . '/delivery/entries/' . $api_delivery_entries_token; |
|
147
|
|
|
$api_delivery_entries_token_file_path = $api_delivery_entries_token_dir_path . '/token.yaml'; |
|
148
|
|
|
|
|
149
|
|
|
if (! Filesystem::has($api_delivery_entries_token_dir_path)) Filesystem::createDir($api_delivery_entries_token_dir_path); |
|
150
|
|
|
|
|
151
|
|
|
Filesystem::write( |
|
152
|
|
|
$api_delivery_entries_token_file_path, |
|
153
|
|
|
$this->parser->encode([ |
|
154
|
|
|
'title' => 'Default', |
|
155
|
|
|
'icon' => 'fas fa-database', |
|
156
|
|
|
'limit_calls' => (int) 0, |
|
157
|
|
|
'calls' => (int) 0, |
|
158
|
|
|
'state' => 'enabled', |
|
159
|
|
|
'uuid' => $uuid, |
|
160
|
|
|
'created_by' => $uuid, |
|
161
|
|
|
'created_at' => $time, |
|
162
|
|
|
'updated_by' => $uuid, |
|
163
|
|
|
'updated_at' => $time, |
|
164
|
|
|
], 'yaml') |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
// Create default images delivery token |
|
168
|
|
|
$api_delivery_images_token = bin2hex(random_bytes(16)); |
|
169
|
|
|
$api_delivery_images_token_dir_path = PATH['site'] . '/tokens' . '/delivery/images/' . $api_delivery_images_token; |
|
170
|
|
|
$api_delivery_images_token_file_path = $api_delivery_images_token_dir_path . '/token.yaml'; |
|
171
|
|
|
|
|
172
|
|
|
if (! Filesystem::has($api_delivery_images_token_dir_path)) Filesystem::createDir($api_delivery_images_token_dir_path); |
|
173
|
|
|
|
|
174
|
|
|
Filesystem::write( |
|
175
|
|
|
$api_delivery_images_token_file_path, |
|
176
|
|
|
$this->parser->encode([ |
|
177
|
|
|
'title' => 'Default', |
|
178
|
|
|
'icon' => 'far fa-images', |
|
179
|
|
|
'limit_calls' => (int) 0, |
|
180
|
|
|
'calls' => (int) 0, |
|
181
|
|
|
'state' => 'enabled', |
|
182
|
|
|
'uuid' => $uuid, |
|
183
|
|
|
'created_by' => $uuid, |
|
184
|
|
|
'created_at' => $time, |
|
185
|
|
|
'updated_by' => $uuid, |
|
186
|
|
|
'updated_at' => $time, |
|
187
|
|
|
], 'yaml') |
|
188
|
|
|
); |
|
189
|
|
|
|
|
190
|
|
|
// Create default registry delivery token |
|
191
|
|
|
$api_delivery_registry_token = bin2hex(random_bytes(16)); |
|
192
|
|
|
$api_delivery_registry_token_dir_path = PATH['site'] . '/tokens' . '/delivery/registry/' . $api_delivery_registry_token; |
|
193
|
|
|
$api_delivery_registry_token_file_path = $api_delivery_registry_token_dir_path . '/token.yaml'; |
|
194
|
|
|
|
|
195
|
|
|
if (! Filesystem::has($api_delivery_registry_token_dir_path)) Filesystem::createDir($api_delivery_registry_token_dir_path); |
|
196
|
|
|
|
|
197
|
|
|
Filesystem::write( |
|
198
|
|
|
$api_delivery_registry_token_file_path, |
|
199
|
|
|
$this->parser->encode([ |
|
200
|
|
|
'title' => 'Default', |
|
201
|
|
|
'icon' => 'fas fa-archive', |
|
202
|
|
|
'limit_calls' => (int) 0, |
|
203
|
|
|
'calls' => (int) 0, |
|
204
|
|
|
'state' => 'enabled', |
|
205
|
|
|
'uuid' => $uuid, |
|
206
|
|
|
'created_by' => $uuid, |
|
207
|
|
|
'created_at' => $time, |
|
208
|
|
|
'updated_by' => $uuid, |
|
209
|
|
|
'updated_at' => $time, |
|
210
|
|
|
], 'yaml') |
|
211
|
|
|
); |
|
212
|
|
|
|
|
213
|
|
|
// Set Default API's tokens |
|
214
|
|
|
$custom_flextype_settings_file_path = PATH['site'] . '/config/' . '/settings.yaml'; |
|
215
|
|
|
$custom_flextype_settings_file_data = $this->parser->decode(Filesystem::read($custom_flextype_settings_file_path), 'yaml'); |
|
216
|
|
|
|
|
217
|
|
|
$custom_flextype_settings_file_data['api']['delivery']['images']['default_token'] = $api_delivery_images_token; |
|
218
|
|
|
$custom_flextype_settings_file_data['api']['delivery']['entries']['default_token'] = $api_delivery_entries_token; |
|
219
|
|
|
$custom_flextype_settings_file_data['api']['delivery']['registry']['default_token'] = $api_delivery_registry_token; |
|
220
|
|
|
|
|
221
|
|
|
Filesystem::write($custom_flextype_settings_file_path, $this->parser->encode($custom_flextype_settings_file_data, 'yaml')); |
|
222
|
|
|
|
|
223
|
|
|
// Create uploads dir for default entries |
|
224
|
|
|
if (! Filesystem::has(PATH['site'] . '/uploads/entries/home/')) { |
|
225
|
|
|
Filesystem::createDir(PATH['site'] . '/uploads/entries/home/'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $response->withRedirect($this->router->pathFor('admin.users.login')); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
return $response->withRedirect($this->router->pathFor('admin.users.installation')); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $response->withRedirect($this->router->pathFor('admin.users.installation')); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Logout page process |
|
239
|
|
|
* |
|
240
|
|
|
* @param Request $request PSR7 request |
|
241
|
|
|
* @param Response $response PSR7 response |
|
242
|
|
|
*/ |
|
243
|
|
|
public function logoutProcess(Request $request, Response $response) : Response |
|
244
|
|
|
{ |
|
245
|
|
|
Session::destroy(); |
|
246
|
|
|
|
|
247
|
|
|
return $response->withRedirect($this->router->pathFor('admin.users.login')); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get Users list |
|
252
|
|
|
* |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getUsersList() : array |
|
256
|
|
|
{ |
|
257
|
|
|
// Get Users Profiles |
|
258
|
|
|
$users_list = Filesystem::listContents(PATH['site'] . '/accounts'); |
|
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
// Users |
|
261
|
|
|
$users = []; |
|
262
|
|
|
|
|
263
|
|
|
foreach ($users_list as $user) { |
|
264
|
|
|
if ($user['type'] === 'dir' && Filesystem::has($user['path'] . '/profile.yaml')) { |
|
265
|
|
|
$users[$user['dirname']] = $user; |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return $users; |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths