forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserSession.php
More file actions
72 lines (61 loc) · 2.23 KB
/
UserSession.php
File metadata and controls
72 lines (61 loc) · 2.23 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
namespace ProcessMaker\Listeners;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use Jenssegers\Agent\Agent;
use ProcessMaker\Models\Setting;
use ProcessMaker\Models\UserSession as Session;
class UserSession
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}
/**
* Handle the event.
*/
public function handle(object $event): void
{
$agent = new Agent();
$user = $event->user;
$configIP = Setting::configByKey('session-control.ip_restriction');
$configDevice = Setting::configByKey('session-control.device_restriction');
// Get the IP address and device information
$ip = request()->getClientIp() ?? request()->ip();
$agentDevice = $agent->device() ? $agent->device() : 'Unknown';
$agentDeviceType = $agent->deviceType();
$agentPlatform = $agent->platform();
if ($configIP === '2') {
$user->sessions()
->where('is_active', true)
->where('ip_address', $ip)
->update(['expired_date' => now()->toDateTimeString()]);
}
if ($configDevice === '2') {
$user->sessions()
->where('is_active', true)
->where(function (Builder $query) use ($agentDevice, $agentDeviceType, $agentPlatform, $ip) {
$query->where('device_name', '!=', $agentDevice)
->orWhere('device_type', '!=', $agentDeviceType)
->orWhere('device_platform', '!=', $agentPlatform)
->orWhere('ip_address', '!=', $ip);
})
->update(['expired_date' => now()->toDateTimeString()]);
}
$session = new Session([
'user_agent' => request()->userAgent(),
'ip_address' => $ip,
'device_name' => $agentDevice,
'device_type' => $agentDeviceType,
'device_platform' => $agentPlatform,
'device_browser' => $agent->browser(),
'token' => Str::uuid()->toString(),
'is_active' => true,
]);
$user->sessions()->save($session);
session(['user_session' => $session->token]);
}
}