1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Authentication\Controller; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\Criteria; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\ORMException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Token; |
15
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
16
|
|
|
use SlayerBirden\DataFlowServer\Notification\SuccessMessage; |
17
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
18
|
|
|
use Zend\Hydrator\HydratorInterface; |
19
|
|
|
|
20
|
|
|
class InvalidateTokensAction implements MiddlewareInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var EntityManager |
24
|
|
|
*/ |
25
|
|
|
private $entityManager; |
26
|
|
|
/** |
27
|
|
|
* @var LoggerInterface |
28
|
|
|
*/ |
29
|
|
|
private $logger; |
30
|
|
|
/** |
31
|
|
|
* @var HydratorInterface |
32
|
|
|
*/ |
33
|
|
|
private $hydrator; |
34
|
|
|
|
35
|
3 |
|
public function __construct(EntityManager $entityManager, LoggerInterface $logger, HydratorInterface $hydrator) |
|
|
|
|
36
|
|
|
{ |
37
|
3 |
|
$this->entityManager = $entityManager; |
38
|
3 |
|
$this->logger = $logger; |
39
|
3 |
|
$this->hydrator = $hydrator; |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
* @throws ORMException |
45
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
46
|
|
|
*/ |
47
|
3 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
48
|
|
|
{ |
49
|
3 |
|
$data = $request->getParsedBody(); |
50
|
|
|
|
51
|
3 |
|
$users = $data['users'] ?? []; |
52
|
3 |
|
return $this->invalidate($users); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $users |
57
|
|
|
* @return ResponseInterface |
58
|
|
|
* @throws ORMException |
59
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
60
|
|
|
*/ |
61
|
3 |
|
private function invalidate(array $users = []): ResponseInterface |
62
|
|
|
{ |
63
|
3 |
|
$criteria = Criteria::create(); |
64
|
3 |
|
$criteria->where(Criteria::expr()->eq('active', true)); |
65
|
3 |
|
if (!empty($users)) { |
66
|
2 |
|
$criteria->andWhere(Criteria::expr()->in('owner', $this->getUsers($users))); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
$collection = $this->entityManager |
70
|
3 |
|
->getRepository(Token::class) |
71
|
3 |
|
->matching($criteria); |
72
|
|
|
|
73
|
3 |
|
if ($collection->count() === 0) { |
74
|
1 |
|
return new JsonResponse([ |
75
|
1 |
|
'data' => [ |
76
|
|
|
'count' => 0, |
77
|
|
|
'tokens' => [], |
78
|
|
|
], |
79
|
|
|
'success' => false, |
80
|
1 |
|
'msg' => new SuccessMessage('No tokens found to invalidate for given criteria.'), |
81
|
1 |
|
], 400); |
82
|
|
|
} |
83
|
2 |
|
foreach ($collection as $token) { |
84
|
2 |
|
$token->setActive(false); |
85
|
2 |
|
$this->entityManager->persist($token); |
86
|
|
|
} |
87
|
2 |
|
$this->entityManager->flush(); |
88
|
2 |
|
return new JsonResponse([ |
89
|
2 |
|
'data' => [ |
90
|
2 |
|
'count' => $collection->count(), |
91
|
2 |
|
'tokens' => array_map([$this->hydrator, 'extract'], $collection->toArray()), |
92
|
|
|
], |
93
|
|
|
'success' => true, |
94
|
2 |
|
'msg' => new SuccessMessage('Tokens have been deactivated.'), |
95
|
2 |
|
], 200); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
private function getUsers(array $users): array |
99
|
|
|
{ |
100
|
2 |
|
$collection = $this->entityManager |
101
|
2 |
|
->getRepository(User::class) |
102
|
2 |
|
->matching( |
103
|
2 |
|
Criteria::create()->where(Criteria::expr()->in('id', $users)) |
104
|
|
|
); |
105
|
2 |
|
return $collection->toArray(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.