-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters