diff --git a/site/plugins/admin/app/Controllers/ApiDeliveryController.php b/site/plugins/admin/app/Controllers/ApiDeliveryController.php index 4929cd6f..54230e90 100644 --- a/site/plugins/admin/app/Controllers/ApiDeliveryController.php +++ b/site/plugins/admin/app/Controllers/ApiDeliveryController.php @@ -30,7 +30,7 @@ class ApiDeliveryController extends Controller 'plugins/admin/templates/system/api/delivery/index.html', [ 'menu_item' => 'api', - 'api_list' => ['entries' => 'Entries', 'images' => 'Images'], + 'api_list' => ['entries' => 'Entries', 'images' => 'Images', 'registry' => 'Registry'], 'links' => [ 'api' => [ 'link' => $this->router->pathFor('admin.api.index'), diff --git a/site/plugins/admin/app/Controllers/ApiDeliveryRegistryController.php b/site/plugins/admin/app/Controllers/ApiDeliveryRegistryController.php new file mode 100644 index 00000000..8e430138 --- /dev/null +++ b/site/plugins/admin/app/Controllers/ApiDeliveryRegistryController.php @@ -0,0 +1,266 @@ + 0) { + foreach ($tokens_list as $token) { + if ($token['type'] == 'dir' && Filesystem::has(PATH['tokens'] . '/delivery/registry/' . $token['dirname'] . '/token.yaml')) { + $tokens[] = $token; + } + } + } + + return $this->view->render( + $response, + 'plugins/admin/templates/system/api/delivery/registry/index.html', + [ + 'menu_item' => 'api', + 'tokens' => $tokens, + 'links' => [ + 'api' => [ + 'link' => $this->router->pathFor('admin.api.index'), + 'title' => __('admin_api'), + ], + 'api_delivery' => [ + 'link' => $this->router->pathFor('admin.api_delivery.index'), + 'title' => __('admin_delivery') + ], + 'api_delivery_registry' => [ + 'link' => $this->router->pathFor('admin.api_delivery_registry.index'), + 'title' => __('admin_registry'), + 'active' => true + ], + ], + 'buttons' => [ + 'api_delivery_registry_add' => [ + 'link' => $this->router->pathFor('admin.api_delivery_registry.add'), + 'title' => __('admin_create_new_token') + ], + ], + ] + ); + } + + /** + * Add token page + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function add(Request $request, Response $response) : Response + { + return $this->view->render( + $response, + 'plugins/admin/templates/system/api/delivery/registry/add.html', + [ + 'menu_item' => 'api', + 'links' => [ + 'api' => [ + 'link' => $this->router->pathFor('admin.api.index'), + 'title' => __('admin_api'), + ], + 'api_delivery' => [ + 'link' => $this->router->pathFor('admin.api_delivery.index'), + 'title' => __('admin_delivery') + ], + 'api_delivery_registry' => [ + 'link' => $this->router->pathFor('admin.api_delivery_registry.index'), + 'title' => __('admin_registry') + ], + 'api_delivery_registry_add' => [ + 'link' => $this->router->pathFor('admin.api_delivery_registry.add'), + 'title' => __('admin_create_new_token'), + 'active' => true + ], + ], + ] + ); + } + + /** + * Add new token - process + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function addProcess(Request $request, Response $response) : Response + { + // Get POST data + $post_data = $request->getParsedBody(); + + // Generate API token + $api_token = bin2hex(random_bytes(16)); + + $api_token_dir_path = PATH['tokens'] . '/delivery/registry/' . $api_token; + $api_token_file_path = $api_token_dir_path . '/token.yaml'; + + if (! Filesystem::has($api_token_file_path)) { + + Filesystem::createDir($api_token_dir_path); + + // Generate UUID + $uuid = Uuid::uuid4()->toString(); + + // Get time + $time = date($this->registry->get('flextype.date_format'), time()); + + // Create API Token account + if (Filesystem::write( + $api_token_file_path, + $this->parser->encode([ + 'title' => $post_data['title'], + 'icon' => $post_data['icon'], + 'limit_calls' => (int) $post_data['limit_calls'], + 'calls' => (int) 0, + 'state' => $post_data['state'], + 'uuid' => $uuid, + 'created_by' => Session::get('uuid'), + 'created_at' => $time, + 'updated_by' => Session::get('uuid'), + 'updated_at' => $time, + ], 'yaml') + )) { + $this->flash->addMessage('success', __('admin_message_delivery_registry_api_token_created')); + } else { + $this->flash->addMessage('error', __('admin_message_delivery_registry_api_token_was_not_created1')); + } + } else { + $this->flash->addMessage('error', __('admin_message_delivery_registry_api_token_was_not_created2')); + } + + if (isset($post_data['create-and-edit'])) { + return $response->withRedirect($this->router->pathFor('admin.api_delivery_registry.edit') . '?token=' . $api_token); + } else { + return $response->withRedirect($this->router->pathFor('admin.api_delivery_registry.index')); + } + } + + /** + * Edit token page + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function edit(Request $request, Response $response) : Response + { + $token = $request->getQueryParams()['token']; + $token_data = $this->parser->decode(Filesystem::read(PATH['tokens'] . '/delivery/registry/' . $token . '/token.yaml'), 'yaml'); + + return $this->view->render( + $response, + 'plugins/admin/templates/system/api/delivery/registry/edit.html', + [ + 'menu_item' => 'api', + 'token' => $token, + 'token_data' => $token_data, + 'links' => [ + 'api' => [ + 'link' => $this->router->pathFor('admin.api.index'), + 'title' => __('admin_api') + ], + 'api_tokens' => [ + 'link' => $this->router->pathFor('admin.api_delivery.index'), + 'title' => __('admin_delivery') + ], + 'api_delivery_registry' => [ + 'link' => $this->router->pathFor('admin.api_delivery_registry.index'), + 'title' => __('admin_registry') + ], + 'api_tokens_edit' => [ + 'link' => $this->router->pathFor('admin.api_delivery_registry.edit'), + 'title' => __('admin_edit_delivery_token'), + 'active' => true + ], + ] + ] + ); + } + + /** + * Edit token - process + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function editProcess(Request $request, Response $response) : Response + { + // Get POST data + $post_data = $request->getParsedBody(); + + $api_token_dir_path = PATH['tokens'] . '/delivery/registry/' . $post_data['token']; + $api_token_file_path = $api_token_dir_path . '/' . 'token.yaml'; + + // Update API Token File + if (Filesystem::has($api_token_file_path)) { + if (Filesystem::write( + $api_token_file_path, + $this->parser->encode([ + 'title' => $post_data['title'], + 'icon' => $post_data['icon'], + 'limit_calls' => (int) $post_data['limit_calls'], + 'calls' => (int) $post_data['calls'], + 'state' => $post_data['state'], + 'uuid' => $post_data['uuid'], + 'created_by' => $post_data['created_by'], + 'created_at' => $post_data['created_at'], + 'updated_by' => Session::get('uuid'), + 'updated_at' => date($this->registry->get('flextype.date_format'), time()), + ], 'yaml') + )) { + $this->flash->addMessage('success', __('admin_message_delivery_registry_api_token_updated')); + } + } else { + $this->flash->addMessage('error', __('admin_message_delivery_registry_api_token_was_not_updated')); + } + + return $response->withRedirect($this->router->pathFor('admin.api_delivery_registry.index')); + } + + /** + * Delete token - process + * + * @param Request $request PSR7 request + * @param Response $response PSR7 response + */ + public function deleteProcess(Request $request, Response $response) : Response + { + // Get POST data + $post_data = $request->getParsedBody(); + + $api_token_dir_path = PATH['tokens'] . '/delivery/registry/' . $post_data['token']; + + if (Filesystem::deleteDir($api_token_dir_path)) { + $this->flash->addMessage('success', __('admin_message_delivery_registry_api_token_deleted')); + } else { + $this->flash->addMessage('error', __('admin_message_delivery_registry_api_token_was_not_deleted')); + } + + return $response->withRedirect($this->router->pathFor('admin.api_delivery_registry.index')); + } +} diff --git a/site/plugins/admin/dependencies.php b/site/plugins/admin/dependencies.php index b41bb384..7cea5532 100644 --- a/site/plugins/admin/dependencies.php +++ b/site/plugins/admin/dependencies.php @@ -88,3 +88,7 @@ $flextype['ApiDeliveryEntriesController'] = static function ($container) { $flextype['ApiDeliveryImagesController'] = static function ($container) { return new ApiDeliveryImagesController($container); }; + +$flextype['ApiDeliveryRegistryController'] = static function ($container) { + return new ApiDeliveryRegistryController($container); +}; diff --git a/site/plugins/admin/lang/en_US.yaml b/site/plugins/admin/lang/en_US.yaml index 6295270e..4d961d5f 100755 --- a/site/plugins/admin/lang/en_US.yaml +++ b/site/plugins/admin/lang/en_US.yaml @@ -351,6 +351,14 @@ admin_message_delivery_entries_api_token_deleted: "Delivery token for entries de admin_message_delivery_entries_api_token_was_not_deleted: "Delivery token for entries was not deleted." admin_confirmation_required_for_delivery_entries_token_delete: "Are you sure you want to delete this delivery token for entries?" admin_you_have_not_created_any_delivery_images_api_tokens_yet: "You haven't created any Delivery Images tokens yet!" +admin_you_have_not_created_any_delivery_registry_api_tokens_yet: "You haven't created any Delivery Registry tokens yet!" +admin_message_delivery_registry_api_token_updated: "Delivery token for registry updated." +admin_message_delivery_registry_api_token_was_not_updated: "Delivery token for registry was not updated." +admin_message_delivery_registry_api_token_created: "Delivery token for registry created." +admin_message_delivery_registry_api_token_was_not_created: "Delivery token for registry was not created." +admin_message_delivery_registry_api_token_deleted: "Delivery token for registry deleted." +admin_message_delivery_registry_api_token_was_not_deleted: "Delivery token for registry was not deleted." +admin_confirmation_required_for_delivery_registry_token_delete: "Are you sure you want to delete this delivery token for registry?" admin_message_delivery_images_api_token_updated: "Delivery token for images updated." admin_message_delivery_images_api_token_was_not_updated: "Delivery token for images was not updated." admin_message_delivery_images_api_token_created: "Delivery token for images created." diff --git a/site/plugins/admin/routes/web.php b/site/plugins/admin/routes/web.php index d7ce03db..45a32445 100644 --- a/site/plugins/admin/routes/web.php +++ b/site/plugins/admin/routes/web.php @@ -116,5 +116,11 @@ $app->group('/' . $admin_route, function () use ($app) : void { $app->post('/api/delivery/images/edit', 'ApiDeliveryImagesController:editProcess')->setName('admin.api_delivery_images.editProcess'); $app->post('/api/delivery/images/delete', 'ApiDeliveryImagesController:deleteProcess')->setName('admin.api_delivery_images.deleteProcess'); + $app->get('/api/delivery/registry', 'ApiDeliveryRegistryController:index')->setName('admin.api_delivery_registry.index'); + $app->get('/api/delivery/registry/add', 'ApiDeliveryRegistryController:add')->setName('admin.api_delivery_registry.add'); + $app->post('/api/delivery/registry/add', 'ApiDeliveryRegistryController:addProcess')->setName('admin.api_delivery_registry.addProcess'); + $app->get('/api/delivery/registry/edit', 'ApiDeliveryRegistryController:edit')->setName('admin.api_delivery_registry.edit'); + $app->post('/api/delivery/registry/edit', 'ApiDeliveryRegistryController:editProcess')->setName('admin.api_delivery_registry.editProcess'); + $app->post('/api/delivery/registry/delete', 'ApiDeliveryRegistryController:deleteProcess')->setName('admin.api_delivery_registry.deleteProcess'); })->add(new AuthMiddleware($flextype)); diff --git a/site/plugins/admin/templates/system/api/delivery/index.html b/site/plugins/admin/templates/system/api/delivery/index.html index dbcdf291..8a3c3413 100644 --- a/site/plugins/admin/templates/system/api/delivery/index.html +++ b/site/plugins/admin/templates/system/api/delivery/index.html @@ -21,6 +21,9 @@ {% if key == 'images' %} {{ icon('far fa-images') }} {% endif %} + {% if key == 'registry' %} + {{ icon('fas fa-archive') }} + {% endif %}
+ | + {{ tr('admin_title') }} + | ++ {{ tr('admin_token') }} + | ++ {{ tr('admin_calls') }} + | +|
---|---|---|---|---|
+ {{ icon(token_data.icon) }} + | ++ {{ token_data.title }} + | +{{ token.dirname }} | +{{ token_data.calls }} + / + {% if (token_data.limit_calls > 0) %}{{ token_data.limit_calls }} + {% else %}∞{% endif %} + | ++ + + | +