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
77 lines (69 loc) · 2.36 KB
/
UserSession.php
File metadata and controls
77 lines (69 loc) · 2.36 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
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use ProcessMaker\Models\ProcessMakerModel;
class UserSession extends ProcessMakerModel
{
use HasFactory;
protected $fillable = [
'user_agent',
'ip_address',
'device_name',
'device_type',
'device_platform',
'device_browser',
'token',
'is_active',
'expired_date',
];
protected $casts = [
'is_active' => 'boolean',
'expired_date' => 'datetime',
];
public function user()
{
return $this->belongsTo(User::class);
}
/**
* Expires sessions from other IP addresses different to the active one.
*/
public static function expiresDuplicatedSessionByIP()
{
$usersActiveIP = [];
self::where('is_active', true)
->whereNull('expired_date')
->orderBy('user_id', 'asc')
->orderBy('created_at', 'desc')
->chunk(100, function ($sessions) use (&$usersActiveIP) {
foreach ($sessions as $session) {
if (!array_key_exists($session->user_id, $usersActiveIP)) {
$usersActiveIP[$session->user_id] = $session->user_id;
} else {
// expire all sessions except the ones within the active IP
$session->update(['expired_date' => now()]);
}
}
});
}
/**
* Close all active sessions from other devices different to the active one.
*/
public static function expiresDuplicatedSessionByDevice()
{
$usersActiveDevice = [];
self::where('is_active', true)
->whereNull('expired_date')
->orderBy('user_id', 'asc')
->orderBy('created_at', 'desc')
->chunk(100, function ($sessions) use (&$usersActiveDevice) {
foreach ($sessions as $session) {
if (!array_key_exists($session->user_id, $usersActiveDevice)) {
$usersActiveDevice[$session->user_id] = $session->user_id;
} else {
// expire all sessions except the ones within the active device
$session->update(['expired_date' => now()]);
}
}
});
}
}