Passed
Push — 5.0.0 ( 49e1c0...87aae2 )
by Fèvre
06:22
created

SecurityController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A index() 0 25 2
A getActiveSessions() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Xetaravel\Http\Controllers\User;
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\Http\Controllers\Controller;
14
use Xetaravel\Services\DeviceDetectorService;
15
16
class SecurityController extends Controller
17
{
18
    protected DeviceDetectorService $detector;
19
20
    /**
21
     * Constructor
22
     */
23
    public function __construct(DeviceDetectorService $detector)
24
    {
25
        parent::__construct();
26
        $this->detector = $detector;
27
28
        $this->breadcrumbs->addCrumb(
29
            '<svg class="inline w-5 h-5 mr-2" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path d="M224 256A128 128 0 1 0 224 0a128 128 0 1 0 0 256zm-45.7 48C79.8 304 0 383.8 0 482.3C0 498.7 13.3 512 29.7 512l388.6 0c1.8 0 3.5-.2 5.3-.5c-76.3-55.1-99.8-141-103.1-200.2c-16.1-4.8-33.1-7.3-50.7-7.3l-91.4 0zm308.8-78.3l-120 48C358 277.4 352 286.2 352 296c0 63.3 25.9 168.8 134.8 214.2c5.9 2.5 12.6 2.5 18.5 0C614.1 464.8 640 359.3 640 296c0-9.8-6-18.6-15.1-22.3l-120-48c-5.7-2.3-12.1-2.3-17.8 0zM591.4 312c-3.9 50.7-27.2 116.7-95.4 149.7l0-187.8L591.4 312z"></path></svg>
30
                          Security',
31
            route('user.security.index')
32
        );
33
    }
34
35
    /**
36
     * Show the security index page.
37
     *
38
     * @param Request $request
39
     *
40
     * @return View
41
     *
42
     * @throws MobileDetectException
43
     */
44
    public function index(Request $request): View
45
    {
46
        $records = $this->getActiveSessions();
47
48
        $sessions = [];
49
50
        foreach ($records as $record) {
51
            $infos = [
52
                'platform' => $this->detector->getPlatform($record->user_agent),
53
                'platform_version' => $this->detector->getPlatformVersion($record->user_agent),
54
                'browser' => $this->detector->getBrowser($record->user_agent),
55
                'browser_version' => $this->detector->getBrowserVersion($record->user_agent),
56
                'device_type' => $this->detector->getDeviceType($record->user_agent)
57
            ];
58
            $record->infos = $infos;
59
60
            $sessions[] = $record;
61
        }
62
63
        $sessionId = $request->session()->getId();
64
65
        return view('security.index', [
66
            'sessions' => $sessions,
67
            'sessionId' => $sessionId,
68
            'breadcrumbs' => $this->breadcrumbs
69
        ]);
70
    }
71
72
    /**
73
     * Récupère toutes les sessions non expirées de l'utilisateur connecté.
74
     *
75
     * @return Collection
76
     */
77
    public function getActiveSessions()
78
    {
79
        $sessionLifetime = config('session.lifetime') * 60;
80
81
        $expirationTime = time() - $sessionLifetime;
82
83
        return DB::table(config('session.table'))
84
            ->where('user_id', Auth::id())
85
            ->where('last_activity', '>', $expirationTime)
86
            ->get();
87
    }
88
}
89