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 bin2hex; |
13
|
|
|
use function date; |
14
|
|
|
use function Flextype\Component\I18n\__; |
|
|
|
|
15
|
|
|
use function random_bytes; |
16
|
|
|
use function time; |
17
|
|
|
|
18
|
|
|
class ApiAccessController extends Container |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Access Index page |
22
|
|
|
* |
23
|
|
|
* @param Request $request PSR7 request |
24
|
|
|
* @param Response $response PSR7 response |
25
|
|
|
*/ |
26
|
|
|
public function index(Request $request, Response $response) : Response |
27
|
|
|
{ |
28
|
|
|
$tokens = []; |
29
|
|
|
$tokens_list = Filesystem::listContents(PATH['project'] . '/tokens' . '/access/'); |
|
|
|
|
30
|
|
|
|
31
|
|
|
if (count($tokens_list) > 0) { |
32
|
|
|
foreach ($tokens_list as $token) { |
33
|
|
|
if ($token['type'] == 'dir' && Filesystem::has(PATH['project'] . '/tokens' . '/access/' . $token['dirname'] . '/token.yaml')) { |
34
|
|
|
$tokens[] = $token; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->twig->render( |
40
|
|
|
$response, |
41
|
|
|
'plugins/admin/templates/system/api/access/index.html', |
42
|
|
|
[ |
43
|
|
|
'menu_item' => 'api', |
44
|
|
|
'tokens' => $tokens, |
45
|
|
|
'links' => [ |
46
|
|
|
'api' => [ |
47
|
|
|
'link' => $this->router->pathFor('admin.api.index'), |
48
|
|
|
'title' => __('admin_api'), |
|
|
|
|
49
|
|
|
], |
50
|
|
|
'api_Access' => [ |
51
|
|
|
'link' => $this->router->pathFor('admin.api_access.index'), |
52
|
|
|
'title' => __('admin_access'), |
53
|
|
|
'active' => true |
54
|
|
|
], |
55
|
|
|
], |
56
|
|
|
'buttons' => [ |
57
|
|
|
'api_access_add' => [ |
58
|
|
|
'link' => $this->router->pathFor('admin.api_access.add'), |
59
|
|
|
'title' => __('admin_create_new_token') |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add token page |
68
|
|
|
* |
69
|
|
|
* @param Request $request PSR7 request |
70
|
|
|
* @param Response $response PSR7 response |
71
|
|
|
*/ |
72
|
|
|
public function add(Request $request, Response $response) : Response |
73
|
|
|
{ |
74
|
|
|
return $this->twig->render( |
75
|
|
|
$response, |
76
|
|
|
'plugins/admin/templates/system/api/access/add.html', |
77
|
|
|
[ |
78
|
|
|
'menu_item' => 'api', |
79
|
|
|
'links' => [ |
80
|
|
|
'api' => [ |
81
|
|
|
'link' => $this->router->pathFor('admin.api.index'), |
82
|
|
|
'title' => __('admin_api'), |
|
|
|
|
83
|
|
|
], |
84
|
|
|
'api_access' => [ |
85
|
|
|
'link' => $this->router->pathFor('admin.api_access.index'), |
86
|
|
|
'title' => __('admin_access') |
87
|
|
|
], |
88
|
|
|
'api_access_add' => [ |
89
|
|
|
'link' => $this->router->pathFor('admin.api_access.add'), |
90
|
|
|
'title' => __('admin_create_new_token'), |
91
|
|
|
'active' => true |
92
|
|
|
], |
93
|
|
|
], |
94
|
|
|
] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add new token - process |
100
|
|
|
* |
101
|
|
|
* @param Request $request PSR7 request |
102
|
|
|
* @param Response $response PSR7 response |
103
|
|
|
*/ |
104
|
|
|
public function addProcess(Request $request, Response $response) : Response |
105
|
|
|
{ |
106
|
|
|
// Get POST data |
107
|
|
|
$post_data = $request->getParsedBody(); |
108
|
|
|
|
109
|
|
|
// Generate API token |
110
|
|
|
$api_token = bin2hex(random_bytes(16)); |
111
|
|
|
|
112
|
|
|
$api_token_dir_path = PATH['project'] . '/tokens' . '/access/' . $api_token; |
|
|
|
|
113
|
|
|
$api_token_file_path = $api_token_dir_path . '/token.yaml'; |
114
|
|
|
|
115
|
|
|
if (! Filesystem::has($api_token_file_path)) { |
116
|
|
|
|
117
|
|
|
Filesystem::createDir($api_token_dir_path); |
118
|
|
|
|
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 API Token account |
126
|
|
|
if (Filesystem::write( |
127
|
|
|
$api_token_file_path, |
128
|
|
|
$this->serializer->encode([ |
129
|
|
|
'title' => $post_data['title'], |
130
|
|
|
'icon' => $post_data['icon'], |
131
|
|
|
'limit_calls' => (int) $post_data['limit_calls'], |
132
|
|
|
'calls' => (int) 0, |
133
|
|
|
'state' => $post_data['state'], |
134
|
|
|
'uuid' => $uuid, |
135
|
|
|
'created_by' => Session::get('uuid'), |
136
|
|
|
'created_at' => $time, |
137
|
|
|
'updated_by' => Session::get('uuid'), |
138
|
|
|
'updated_at' => $time, |
139
|
|
|
], 'yaml') |
140
|
|
|
)) { |
141
|
|
|
$this->flash->addMessage('success', __('admin_message_access_api_token_created')); |
|
|
|
|
142
|
|
|
} else { |
143
|
|
|
$this->flash->addMessage('error', __('admin_message_access_api_token_was_not_created1')); |
144
|
|
|
} |
145
|
|
|
} else { |
146
|
|
|
$this->flash->addMessage('error', __('admin_message_access_api_token_was_not_created2')); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (isset($post_data['create-and-edit'])) { |
150
|
|
|
return $response->withRedirect($this->router->pathFor('admin.api_access.edit') . '?token=' . $api_token); |
151
|
|
|
} else { |
152
|
|
|
return $response->withRedirect($this->router->pathFor('admin.api_access.index')); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Edit token page |
158
|
|
|
* |
159
|
|
|
* @param Request $request PSR7 request |
160
|
|
|
* @param Response $response PSR7 response |
161
|
|
|
*/ |
162
|
|
|
public function edit(Request $request, Response $response) : Response |
163
|
|
|
{ |
164
|
|
|
$token = $request->getQueryParams()['token']; |
165
|
|
|
$token_data = $this->serializer->decode(Filesystem::read(PATH['project'] . '/tokens' . '/access/' . $token . '/token.yaml'), 'yaml'); |
|
|
|
|
166
|
|
|
|
167
|
|
|
return $this->twig->render( |
168
|
|
|
$response, |
169
|
|
|
'plugins/admin/templates/system/api/access/edit.html', |
170
|
|
|
[ |
171
|
|
|
'menu_item' => 'api', |
172
|
|
|
'token' => $token, |
173
|
|
|
'token_data' => $token_data, |
174
|
|
|
'links' => [ |
175
|
|
|
'api' => [ |
176
|
|
|
'link' => $this->router->pathFor('admin.api.index'), |
177
|
|
|
'title' => __('admin_api') |
|
|
|
|
178
|
|
|
], |
179
|
|
|
'api_access' => [ |
180
|
|
|
'link' => $this->router->pathFor('admin.api_access.index'), |
181
|
|
|
'title' => __('admin_access') |
182
|
|
|
], |
183
|
|
|
'api_tokens_edit' => [ |
184
|
|
|
'link' => $this->router->pathFor('admin.api_access.edit'), |
185
|
|
|
'title' => __('admin_edit_token'), |
186
|
|
|
'active' => true |
187
|
|
|
], |
188
|
|
|
] |
189
|
|
|
] |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Edit token - process |
195
|
|
|
* |
196
|
|
|
* @param Request $request PSR7 request |
197
|
|
|
* @param Response $response PSR7 response |
198
|
|
|
*/ |
199
|
|
|
public function editProcess(Request $request, Response $response) : Response |
200
|
|
|
{ |
201
|
|
|
// Get POST data |
202
|
|
|
$post_data = $request->getParsedBody(); |
203
|
|
|
|
204
|
|
|
$api_token_dir_path = PATH['project'] . '/tokens' . '/access/' . $post_data['token']; |
|
|
|
|
205
|
|
|
$api_token_file_path = $api_token_dir_path . '/' . 'token.yaml'; |
206
|
|
|
|
207
|
|
|
// Update API Token File |
208
|
|
|
if (Filesystem::has($api_token_file_path)) { |
209
|
|
|
if (Filesystem::write( |
210
|
|
|
$api_token_file_path, |
211
|
|
|
$this->serializer->encode([ |
212
|
|
|
'title' => $post_data['title'], |
213
|
|
|
'icon' => $post_data['icon'], |
214
|
|
|
'limit_calls' => (int) $post_data['limit_calls'], |
215
|
|
|
'calls' => (int) $post_data['calls'], |
216
|
|
|
'state' => $post_data['state'], |
217
|
|
|
'uuid' => $post_data['uuid'], |
218
|
|
|
'created_by' => $post_data['created_by'], |
219
|
|
|
'created_at' => $post_data['created_at'], |
220
|
|
|
'updated_by' => Session::get('uuid'), |
221
|
|
|
'updated_at' => date($this->registry->get('flextype.settings.date_format'), time()), |
222
|
|
|
], 'yaml') |
223
|
|
|
)) { |
224
|
|
|
$this->flash->addMessage('success', __('admin_message_access_api_token_updated')); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
} else { |
227
|
|
|
$this->flash->addMessage('error', __('admin_message_access_api_token_was_not_updated')); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $response->withRedirect($this->router->pathFor('admin.api_access.index')); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Delete token - process |
235
|
|
|
* |
236
|
|
|
* @param Request $request PSR7 request |
237
|
|
|
* @param Response $response PSR7 response |
238
|
|
|
*/ |
239
|
|
|
public function deleteProcess(Request $request, Response $response) : Response |
240
|
|
|
{ |
241
|
|
|
// Get POST data |
242
|
|
|
$post_data = $request->getParsedBody(); |
243
|
|
|
|
244
|
|
|
$api_token_dir_path = PATH['project'] . '/tokens' . '/access/' . $post_data['token']; |
|
|
|
|
245
|
|
|
|
246
|
|
|
if (Filesystem::deleteDir($api_token_dir_path)) { |
247
|
|
|
$this->flash->addMessage('success', __('admin_message_access_api_token_deleted')); |
|
|
|
|
248
|
|
|
} else { |
249
|
|
|
$this->flash->addMessage('error', __('admin_message_access_api_token_was_not_deleted')); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $response->withRedirect($this->router->pathFor('admin.api_access.index')); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
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