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 ;
2023-05-23 20:31:51 +02:00
use Grocy\Services\ApiKeyService ;
2023-05-13 14:43:51 +02:00
use Psr\Http\Message\ResponseInterface as Response ;
use Psr\Http\Message\ServerRequestInterface as Request ;
2020-09-06 10:00:49 +02:00
2018-04-20 23:09:18 +02:00
class OpenApiController extends BaseApiController
{
2023-05-13 14:43:51 +02:00
public function ApiKeysList ( Request $request , Response $response , array $args )
2018-04-21 19:18:00 +02:00
{
2023-05-23 20:31:51 +02:00
$selectedKeyId = - 1 ;
if ( isset ( $request -> getQueryParams ()[ 'key' ]) && filter_var ( $request -> getQueryParams ()[ 'key' ], FILTER_VALIDATE_INT ))
{
$selectedKeyId = $request -> getQueryParams ()[ 'key' ];
}
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
}
2023-05-23 20:31:51 +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 ,
2023-05-23 20:31:51 +02:00
'users' => $this -> getDatabase () -> users (),
'selectedKeyId' => $selectedKeyId
2020-08-31 20:40:31 +02:00
]);
2018-04-21 19:18:00 +02:00
}
2023-05-13 14:43:51 +02:00
public function CreateNewApiKey ( Request $request , Response $response , array $args )
2018-04-20 23:09:18 +02:00
{
2023-05-23 20:31:51 +02:00
$description = null ;
if ( isset ( $request -> getQueryParams ()[ 'description' ]))
{
$description = $request -> getQueryParams ()[ 'description' ];
}
$newApiKey = $this -> getApiKeyService () -> CreateApiKey ( ApiKeyService :: API_KEY_TYPE_DEFAULT , $description );
2020-08-31 20:40:31 +02:00
$newApiKeyId = $this -> getApiKeyService () -> GetApiKeyId ( $newApiKey );
2023-05-23 20:31:51 +02:00
return $response -> withRedirect ( $this -> AppContainer -> get ( 'UrlManager' ) -> ConstructUrl ( " /manageapikeys?key= $newApiKeyId " ));
2018-04-20 23:09:18 +02:00
}
2023-05-13 14:43:51 +02:00
public function DocumentationSpec ( Request $request , Response $response , array $args )
2018-04-20 23:09:18 +02:00
{
2020-11-16 22:18:37 +01:00
$spec = $this -> getOpenApiSpec ();
2018-04-20 23:09:18 +02:00
2020-11-16 22:18:37 +01:00
$applicationService = $this -> getApplicationService ();
2018-05-13 09:00:14 +02:00
$versionInfo = $applicationService -> GetInstalledVersion ();
2020-11-16 22:18:37 +01:00
$spec -> info -> version = $versionInfo -> Version ;
$spec -> info -> description = str_replace ( 'PlaceHolderManageApiKeysUrl' , $this -> AppContainer -> get ( 'UrlManager' ) -> ConstructUrl ( '/manageapikeys' ), $spec -> info -> description );
$spec -> servers [ 0 ] -> url = $this -> AppContainer -> get ( 'UrlManager' ) -> ConstructUrl ( '/api' );
2023-05-13 14:43:51 +02:00
$spec -> components -> schemas -> ExposedEntity_IncludingUserEntities = clone $spec -> components -> schemas -> StringEnumTemplate ;
;
2021-06-28 17:00:16 +02:00
foreach ( $this -> getUserfieldsService () -> GetEntities () as $userEntity )
{
2021-07-05 23:23:59 +02:00
array_push ( $spec -> components -> schemas -> ExposedEntity_IncludingUserEntities -> enum , $userEntity );
2021-06-28 17:00:16 +02:00
}
2022-01-06 14:17:02 +01:00
sort ( $spec -> components -> schemas -> ExposedEntity_IncludingUserEntities -> enum );
2021-06-28 17:00:16 +02:00
2021-07-05 23:23:59 +02:00
$spec -> components -> schemas -> ExposedEntity_NotIncludingNotEditable = clone $spec -> components -> schemas -> StringEnumTemplate ;
foreach ( $spec -> components -> schemas -> ExposedEntity -> enum as $value )
2020-11-16 22:18:37 +01:00
{
2021-07-05 23:23:59 +02:00
if ( ! in_array ( $value , $spec -> components -> schemas -> ExposedEntityNoEdit -> enum ))
2020-11-16 22:18:37 +01:00
{
2021-07-05 23:23:59 +02:00
array_push ( $spec -> components -> schemas -> ExposedEntity_NotIncludingNotEditable -> enum , $value );
2020-11-16 22:18:37 +01:00
}
}
2022-01-06 14:17:02 +01:00
sort ( $spec -> components -> schemas -> ExposedEntity_NotIncludingNotEditable -> enum );
2018-04-20 23:09:18 +02:00
2021-07-05 23:23:59 +02:00
$spec -> components -> schemas -> ExposedEntity_IncludingUserEntities_NotIncludingNotEditable = clone $spec -> components -> schemas -> StringEnumTemplate ;
foreach ( $spec -> components -> schemas -> ExposedEntity_IncludingUserEntities -> enum as $value )
2021-06-28 17:00:16 +02:00
{
2021-07-05 23:23:59 +02:00
if ( ! in_array ( $value , $spec -> components -> schemas -> ExposedEntityNoEdit -> enum ))
2021-06-28 17:00:16 +02:00
{
2021-07-05 23:23:59 +02:00
array_push ( $spec -> components -> schemas -> ExposedEntity_IncludingUserEntities_NotIncludingNotEditable -> enum , $value );
2021-06-28 17:00:16 +02:00
}
}
2022-08-14 23:09:27 +02:00
array_push ( $spec -> components -> schemas -> ExposedEntity_IncludingUserEntities_NotIncludingNotEditable -> enum , 'stock' ); // TODO: Don't hardcode this here - stock entries are normally not editable, but the corresponding Userfields are
2022-01-06 14:17:02 +01:00
sort ( $spec -> components -> schemas -> ExposedEntity_IncludingUserEntities_NotIncludingNotEditable -> enum );
2021-06-28 17:00:16 +02:00
2021-07-05 23:23:59 +02:00
$spec -> components -> schemas -> ExposedEntity_NotIncludingNotDeletable = clone $spec -> components -> schemas -> StringEnumTemplate ;
foreach ( $spec -> components -> schemas -> ExposedEntity -> enum as $value )
2020-12-21 16:20:12 +01:00
{
2021-07-05 23:23:59 +02:00
if ( ! in_array ( $value , $spec -> components -> schemas -> ExposedEntityNoDelete -> enum ))
2020-12-21 16:20:12 +01:00
{
2021-07-05 23:23:59 +02:00
array_push ( $spec -> components -> schemas -> ExposedEntity_NotIncludingNotDeletable -> enum , $value );
2020-12-21 16:20:12 +01:00
}
}
2022-01-06 14:17:02 +01:00
sort ( $spec -> components -> schemas -> ExposedEntity_NotIncludingNotDeletable -> enum );
2020-12-21 16:20:12 +01:00
2021-07-05 23:23:59 +02:00
$spec -> components -> schemas -> ExposedEntity_NotIncludingNotListable = clone $spec -> components -> schemas -> StringEnumTemplate ;
foreach ( $spec -> components -> schemas -> ExposedEntity -> enum as $value )
2020-12-12 10:10:21 +01:00
{
2021-07-05 23:23:59 +02:00
if ( ! in_array ( $value , $spec -> components -> schemas -> ExposedEntityNoListing -> enum ))
2020-12-12 10:10:21 +01:00
{
2021-07-05 23:23:59 +02:00
array_push ( $spec -> components -> schemas -> ExposedEntity_NotIncludingNotListable -> enum , $value );
2020-12-12 10:10:21 +01:00
}
}
2022-01-06 14:17:02 +01:00
sort ( $spec -> components -> schemas -> ExposedEntity_NotIncludingNotListable -> enum );
2020-12-12 10:10:21 +01:00
2020-11-16 22:18:37 +01:00
return $this -> ApiResponse ( $response , $spec );
2018-04-20 23:09:18 +02:00
}
2018-04-21 19:18:00 +02:00
2023-05-13 14:43:51 +02:00
public function DocumentationUi ( Request $request , Response $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
}
2018-04-20 23:09:18 +02:00
}