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