Skip to content

Commit

Permalink
Add endpoint DELETE.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syndesi committed Aug 31, 2023
1 parent 3283145 commit 947321a
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions src/Controller/User/DeleteTokenController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Controller\User;

use App\Exception\ClientNotFoundException;
use App\Exception\ClientUnauthorizedException;
use App\Response\NoContentResponse;
use App\Security\AuthProvider;
use App\Service\ElementManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DeleteTokenController extends AbstractController
{
public function __construct(
private ElementManager $elementManager,
private AuthProvider $authProvider
) {
}

#[Route(
'/token',
name: 'delete-token',
methods: ['DELETE']
)]
public function deleteToken(): Response
{
$userUuid = $this->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();
}
}
61 changes: 61 additions & 0 deletions src/Controller/User/GetTokenController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Controller\User;

use App\Exception\ClientUnauthorizedException;
use App\Security\AuthProvider;
use App\Service\CollectionService;
use Laudis\Neo4j\Databags\Statement;
use Ramsey\Uuid\Rfc4122\UuidV4;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Syndesi\CypherEntityManager\Type\EntityManager as CypherEntityManager;

class GetTokenController extends AbstractController
{
public function __construct(
private CypherEntityManager $cypherEntityManager,
private AuthProvider $authProvider,
private CollectionService $collectionService
) {
}

#[Route(
'/token',
name: 'get-token',
methods: ['GET']
)]
public function getToken(): Response
{
$userUuid = $this->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));
}
}
2 changes: 2 additions & 0 deletions test-feature-prepare
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 947321a

Please sign in to comment.