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