From 87348c83356bebafadef3deaa4043c841919f3ff Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 23 Aug 2017 19:32:56 +0200 Subject: [PATCH] Implementation of Api action "delete" --- app/bootstrap.php.d/60-api.php | 2 +- app/config/routing.yml | 4 ++ src/Gist/Api/Client.php | 67 ++++++++++++++++++--------- src/Gist/Controller/ApiController.php | 61 +++++++++++++++++++++++- src/Gist/Service/UserProvider.php | 14 ++++++ 5 files changed, 124 insertions(+), 24 deletions(-) diff --git a/app/bootstrap.php.d/60-api.php b/app/bootstrap.php.d/60-api.php index ba94758..d630058 100644 --- a/app/bootstrap.php.d/60-api.php +++ b/app/bootstrap.php.d/60-api.php @@ -6,7 +6,7 @@ $app['api_client'] = $app->share(function ($app) { $client = new Client(['base_uri' => rtrim($app['settings']['api']['base_url'], '/')]); if (!empty($app['settings']['api']['client']['api_key'])) { - $client->setApiToken($app['settings']['api']['client']['api_key']); + $client->setApiKey($app['settings']['api']['client']['api_key']); } return $client; diff --git a/app/config/routing.yml b/app/config/routing.yml index 2deef13..49ad3f4 100644 --- a/app/config/routing.yml +++ b/app/config/routing.yml @@ -68,3 +68,7 @@ api_create: api_update: path: /api/update/{gist}/{apiKey} defaults: {_controller: Gist\Controller\ApiController::updateAction, _locale: en, apiKey: null} + +api_delete: + path: /api/delete/{gist}/{apiKey} + defaults: {_controller: Gist\Controller\ApiController::deleteAction, _locale: en, apiKey: null} diff --git a/src/Gist/Api/Client.php b/src/Gist/Api/Client.php index 548839f..82ed6cd 100644 --- a/src/Gist/Api/Client.php +++ b/src/Gist/Api/Client.php @@ -25,6 +25,13 @@ class Client extends BaseClient */ const UPDATE = '/en/api/update/{gist}'; + /** + * URI of delete. + * + * @const string + */ + const DELETE = '/en/api/delete/{gist}'; + /** * URI of list. * @@ -33,17 +40,17 @@ class Client extends BaseClient const LIST = '/en/api/list'; /** - * The API token. + * The API key. * * @var string|null */ - protected $apiToken; + protected $apiKey; /** * Creates a gist. * - * @param string $title The title - * @param string $type The type + * @param string $title The title + * @param string $type The type * @param string $content The content * * @return array @@ -51,7 +58,7 @@ class Client extends BaseClient public function create($title, $type, $content) { $response = $this->post( - $this->mergeToken(self::CREATE), + $this->mergeApiKey(self::CREATE), array( 'form_params' => array( 'form' => array( @@ -71,9 +78,9 @@ class Client extends BaseClient } /** - * Clones and update a gist + * Clones and update a gist. * - * @param string $gist Gist's ID + * @param string $gist Gist's ID * @param string $content The content * * @return array @@ -81,7 +88,7 @@ class Client extends BaseClient public function update($gist, $content) { $response = $this->post( - str_replace('{gist}', $gist, $this->mergeToken(self::LIST)), + str_replace('{gist}', $gist, $this->mergeApiKey(self::LIST)), array( 'form_params' => array( 'form' => array( @@ -99,16 +106,34 @@ class Client extends BaseClient } /** - * Lists the user's gists. + * Deletes a gist. * * @param string $gist Gist's ID + * + * @return array + */ + public function delete($gist) + { + $response = $this->post(str_replace('{gist}', $gist, $this->mergeApiKey(self::DELETE))); + + if ($response->getStatusCode() === 200) { + return json_decode($response->getBody()->getContents(), true); + } + + return []; + } + + /** + * Lists the user's gists. + * + * @param string $gist Gist's ID * @param string $content The content * * @return array */ public function list() { - $response = $this->get($this->mergeToken(self::LIST)); + $response = $this->get($this->mergeApiKey(self::LIST)); if ($response->getStatusCode() === 200) { return json_decode($response->getBody()->getContents(), true); @@ -118,42 +143,42 @@ class Client extends BaseClient } /* - * Merges the API token with the given url.. + * Merges the API key with the given url. * * @param string $url * * @return string */ - public function mergeToken($url) + public function mergeApiKey($url) { - if (empty($this->apiToken)) { + if (empty($this->apiKey)) { return $url; } - return rtrim($url, '/').'/'.$this->apiToken; + return rtrim($url, '/').'/'.$this->apiKey; } /* - * Set the value of "apiToken". + * Set the value of "apiKey". * - * @param string|null $apiToken + * @param string|null $apiKey * * @return Client */ - public function setApiToken($apiToken) + public function setApiKey($apiKey) { - $this->apiToken = $apiToken; + $this->apiKey = $apiKey; return $this; } /* - * Get the value of "apiToken". + * Get the value of "apiKey". * * @return string|null */ - public function getApiToken() + public function getApiKey() { - return $this->apiToken; + return $this->apiKey; } } diff --git a/src/Gist/Controller/ApiController.php b/src/Gist/Controller/ApiController.php index f5fe881..3013bab 100644 --- a/src/Gist/Controller/ApiController.php +++ b/src/Gist/Controller/ApiController.php @@ -42,7 +42,8 @@ class ApiController extends Controller return $this->invalidMethodResponse('GET method is required.'); } - $gists = GistQuery::create()->find(); + $user = $app['user.provider']->loadUserByApiKey($apiKey); + $gists = $user->getGists(); $data = array(); foreach ($gists as $gist) { @@ -102,8 +103,12 @@ class ApiController extends Controller $form->submit($request); if ($form->isValid()) { + $user = !empty($apiKey) ? $app['user.provider']->loadUserByApiKey($apiKey) : null; $gist = $app['gist']->create(new Gist(), $form->getData()); - $gist->setCipher(false)->save(); + $gist + ->setCipher(false) + ->setUser($user) + ->save(); $history = $app['gist']->getHistory($gist); @@ -189,6 +194,49 @@ class ApiController extends Controller return $this->invalidRequestResponse('Invalid field(s)'); } + /** + * Deletes a gist. + * + * @param Request $request + * @param string $gist + * @param string $apiKey + * + * @return JsonResponse + */ + public function deleteAction(Request $request, $gist, $apiKey) + { + $app = $this->getApp(); + + if (false === $app['settings']['api']['enabled']) { + return new Response('', 403); + } + + if (false === $this->isValidApiKey($apiKey, true)) { + return $this->invalidApiKeyResponse(); + } + + if (false === $request->isMethod('post')) { + // return $this->invalidMethodResponse('POST method is required.'); + } + + $user = $app['user.provider']->loadUserByApiKey($apiKey); + + $gist = GistQuery::create() + ->filterById((int) $gist) + ->_or() + ->filterByFile($gist) + ->filterByUser($user) + ->findOne(); + + if (!$gist) { + return $this->invalidRequestResponse('Invalid Gist'); + } + + $gist->delete(); + + return new JsonResponse(['error' => false]); + } + /** * Builds an invalid api key response. * @@ -240,6 +288,15 @@ class ApiController extends Controller return new JsonResponse($data, 400); } + /** + * Checks if the given api key is valid + * depending of the requirement. + * + * @param mixed $apiKey + * @param mixed $required + * + * @return bool + */ protected function isValidApiKey($apiKey, $required = false) { if (empty($apiKey)) { diff --git a/src/Gist/Service/UserProvider.php b/src/Gist/Service/UserProvider.php index b0f1813..e887b50 100644 --- a/src/Gist/Service/UserProvider.php +++ b/src/Gist/Service/UserProvider.php @@ -167,6 +167,20 @@ class UserProvider implements UserProviderInterface return $user; } + /** + * Loads a user by his api key. + * + * @param string $apiKey + * + * @return User + */ + public function loadUserByApiKey($apiKey) + { + $user = UserQuery::create()->findOneByApiKey($apiKey); + + return $user; + } + /* * Checks if the given password is the current user password. *