|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Command/ApiKey/ApiKeyHelper.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Command\ApiKey; |
|
10
|
|
|
|
|
11
|
|
|
use App\Entity\ApiKey; |
|
12
|
|
|
use App\Resource\ApiKeyResource; |
|
13
|
|
|
use App\Security\RolesService; |
|
14
|
|
|
use Closure; |
|
15
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
16
|
|
|
use Throwable; |
|
17
|
|
|
use function array_map; |
|
18
|
|
|
use function implode; |
|
19
|
|
|
use function sprintf; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class ApiKeyHelper |
|
23
|
|
|
* |
|
24
|
|
|
* @package App\Command\ApiKey |
|
25
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class ApiKeyHelper |
|
28
|
|
|
{ |
|
29
|
|
|
public function __construct( |
|
30
|
|
|
private readonly ApiKeyResource $apiKeyResource, |
|
31
|
|
|
private readonly RolesService $rolesService, |
|
32
|
|
|
) { |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Method to get API key entity. Also note that this may return a null in |
|
37
|
|
|
* cases that user do not want to make any changes to API keys. |
|
38
|
|
|
* |
|
39
|
|
|
* @throws Throwable |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getApiKey(SymfonyStyle $io, string $question): ?ApiKey |
|
42
|
|
|
{ |
|
43
|
|
|
$found = false; |
|
44
|
|
|
$apiKey = null; |
|
45
|
|
|
|
|
46
|
|
|
while ($found !== true) { |
|
47
|
|
|
$apiKey = $this->getApiKeyEntity($io, $question); |
|
48
|
|
|
|
|
49
|
|
|
if (!$apiKey instanceof ApiKey) { |
|
50
|
|
|
break; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$message = sprintf( |
|
54
|
|
|
'Is this the correct API key \'[%s] [%s] %s\'?', |
|
55
|
|
|
$apiKey->getId(), |
|
56
|
|
|
$apiKey->getToken(), |
|
57
|
|
|
$apiKey->getDescription(), |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$found = $io->confirm($message, false); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $apiKey ?? null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Helper method to get "normalized" message for API key. This is used on |
|
68
|
|
|
* following cases: |
|
69
|
|
|
* - User changes API key token |
|
70
|
|
|
* - User creates new API key |
|
71
|
|
|
* - User modifies API key |
|
72
|
|
|
* - User removes API key |
|
73
|
|
|
* |
|
74
|
|
|
* @return array<int, string> |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getApiKeyMessage(string $message, ApiKey $apiKey): array |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
|
|
$message, |
|
80
|
|
|
sprintf( |
|
81
|
|
|
"GUID: %s\nToken: %s", |
|
82
|
|
|
$apiKey->getId(), |
|
83
|
|
|
$apiKey->getToken(), |
|
84
|
|
|
), |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Method to list ApiKeys where user can select desired one. |
|
90
|
|
|
* |
|
91
|
|
|
* @throws Throwable |
|
92
|
|
|
*/ |
|
93
|
|
|
private function getApiKeyEntity(SymfonyStyle $io, string $question): ?ApiKey |
|
94
|
|
|
{ |
|
95
|
|
|
/** @var array<string, string> $choices */ |
|
96
|
|
|
$choices = []; |
|
97
|
|
|
|
|
98
|
|
|
foreach ($this->apiKeyResource->find() as $apiKey) { |
|
99
|
|
|
$choices[$apiKey->getId()] = sprintf( |
|
100
|
|
|
'[Token: %s] %s - Roles: %s', |
|
101
|
|
|
$apiKey->getToken(), |
|
102
|
|
|
$apiKey->getDescription(), |
|
103
|
|
|
implode(', ', $this->rolesService->getInheritedRoles($apiKey->getRoles())), |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$choices['Exit'] = 'Exit command'; |
|
108
|
|
|
|
|
109
|
|
|
return $this->apiKeyResource->findOne((string)$io->choice($question, $choices)); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|