From 947321ac43a3085c9fecb80b2b40342673ef1fd0 Mon Sep 17 00:00:00 2001 From: Syndesi Date: Thu, 31 Aug 2023 15:42:29 +0200 Subject: [PATCH] Add endpoint DELETE. --- CHANGELOG.md | 1 + src/Controller/User/DeleteTokenController.php | 55 +++++++++++++++++ src/Controller/User/GetTokenController.php | 61 +++++++++++++++++++ test-feature-prepare | 2 + 4 files changed, 119 insertions(+) create mode 100644 src/Controller/User/DeleteTokenController.php create mode 100644 src/Controller/User/GetTokenController.php diff --git a/CHANGELOG.md b/CHANGELOG.md index a980a868..a8e2ceca 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Added - Add CI workflow to check for upstream Alpine updated. +- Add endpoint DELETE `/token`. ## 0.0.24 - 2023-08-21 ### Changed diff --git a/src/Controller/User/DeleteTokenController.php b/src/Controller/User/DeleteTokenController.php new file mode 100644 index 00000000..751b4648 --- /dev/null +++ b/src/Controller/User/DeleteTokenController.php @@ -0,0 +1,55 @@ +authProvider->getUserUuid(); + + if (!$userUuid) { + throw new ClientUnauthorizedException(); + } + + if ($this->authProvider->isAnonymous()) { + throw new ClientUnauthorizedException(); + } + + $tokenUuid = $this->authProvider->getTokenUuid(); + if (null === $tokenUuid) { + throw new \LogicException('Token must be provided.'); + } + + $element = $this->elementManager->getElement($tokenUuid); + if (null === $element) { + throw new ClientNotFoundException(); + } + $this->elementManager->delete($element); + $this->elementManager->flush(); + + // todo: remove cached token from redis + + return new NoContentResponse(); + } +} diff --git a/src/Controller/User/GetTokenController.php b/src/Controller/User/GetTokenController.php new file mode 100644 index 00000000..54bd236a --- /dev/null +++ b/src/Controller/User/GetTokenController.php @@ -0,0 +1,61 @@ +authProvider->getUserUuid(); + + if (!$userUuid) { + throw new ClientUnauthorizedException(); + } + + if ($this->authProvider->isAnonymous()) { + throw new ClientUnauthorizedException(); + } + + $cypherClient = $this->cypherEntityManager->getClient(); + $res = $cypherClient->runStatement(Statement::create( + "MATCH (user:User {id: \$userId})\n". + "MATCH (user)-[:OWNS]->(token:Token)\n". + "RETURN token.id\n". + "SKIP \$skip\n". + 'LIMIT $limit', + [ + 'userId' => $userUuid->toString(), + 'skip' => ($this->collectionService->getCurrentPage() - 1) * $this->collectionService->getPageSize(), + 'limit' => $this->collectionService->getPageSize(), + ] + )); + $tokenUuids = []; + foreach ($res as $resultSet) { + $tokenUuids[] = UuidV4::fromString($resultSet->get('token.id')); + } + + return $this->collectionService->buildCollectionFromUuids($tokenUuids, [], count($tokenUuids)); + } +} diff --git a/test-feature-prepare b/test-feature-prepare index 9197183e..6c5ab923 100644 --- a/test-feature-prepare +++ b/test-feature-prepare @@ -1,5 +1,7 @@ #!/bin/bash +set -e + REFERENCE_DATASET_VERSION=$(grep -E '^REFERENCE_DATASET_VERSION=' .env | cut -d '=' -f 2) php bin/console backup:fetch reference-dataset "https://github.com/ember-nexus/reference-dataset/archive/refs/tags/$REFERENCE_DATASET_VERSION.zip" --force