-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAuthorization.php
More file actions
72 lines (61 loc) · 2.04 KB
/
Authorization.php
File metadata and controls
72 lines (61 loc) · 2.04 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
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\oidc\Admin;
use SimpleSAML\Error\Exception;
use SimpleSAML\Locale\Translate;
use SimpleSAML\Module\oidc\Bridges\SspBridge;
use SimpleSAML\Module\oidc\Exceptions\AuthorizationException;
use SimpleSAML\Module\oidc\Services\AuthContextService;
class Authorization
{
public function __construct(
protected readonly SspBridge $sspBridge,
protected readonly AuthContextService $authContextService,
) {
}
public function isAdmin(): bool
{
return $this->sspBridge->utils()->auth()->isAdmin();
}
/**
* @throws \SimpleSAML\Module\oidc\Exceptions\AuthorizationException
*/
public function requireAdmin(bool $forceAdminAuthentication = false): void
{
if ($forceAdminAuthentication) {
try {
$this->sspBridge->utils()->auth()->requireAdmin();
} catch (Exception $exception) {
throw new AuthorizationException(
Translate::noop('Unable to initiate SimpleSAMLphp admin authentication.'),
$exception->getCode(),
$exception,
);
}
}
if (! $this->isAdmin()) {
throw new AuthorizationException(Translate::noop('SimpleSAMLphp admin access required.'));
}
}
/**
* @throws \SimpleSAML\Module\oidc\Exceptions\AuthorizationException
*/
public function requireAdminOrUserWithPermission(string $permission): void
{
if ($this->isAdmin()) {
return;
}
try {
$this->authContextService->requirePermission($permission);
} catch (\Exception) {
// TODO mivanci v7 log this exception
}
// If we get here, the user does not have the required permission, or permissions are not enabled.
// Fallback to admin authentication.
$this->requireAdmin(true);
}
public function getUserId(): string
{
return $this->authContextService->getAuthUserId();
}
}