|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Xetaravel\Http\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Detection\Exception\MobileDetectException; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Support\Collection; |
|
10
|
|
|
use Illuminate\Support\Facades\Auth; |
|
11
|
|
|
use Illuminate\Support\Facades\DB; |
|
12
|
|
|
use Illuminate\View\View; |
|
13
|
|
|
use Xetaravel\Services\DeviceDetectorService; |
|
14
|
|
|
|
|
15
|
|
|
class SecurityController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
protected DeviceDetectorService $detector; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(DeviceDetectorService $detector) |
|
23
|
|
|
{ |
|
24
|
|
|
parent::__construct(); |
|
25
|
|
|
$this->detector = $detector; |
|
26
|
|
|
|
|
27
|
|
|
$this->breadcrumbs->addCrumb( |
|
28
|
|
|
'<i class="fa-solid fa-user-shield mr-2"></i> Security', |
|
29
|
|
|
route('user.security.index') |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Show the security index page. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Request $request |
|
37
|
|
|
* |
|
38
|
|
|
* @return View |
|
39
|
|
|
* |
|
40
|
|
|
* @throws MobileDetectException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function index(Request $request): View |
|
43
|
|
|
{ |
|
44
|
|
|
$records = $this->getActiveSessions(); |
|
45
|
|
|
|
|
46
|
|
|
$sessions = []; |
|
47
|
|
|
|
|
48
|
|
|
foreach ($records as $record) { |
|
49
|
|
|
$infos = [ |
|
50
|
|
|
'platform' => $this->detector->getPlatform($record->user_agent), |
|
51
|
|
|
'platform_version' => $this->detector->getPlatformVersion($record->user_agent), |
|
52
|
|
|
'browser' => $this->detector->getBrowser($record->user_agent), |
|
53
|
|
|
'browser_version' => $this->detector->getBrowserVersion($record->user_agent), |
|
54
|
|
|
'device_type' => $this->detector->getDeviceType($record->user_agent) |
|
55
|
|
|
]; |
|
56
|
|
|
$record->infos = $infos; |
|
57
|
|
|
|
|
58
|
|
|
$sessions[] = $record; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$sessionId = $request->session()->getId(); |
|
62
|
|
|
|
|
63
|
|
|
return view('security.index', [ |
|
64
|
|
|
'sessions' => $sessions, |
|
65
|
|
|
'sessionId' => $sessionId, |
|
66
|
|
|
'breadcrumbs' => $this->breadcrumbs |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Récupère toutes les sessions non expirées de l'utilisateur connecté. |
|
72
|
|
|
* |
|
73
|
|
|
* @return Collection |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getActiveSessions() |
|
76
|
|
|
{ |
|
77
|
|
|
$sessionLifetime = config('session.lifetime') * 60; |
|
78
|
|
|
|
|
79
|
|
|
$expirationTime = time() - $sessionLifetime; |
|
80
|
|
|
|
|
81
|
|
|
return DB::table(config('session.table')) |
|
82
|
|
|
->where('user_id', Auth::id()) |
|
83
|
|
|
->where('last_activity', '>', $expirationTime) |
|
84
|
|
|
->get(); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|