Implementation of Api action "delete"
This commit is contained in:
parent
f6e4837776
commit
87348c8335
|
@ -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;
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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,11 +40,11 @@ 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.
|
||||
|
@ -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,7 +78,7 @@ class Client extends BaseClient
|
|||
}
|
||||
|
||||
/**
|
||||
* Clones and update a gist
|
||||
* Clones and update a gist.
|
||||
*
|
||||
* @param string $gist Gist's ID
|
||||
* @param string $content The content
|
||||
|
@ -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(
|
||||
|
@ -98,6 +105,24 @@ class Client extends BaseClient
|
|||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
@ -108,7 +133,7 @@ class Client extends BaseClient
|
|||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue