-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionGridTrait.php
More file actions
113 lines (93 loc) · 3.22 KB
/
Copy pathSessionGridTrait.php
File metadata and controls
113 lines (93 loc) · 3.22 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
<?php
declare(strict_types=1);
namespace ADT\FancyAdmin\UI\Components\Grids\Session;
use ADT\Datagrid\Component\DataGrid;
use ADT\DoctrineAuthenticator\StorageEntity;
use ADT\FancyAdmin\DI\Injects\AuthenticatorInject;
trait SessionGridTrait
{
use AuthenticatorInject;
protected function setGridDataSource(DataGrid $grid): void
{
$this->withoutIsActiveColumn = true;
$grid->setPagination(false);
$objectId = $this->getSecurityUser()->getIdentity()->getAuthObjectId();
$currentSessionId = $this->_authenticator->getCurrentSessionId();
$sessions = $this->_authenticator->getActiveSessions($objectId);
$data = [];
foreach ($sessions as $session) {
/** @var StorageEntity $session */
$data[] = [
'id' => $session->getId(),
'ip' => $session->getIp(),
'userAgent' => $this->parseUserAgent($session->getUserAgent()),
'createdAt' => $session->getCreatedAt()->format('d.m.Y H:i'),
'validUntil' => $session->getValidUntil()->format('d.m.Y H:i'),
'isCurrent' => $session->getId() === $currentSessionId,
];
}
$grid->setDataSource($data);
}
public function initGrid(DataGrid $grid): void
{
$grid->addColumnText('ip', 'fcadmin.grids.session.ip');
$grid->addColumnText('userAgent', 'fcadmin.grids.session.userAgent');
$grid->addColumnText('createdAt', 'fcadmin.grids.session.createdAt');
$grid->addColumnText('validUntil', 'fcadmin.grids.session.validUntil');
$grid->addColumnText('isCurrent', 'fcadmin.grids.session.isCurrent')
->setRenderer(function (array $row): string {
return $row['isCurrent']
? '<span class="badge bg-success">' . $this->getTranslator()->translate('fcadmin.grids.session.currentDevice') . '</span>'
: '';
})
->setTemplateEscaping(false);
$grid->addAction('invalidate', 'fcadmin.grids.session.invalidate', 'invalidate!')
->setIcon('sign-out-alt')
->setClass('btn btn-danger btn-sm ajax')
->setRenderCondition(function (array $row): bool {
return !$row['isCurrent'];
});
}
public function handleInvalidate(int $id): void
{
$this->_authenticator->clearSession($id);
$this->redirect('this');
}
protected function getQueryObjectFactoryClass(): string
{
return '';
}
private function parseUserAgent(?string $userAgent): string
{
if ($userAgent === null) {
return '';
}
if (preg_match('/(?:Chrome|CriOS)\/[\d.]+/', $userAgent) && !str_contains($userAgent, 'Edg')) {
$browser = 'Chrome';
} elseif (preg_match('/Firefox\/[\d.]+/', $userAgent)) {
$browser = 'Firefox';
} elseif (preg_match('/Edg\/[\d.]+/', $userAgent)) {
$browser = 'Edge';
} elseif (preg_match('/Safari\/[\d.]+/', $userAgent) && !str_contains($userAgent, 'Chrome')) {
$browser = 'Safari';
} elseif (preg_match('/OPR\/[\d.]+/', $userAgent)) {
$browser = 'Opera';
} else {
return $userAgent;
}
if (str_contains($userAgent, 'Windows')) {
$os = 'Windows';
} elseif (str_contains($userAgent, 'Macintosh')) {
$os = 'macOS';
} elseif (str_contains($userAgent, 'Linux')) {
$os = 'Linux';
} elseif (str_contains($userAgent, 'iPhone') || str_contains($userAgent, 'iPad')) {
$os = 'iOS';
} elseif (str_contains($userAgent, 'Android')) {
$os = 'Android';
} else {
$os = '';
}
return $os ? "$browser ($os)" : $browser;
}
}