From 8b416aec6f8bae0bc8c484a3547826d73591de6f Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 18 Feb 2020 10:48:16 +0300 Subject: [PATCH] feat(admin-plugin): update controllers for apis' #349 --- .../admin/app/Controllers/ApiController.php | 272 ------------------ .../app/Controllers/ApiDeliveryController.php | 48 ++++ .../ApiDeliveryEntriesController.php | 262 +++++++++++++++++ 3 files changed, 310 insertions(+), 272 deletions(-) create mode 100644 site/plugins/admin/app/Controllers/ApiDeliveryController.php create mode 100644 site/plugins/admin/app/Controllers/ApiDeliveryEntriesController.php diff --git a/site/plugins/admin/app/Controllers/ApiController.php b/site/plugins/admin/app/Controllers/ApiController.php index 4f681ee7..f40f6349 100644 --- a/site/plugins/admin/app/Controllers/ApiController.php +++ b/site/plugins/admin/app/Controllers/ApiController.php @@ -41,276 +41,4 @@ class ApiController extends Controller ] ); } - - /** - * Delivery Index page - * - * @param Request $request PSR7 request - * @param Response $response PSR7 response - */ - public function deliveryIndex(Request $request, Response $response) : Response - { - return $this->view->render( - $response, - 'plugins/admin/templates/system/api/delivery/index.html', - [ - 'menu_item' => 'api', - 'api_list' => ['entries' => 'Entries', 'images' => 'Images'], - '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'), - 'active' => true, - ], - ], - ] - ); - } - - /** - * Delivery Entries Index page - * - * @param Request $request PSR7 request - * @param Response $response PSR7 response - */ - public function deliveryEntriesIndex(Request $request, Response $response) : Response - { - $tokens = []; - $tokens_list = Filesystem::listContents(PATH['tokens'] . '/delivery/entries/'); - - if (count($tokens_list) > 0) { - foreach ($tokens_list as $token) { - if ($token['type'] == 'dir' && Filesystem::has(PATH['tokens'] . '/delivery/entries/' . $token['dirname'] . '/token.yaml')) { - $tokens[] = $token; - } - } - } - - return $this->view->render( - $response, - 'plugins/admin/templates/system/api/delivery/entries/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_entries' => [ - 'link' => $this->router->pathFor('admin.api_delivery_entries.index'), - 'title' => __('admin_entries'), - 'active' => true - ], - ], - 'buttons' => [ - 'api_delivery_entries_add' => [ - 'link' => $this->router->pathFor('admin.api_delivery_entries.add'), - 'title' => __('admin_create_new_delivery_entries_token') - ], - ], - ] - ); - } - - /** - * Add token page - * - * @param Request $request PSR7 request - * @param Response $response PSR7 response - */ - public function deliveryEntriesAdd(Request $request, Response $response) : Response - { - return $this->view->render( - $response, - 'plugins/admin/templates/system/api/delivery/entries/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_entries' => [ - 'link' => $this->router->pathFor('admin.api_delivery_entries.index'), - 'title' => __('admin_entries') - ], - 'api_delivery_entries_add' => [ - 'link' => $this->router->pathFor('admin.api_delivery_entries.add'), - 'title' => __('admin_create_new_delivery_entries_token'), - 'active' => true - ], - ], - ] - ); - } - - /** - * Add new token - process - * - * @param Request $request PSR7 request - * @param Response $response PSR7 response - */ - public function deliveryEntriesAddProcess(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/entries/' . $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_delvery_entries_api_token_created')); - } else { - $this->flash->addMessage('error', __('admin_message_delvery_entries_api_token_was_not_created1')); - } - } else { - $this->flash->addMessage('error', __('admin_message_delvery_entries_api_token_was_not_created2')); - } - - if (isset($post_data['create-and-edit'])) { - return $response->withRedirect($this->router->pathFor('admin.api_delivery_entries.edit') . '?token=' . $api_token); - } else { - return $response->withRedirect($this->router->pathFor('admin.api_delivery_entries.index')); - } - } - - /** - * Edit token page - * - * @param Request $request PSR7 request - * @param Response $response PSR7 response - */ - public function deliveryEntriesEdit(Request $request, Response $response) : Response - { - $token = $request->getQueryParams()['token']; - $token_data = $this->parser->decode(Filesystem::read(PATH['tokens'] . '/delivery/entries/' . $token . '/token.yaml'), 'yaml'); - - return $this->view->render( - $response, - 'plugins/admin/templates/system/api/delivery/entries/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_entries.index'), - 'title' => __('admin_delivery') - ], - 'api_tokens_edit' => [ - 'link' => $this->router->pathFor('admin.api_delivery_entries.edit'), - 'title' => __('admin_edit_delivery_token'), - 'active' => true - ], - ] - ] - ); - } - - /** - * Edit token - process - * - * @param Request $request PSR7 request - * @param Response $response PSR7 response - */ - public function deliveryEntriesEditProcess(Request $request, Response $response) : Response - { - // Get POST data - $post_data = $request->getParsedBody(); - - $api_token_dir_path = PATH['tokens'] . '/' . $post_data['api'] . '/' . $post_data['api_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_' . $post_data['api'] . '_api_token_updated')); - } - } else { - $this->flash->addMessage('error', __('admin_message_' . $post_data['api'] . '_api_token_was_not_updated')); - } - - return $response->withRedirect($this->router->pathFor('admin.api_tokens.index') . '?api=' . $post_data['api']); - } - - /** - * Delete token - process - * - * @param Request $request PSR7 request - * @param Response $response PSR7 response - */ - public function deliveryEntriesDeleteProcess(Request $request, Response $response) : Response - { - // Get POST data - $post_data = $request->getParsedBody(); - - $api_token_dir_path = PATH['tokens'] . '/delivery/entries/' . $post_data['token']; - - if (Filesystem::deleteDir($api_token_dir_path)) { - $this->flash->addMessage('success', __('admin_message_delivery_entries_api_token_deleted')); - } else { - $this->flash->addMessage('error', __('admin_message_delivery_entries_api_token_was_not_deleted')); - } - - return $response->withRedirect($this->router->pathFor('admin.api_delivery_entries.index')); - } } diff --git a/site/plugins/admin/app/Controllers/ApiDeliveryController.php b/site/plugins/admin/app/Controllers/ApiDeliveryController.php new file mode 100644 index 00000000..4929cd6f --- /dev/null +++ b/site/plugins/admin/app/Controllers/ApiDeliveryController.php @@ -0,0 +1,48 @@ +view->render( + $response, + 'plugins/admin/templates/system/api/delivery/index.html', + [ + 'menu_item' => 'api', + 'api_list' => ['entries' => 'Entries', 'images' => 'Images'], + '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'), + 'active' => true, + ], + ], + ] + ); + } +} diff --git a/site/plugins/admin/app/Controllers/ApiDeliveryEntriesController.php b/site/plugins/admin/app/Controllers/ApiDeliveryEntriesController.php new file mode 100644 index 00000000..791e3ebe --- /dev/null +++ b/site/plugins/admin/app/Controllers/ApiDeliveryEntriesController.php @@ -0,0 +1,262 @@ + 0) { + foreach ($tokens_list as $token) { + if ($token['type'] == 'dir' && Filesystem::has(PATH['tokens'] . '/delivery/entries/' . $token['dirname'] . '/token.yaml')) { + $tokens[] = $token; + } + } + } + + return $this->view->render( + $response, + 'plugins/admin/templates/system/api/delivery/entries/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_entries' => [ + 'link' => $this->router->pathFor('admin.api_delivery_entries.index'), + 'title' => __('admin_entries'), + 'active' => true + ], + ], + 'buttons' => [ + 'api_delivery_entries_add' => [ + 'link' => $this->router->pathFor('admin.api_delivery_entries.add'), + 'title' => __('admin_create_new_delivery_entries_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/entries/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_entries' => [ + 'link' => $this->router->pathFor('admin.api_delivery_entries.index'), + 'title' => __('admin_entries') + ], + 'api_delivery_entries_add' => [ + 'link' => $this->router->pathFor('admin.api_delivery_entries.add'), + 'title' => __('admin_create_new_delivery_entries_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/entries/' . $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_delvery_entries_api_token_created')); + } else { + $this->flash->addMessage('error', __('admin_message_delvery_entries_api_token_was_not_created1')); + } + } else { + $this->flash->addMessage('error', __('admin_message_delvery_entries_api_token_was_not_created2')); + } + + if (isset($post_data['create-and-edit'])) { + return $response->withRedirect($this->router->pathFor('admin.api_delivery_entries.edit') . '?token=' . $api_token); + } else { + return $response->withRedirect($this->router->pathFor('admin.api_delivery_entries.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/entries/' . $token . '/token.yaml'), 'yaml'); + + return $this->view->render( + $response, + 'plugins/admin/templates/system/api/delivery/entries/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_entries.index'), + 'title' => __('admin_delivery') + ], + 'api_tokens_edit' => [ + 'link' => $this->router->pathFor('admin.api_delivery_entries.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'] . '/' . $post_data['api'] . '/' . $post_data['api_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_' . $post_data['api'] . '_api_token_updated')); + } + } else { + $this->flash->addMessage('error', __('admin_message_' . $post_data['api'] . '_api_token_was_not_updated')); + } + + return $response->withRedirect($this->router->pathFor('admin.api_tokens.index') . '?api=' . $post_data['api']); + } + + /** + * 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/entries/' . $post_data['token']; + + if (Filesystem::deleteDir($api_token_dir_path)) { + $this->flash->addMessage('success', __('admin_message_delivery_entries_api_token_deleted')); + } else { + $this->flash->addMessage('error', __('admin_message_delivery_entries_api_token_was_not_deleted')); + } + + return $response->withRedirect($this->router->pathFor('admin.api_delivery_entries.index')); + } +}