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