2018-04-20 23:09:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Grocy\Controllers;
|
|
|
|
|
2020-09-06 10:00:49 +02:00
|
|
|
use Grocy\Controllers\Users\User;
|
|
|
|
|
2018-04-20 23:09:18 +02:00
|
|
|
class OpenApiController extends BaseApiController
|
|
|
|
{
|
2020-08-31 20:40:31 +02:00
|
|
|
public function ApiKeysList(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
2018-04-21 19:18:00 +02:00
|
|
|
{
|
2020-09-06 10:00:49 +02:00
|
|
|
$apiKeys = $this->getDatabase()->api_keys();
|
2020-09-06 10:10:30 +02:00
|
|
|
if (!User::hasPermissions(User::PERMISSION_ADMIN))
|
|
|
|
{
|
2020-09-06 10:00:49 +02:00
|
|
|
$apiKeys = $apiKeys->where('user_id', GROCY_USER_ID);
|
2020-09-06 10:10:30 +02:00
|
|
|
}
|
2020-08-31 20:40:31 +02:00
|
|
|
return $this->renderPage($response, 'manageapikeys', [
|
2020-09-06 10:10:30 +02:00
|
|
|
'apiKeys' => $apiKeys,
|
2020-08-31 20:40:31 +02:00
|
|
|
'users' => $this->getDatabase()->users()
|
|
|
|
]);
|
2018-04-21 19:18:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 20:40:31 +02:00
|
|
|
public function CreateNewApiKey(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
2018-04-20 23:09:18 +02:00
|
|
|
{
|
2020-08-31 20:40:31 +02:00
|
|
|
$newApiKey = $this->getApiKeyService()->CreateApiKey();
|
|
|
|
$newApiKeyId = $this->getApiKeyService()->GetApiKeyId($newApiKey);
|
|
|
|
return $response->withRedirect($this->AppContainer->get('UrlManager')->ConstructUrl("/manageapikeys?CreatedApiKeyId=$newApiKeyId"));
|
2018-04-20 23:09:18 +02:00
|
|
|
}
|
|
|
|
|
2020-02-11 17:42:03 +01:00
|
|
|
public function DocumentationSpec(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
2018-04-20 23:09:18 +02:00
|
|
|
{
|
2020-03-01 23:47:47 +07:00
|
|
|
$applicationService = $this->getApplicationService();
|
2018-04-20 23:09:18 +02:00
|
|
|
|
2018-05-13 09:00:14 +02:00
|
|
|
$versionInfo = $applicationService->GetInstalledVersion();
|
2020-03-01 23:47:47 +07:00
|
|
|
$this->getOpenApiSpec()->info->version = $versionInfo->Version;
|
|
|
|
$this->getOpenApiSpec()->info->description = str_replace('PlaceHolderManageApiKeysUrl', $this->AppContainer->get('UrlManager')->ConstructUrl('/manageapikeys'), $this->getOpenApiSpec()->info->description);
|
|
|
|
$this->getOpenApiSpec()->servers[0]->url = $this->AppContainer->get('UrlManager')->ConstructUrl('/api');
|
2018-04-20 23:09:18 +02:00
|
|
|
|
2020-03-01 23:47:47 +07:00
|
|
|
return $this->ApiResponse($response, $this->getOpenApiSpec());
|
2018-04-20 23:09:18 +02:00
|
|
|
}
|
2018-04-21 19:18:00 +02:00
|
|
|
|
2020-08-31 20:40:31 +02:00
|
|
|
public function DocumentationUi(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args)
|
2018-04-21 19:18:00 +02:00
|
|
|
{
|
2020-08-31 20:40:31 +02:00
|
|
|
return $this->render($response, 'openapiui');
|
2018-04-21 19:18:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 20:40:31 +02:00
|
|
|
public function __construct(\DI\Container $container)
|
2018-04-21 19:18:00 +02:00
|
|
|
{
|
2020-08-31 20:40:31 +02:00
|
|
|
parent::__construct($container);
|
2018-04-21 19:18:00 +02:00
|
|
|
}
|
2018-04-20 23:09:18 +02:00
|
|
|
}
|