-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNetteSecurityIntegration.php
More file actions
79 lines (59 loc) · 1.76 KB
/
NetteSecurityIntegration.php
File metadata and controls
79 lines (59 loc) · 1.76 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
<?php declare(strict_types = 1);
namespace Contributte\Sentry\Integration;
use Nette\DI\Container;
use Nette\Http\IRequest;
use Nette\Http\Session;
use Nette\Security\IIdentity;
use Nette\Security\UserStorage;
use Sentry\Event;
use Sentry\EventHint;
use Sentry\State\HubInterface;
use Sentry\UserDataBag;
class NetteSecurityIntegration extends BaseIntegration
{
public function __construct(protected Container $context)
{
}
public function setup(HubInterface $hub, Event $event, EventHint $hint): ?Event
{
$storage = $this->context->getByType(UserStorage::class, false);
// There is no user storage
if (!$storage instanceof UserStorage) {
return $event;
}
$session = $this->context->getByType(Session::class, false);
// There is no session
if (!$session instanceof Session) {
return $event;
}
// Closed session
if (!$session->isStarted()) {
return $event;
}
$state = $storage->getState();
// There is no user logged in
if (!$state[0]) {
return $event;
}
$identity = $state[1];
// Anonymous user
if (!($identity instanceof IIdentity)) {
return $event;
}
$httpRequest = $this->context->getByType(IRequest::class);
/** @var array<string, mixed> $identityData */
$identityData = $identity->getData();
$identityRoles = $identity->getRoles();
$identityId = $identity->getId();
$bag = new UserDataBag(
is_scalar($identityId) ? (string) $identityId : '',
is_string($identityData['email'] ?? null) ? $identityData['email'] : null,
$httpRequest->getRemoteAddress(),
is_string($identityData['username'] ?? null) ? $identityData['username'] : null
);
$bag->setMetadata('Roles', implode(',', $identityRoles));
$bag->setMetadata('Identity', $identityData);
$event->setUser($bag);
return $event;
}
}