|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Da\User\Query; |
|
13
|
|
|
|
|
14
|
|
|
use Da\User\Traits\ModuleAwareTrait; |
|
15
|
|
|
use yii\web\Session; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
|
|
18
|
|
|
class SessionHistoryCondition |
|
19
|
|
|
{ |
|
20
|
|
|
use ModuleAwareTrait; |
|
21
|
|
|
|
|
22
|
|
|
private $session; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Session $session) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->session = $session; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function unbindSession() |
|
30
|
|
|
{ |
|
31
|
|
|
return ['session_id' => null]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function bySession($sessionId) |
|
35
|
|
|
{ |
|
36
|
|
|
return ['session_id' => $sessionId]; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function byUser($userId) |
|
40
|
|
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
'user_id' => $userId, |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function byUserSession($userId, $sessionId) |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
'user_id' => $userId, |
|
50
|
|
|
'session_id' => $sessionId, |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function inactive($userId = null) |
|
55
|
|
|
{ |
|
56
|
|
|
$where = [ |
|
57
|
|
|
'AND', |
|
58
|
|
|
['session_id' => null] |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
if (isset($userId)) { |
|
62
|
|
|
$where[] = $this->byUser($userId); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $where; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function expired($userId = null) |
|
69
|
|
|
{ |
|
70
|
|
|
$where = [ |
|
71
|
|
|
'AND', |
|
72
|
|
|
['<', 'updated_at', $this->getExpiredTime()] |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
if (isset($userId)) { |
|
76
|
|
|
$where[] = $this->byUser($userId); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $where; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function expiredInactive($userId = null) |
|
83
|
|
|
{ |
|
84
|
|
|
return [ |
|
85
|
|
|
'OR', |
|
86
|
|
|
$this->expired($userId), |
|
87
|
|
|
$this->inactive($userId), |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function shouldDeleteBefore($updatedAt, $userId) |
|
92
|
|
|
{ |
|
93
|
|
|
$condition = ['<', 'updated_at', $updatedAt]; |
|
94
|
|
|
if ($updatedAt > $this->getExpiredTime()) { |
|
95
|
|
|
$condition = [ |
|
96
|
|
|
'OR', |
|
97
|
|
|
[ |
|
98
|
|
|
'AND', |
|
99
|
|
|
$this->inactive(), |
|
100
|
|
|
$condition, |
|
101
|
|
|
], |
|
102
|
|
|
$this->expired() |
|
103
|
|
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return [ |
|
107
|
|
|
'AND', |
|
108
|
|
|
$this->byUser($userId), |
|
109
|
|
|
$condition, |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return int |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getExpiredTime() |
|
117
|
|
|
{ |
|
118
|
|
|
$module = $this->getModule(); |
|
119
|
|
|
$time = time() - max($module->rememberLoginLifespan, $this->session->getTimeout()); |
|
120
|
|
|
if (false === $module->hasTimeoutSessionHistory()) { |
|
121
|
|
|
return $time; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $time - $module->timeoutSessionHistory; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function inactiveData() |
|
128
|
|
|
{ |
|
129
|
|
|
return [ |
|
130
|
|
|
'session_id' => null, |
|
131
|
|
|
]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
public function currentUserData() |
|
138
|
|
|
{ |
|
139
|
|
|
return [ |
|
140
|
|
|
'user_id' => Yii::$app->user->id, |
|
141
|
|
|
'session_id' => Yii::$app->session->getId(), |
|
142
|
|
|
'user_agent' => Yii::$app->request->userAgent, |
|
143
|
|
|
'ip' => Yii::$app->request->userIP, |
|
144
|
|
|
]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
public function currentUserCondition() |
|
151
|
|
|
{ |
|
152
|
|
|
return [ |
|
153
|
|
|
'user_id' => Yii::$app->user->id, |
|
154
|
|
|
'session_id' => Yii::$app->session->getId(), |
|
155
|
|
|
'user_agent' => Yii::$app->request->userAgent, |
|
156
|
|
|
]; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|