-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeycloakManager.php
More file actions
180 lines (154 loc) · 4.47 KB
/
Copy pathKeycloakManager.php
File metadata and controls
180 lines (154 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace ADT\FancyAdmin\Model\Security\Keycloak;
use ADT\DoctrineComponents\EntityManager;
use ADT\FancyAdmin\Model\Entities\Identity;
use ADT\FancyAdmin\Model\Entities\Sso;
use ADT\FancyAdmin\Model\FancyAdmin;
use ADT\FancyAdmin\Model\Queries\Factories\AclRoleQueryFactory;
use ADT\FancyAdmin\Model\Queries\Factories\IdentityQueryFactory;
use ADT\FancyAdmin\Model\Security\SecurityUser;
use Nette\Application\LinkGenerator;
use Nette\Caching\Storage;
use Nette\Http\Session;
class KeycloakManager
{
/** @var array<string, Keycloak> */
private array $instances = [];
private ?string $ssoClass = null;
public function __construct(
private readonly EntityManager $em,
private readonly LinkGenerator $linkGenerator,
private readonly SecurityUser $securityUser,
private readonly IdentityQueryFactory $identityQueryFactory,
private readonly AclRoleQueryFactory $aclRoleQueryFactory,
private readonly FancyAdmin $fancyAdmin,
private readonly Session $session,
private readonly Storage $storage,
) {}
/**
* Vrátí Keycloak instanci podle názvu. Vytvoří ji lazy z DB.
*/
public function getInstance(string $name): ?Keycloak
{
if (isset($this->instances[$name])) {
return $this->instances[$name];
}
$sso = $this->findSso($name);
if ($sso === null) {
return null;
}
return $this->createInstanceFromSso($sso);
}
/**
* Vrátí Keycloak instanci přiřazenou k dané identitě.
* Identita musí mít nastavenou SSO vazbu (identity.sso) a zároveň
* alespoň jedna její role musí mít needsSso = true.
*/
public function getInstanceForIdentity(Identity $identity): ?Keycloak
{
$sso = $identity->getSso();
if ($sso === null) {
return null;
}
// Zkontrolujeme, zda alespoň jedna role vyžaduje SSO
$needsSso = false;
foreach ($identity->getRoles() as $role) {
if ($role->getNeedsSso()) {
$needsSso = true;
break;
}
}
if (!$needsSso) {
return null;
}
return $this->getInstance($sso->getName());
}
/**
* Vrátí Keycloak instanci přiřazenou k danému Sso záznamu.
*/
public function getInstanceForSso(Sso $sso): ?Keycloak
{
return $this->getInstance($sso->getName());
}
/**
* Vrátí Keycloak instanci, přes kterou se aktuální uživatel přihlásil.
* Čte z session.
*/
public function getInstanceFromSession(): ?Keycloak
{
if (!$this->session->hasSection(KeycloakSessionSection::SECTION_NAME)) {
return null;
}
$keycloakSession = $this->session->getSection(KeycloakSessionSection::SECTION_NAME);
$instanceName = $keycloakSession->get(KeycloakSessionSection::SSO_INSTANCE_NAME);
if ($instanceName === null) {
return null;
}
return $this->getInstance($instanceName);
}
/**
* Uloží název instance do session (při přihlášení).
*/
public function storeInstanceInSession(string $name): void
{
$keycloakSession = $this->session->getSection(KeycloakSessionSection::SECTION_NAME);
$keycloakSession->set(KeycloakSessionSection::SSO_INSTANCE_NAME, $name);
}
public function hasInstances(): bool
{
return count($this->getAllSsoRecords()) > 0;
}
/**
* Vrátí názvy všech dostupných SSO instancí.
*
* @return string[]
*/
public function getInstanceNames(): array
{
return array_map(
fn (Sso $sso) => $sso->getName(),
$this->getAllSsoRecords()
);
}
/**
* @return Sso[]
*/
private function getAllSsoRecords(): array
{
return $this->em->getRepository($this->getSsoClass())->findAll();
}
private function findSso(string $name): ?Sso
{
return $this->em->getRepository($this->getSsoClass())->findOneBy(['name' => $name]);
}
private function getSsoClass(): string
{
if ($this->ssoClass === null) {
$this->ssoClass = $this->em->findEntityClassByInterface(Sso::class);
}
return $this->ssoClass;
}
private function createInstanceFromSso(Sso $sso): Keycloak
{
$instance = new Keycloak(
realm: $sso->getRealm(),
baseUrl: $sso->getBaseUrl(),
hostUrl: $sso->getHostUrl(),
clientId: $sso->getClientId(),
clientSecret: $sso->getClientSecret(),
frontendClientId: $sso->getFrontendClientId(),
em: $this->em,
linkGenerator: $this->linkGenerator,
securityUser: $this->securityUser,
identityQueryFactory: $this->identityQueryFactory,
aclRoleQueryFactory: $this->aclRoleQueryFactory,
fancyAdmin: $this->fancyAdmin,
session: $this->session,
storage: $this->storage,
);
$instance->setInstanceName($sso->getName());
$this->instances[$sso->getName()] = $instance;
return $instance;
}
}