From b946bf7ef37cc3a52e78b57cef89d764800e736b Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 18 Feb 2020 10:21:15 +0300 Subject: [PATCH] feat(core): add ability to disable/enable APIs globally #349 --- flextype/api/delivery/entries.php | 44 ++++++++++++++++++------------- flextype/api/delivery/images.php | 42 ++++++++++++++++------------- 2 files changed, 49 insertions(+), 37 deletions(-) diff --git a/flextype/api/delivery/entries.php b/flextype/api/delivery/entries.php index 36f4ac20..29f638bd 100644 --- a/flextype/api/delivery/entries.php +++ b/flextype/api/delivery/entries.php @@ -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); } diff --git a/flextype/api/delivery/images.php b/flextype/api/delivery/images.php index 7085bd11..d2866403 100644 --- a/flextype/api/delivery/images.php +++ b/flextype/api/delivery/images.php @@ -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); });