1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Authentication\Controller; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\ORMException; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface; |
14
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
15
|
|
|
use SlayerBirden\DataFlowServer\Notification\SuccessMessage; |
16
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
17
|
|
|
use Zend\Hydrator\ExtractionInterface; |
18
|
|
|
|
19
|
|
|
class InvalidateTokenAction implements MiddlewareInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EntityManager |
23
|
|
|
*/ |
24
|
|
|
private $entityManager; |
25
|
|
|
/** |
26
|
|
|
* @var LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
/** |
30
|
|
|
* @var ExtractionInterface |
31
|
|
|
*/ |
32
|
|
|
private $extraction; |
33
|
|
|
|
34
|
|
|
public function __construct(EntityManager $entityManager, LoggerInterface $logger, ExtractionInterface $extraction) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$this->entityManager = $entityManager; |
37
|
|
|
$this->logger = $logger; |
38
|
|
|
$this->extraction = $extraction; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
45
|
|
|
{ |
46
|
|
|
$token = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE); |
47
|
|
|
try { |
48
|
|
|
$token->setActive(false); |
49
|
|
|
|
50
|
|
|
$this->entityManager->persist($token); |
51
|
|
|
$this->entityManager->flush(); |
52
|
|
|
return new JsonResponse([ |
53
|
|
|
'data' => [ |
54
|
|
|
'token' => $this->extraction->extract($token), |
55
|
|
|
], |
56
|
|
|
'success' => true, |
57
|
|
|
'msg' => new SuccessMessage('Token invalidated.'), |
58
|
|
|
], 200); |
59
|
|
|
} catch (ORMException $exception) { |
60
|
|
|
$this->logger->error((string)$exception); |
61
|
|
|
return new JsonResponse([ |
62
|
|
|
'data' => [], |
63
|
|
|
'success' => false, |
64
|
|
|
'msg' => new DangerMessage('There was an error while invalidating token.'), |
65
|
|
|
], 400); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.