1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-16 10:04:21 +02:00

feat(core): add ability to disable/enable APIs globally #349

This commit is contained in:
Awilum
2020-02-18 10:21:15 +03:00
parent d3793a430c
commit b946bf7ef3
2 changed files with 49 additions and 37 deletions

View File

@@ -35,32 +35,38 @@ $app->get('/api/delivery/entries', function (Request $request, Response $respons
$id = $query['id'];
$args = isset($query['args']) ? $query['args'] : null;
// Validate delivery token
if (validate_delivery_entries_token($request, $flextype)) {
if ($flextype['registry']->get('flextype.api.entries.enabled')) {
$delivery_entries_token_file_path = PATH['tokens'] . '/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml';
// Validate delivery token
if (validate_delivery_entries_token($request, $flextype)) {
// Set delivery token file
if ($delivery_entries_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_entries_token_file_path), 'yaml')) {
if ($delivery_entries_token_file_data['state'] == 'disabled' ||
($delivery_entries_token_file_data['limit_calls'] != 0 && $delivery_entries_token_file_data['calls'] >= $delivery_entries_token_file_data['limit_calls'])) {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
$delivery_entries_token_file_path = PATH['tokens'] . '/delivery/entries/' . $request->getQueryParams()['token'] . '/token.yaml';
// Set delivery token file
if ($delivery_entries_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_entries_token_file_path), 'yaml')) {
if ($delivery_entries_token_file_data['state'] == 'disabled' ||
($delivery_entries_token_file_data['limit_calls'] != 0 && $delivery_entries_token_file_data['calls'] >= $delivery_entries_token_file_data['limit_calls'])) {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
} else {
// Fetch entry
$data = $flextype['entries']->fetch($id, $args);
// Set response code
$response_code = (count($data) > 0) ? 200 : 404 ;
// Update calls counter
Filesystem::write($delivery_entries_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_entries_token_file_data, ['calls' => $delivery_entries_token_file_data['calls'] + 1]), 'yaml'));
// Return response
return $response->withJson($data, $response_code);
}
} else {
// Fetch entry
$data = $flextype['entries']->fetch($id, $args);
// Set response code
$response_code = (count($data) > 0) ? 200 : 404 ;
// Update calls counter
Filesystem::write($delivery_entries_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_entries_token_file_data, ['calls' => $delivery_entries_token_file_data['calls'] + 1]), 'yaml'));
// Return response
return $response->withJson($data, $response_code);
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
}
} else {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
}
} else {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
}

View File

@@ -31,34 +31,40 @@ $app->get('/api/delivery/images/{path:.+}', function (Request $request, Response
// Get Query Params
$query = $request->getQueryParams();
// Validate delivery image token
if (validate_delivery_images_token($request, $flextype)) {
if ($flextype['registry']->get('flextype.api.images.enabled')) {
$delivery_images_token_file_path = PATH['tokens'] . '/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml';
// Validate delivery image token
if (validate_delivery_images_token($request, $flextype)) {
// Set delivery token file
if ($delivery_images_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_images_token_file_path), 'yaml')) {
if ($delivery_images_token_file_data['state'] == 'disabled' ||
($delivery_images_token_file_data['limit_calls'] != 0 && $delivery_images_token_file_data['calls'] >= $delivery_images_token_file_data['limit_calls'])) {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
} else {
$delivery_images_token_file_path = PATH['tokens'] . '/delivery/images/' . $request->getQueryParams()['token'] . '/token.yaml';
// Update calls counter
Filesystem::write($delivery_images_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_images_token_file_data, ['calls' => $delivery_images_token_file_data['calls'] + 1]), 'yaml'));
if (Filesystem::has(PATH['entries'] . '/' . $args['path'])) {
return $flextype['images']->getImageResponse($args['path'], $_GET);
// Set delivery token file
if ($delivery_images_token_file_data = $flextype['parser']->decode(Filesystem::read($delivery_images_token_file_path), 'yaml')) {
if ($delivery_images_token_file_data['state'] == 'disabled' ||
($delivery_images_token_file_data['limit_calls'] != 0 && $delivery_images_token_file_data['calls'] >= $delivery_images_token_file_data['limit_calls'])) {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
} else {
return $response->withJson([], 404);
}
// Update calls counter
Filesystem::write($delivery_images_token_file_path, $flextype['parser']->encode(array_replace_recursive($delivery_images_token_file_data, ['calls' => $delivery_images_token_file_data['calls'] + 1]), 'yaml'));
if (Filesystem::has(PATH['entries'] . '/' . $args['path'])) {
return $flextype['images']->getImageResponse($args['path'], $_GET);
} else {
return $response->withJson([], 404);
}
}
} else {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
}
} else {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
}
return $response->withStatus(404);
} else {
return $response->withJson(["detail" => "Incorrect authentication credentials."], 401);
}
return $response->withStatus(404);
});