1 | <?php |
||
2 | namespace App\Model; |
||
3 | |||
4 | use App\Behavior\TimestampBehavior; |
||
5 | use Yii; |
||
6 | |||
7 | /** |
||
8 | * This is the model class for table "t_user_session". |
||
9 | * |
||
10 | * @property string $id |
||
11 | * @property int $user_id User ID |
||
12 | * @property string $token Access Token |
||
13 | * @property string $refresh_token Refresh Token |
||
14 | * @property string $ip_address IP Address |
||
15 | * @property string $user_agent User Agent |
||
16 | * @property int $expire_time Expire Time |
||
17 | * @property int $refresh_token_expire_time Refresh Token Expire Time |
||
18 | * @property int $create_time Create Time |
||
19 | * @property int $update_time Update Time |
||
20 | * |
||
21 | * @property User $user |
||
22 | */ |
||
23 | class Session extends ActiveRecord implements StatusInterface |
||
24 | { |
||
25 | use StatusTrait; |
||
26 | |||
27 | 3 | public static function tableName() |
|
28 | { |
||
29 | 3 | return '{{%session}}'; |
|
30 | } |
||
31 | |||
32 | 3 | public function behaviors() |
|
33 | { |
||
34 | return [ |
||
35 | 3 | TimestampBehavior::class, |
|
36 | ]; |
||
37 | } |
||
38 | |||
39 | 1 | public function rules() |
|
40 | { |
||
41 | return [ |
||
42 | 1 | [['ip_address', 'user_agent'], 'default', 'value' => ''], |
|
43 | [['id', 'token', 'refresh_token', 'user_id', 'expire_time', 'refresh_token_expire_time'], 'required'], |
||
44 | [['user_id', 'expire_time', 'create_time', 'update_time'], 'integer'], |
||
45 | [['token'], 'string', 'max' => 32], |
||
46 | [['ip_address'], 'string', 'max' => 45], |
||
47 | [['user_agent'], 'string', 'max' => 255], |
||
48 | [['token'], 'unique'], |
||
49 | ]; |
||
50 | } |
||
51 | |||
52 | public function attributeLabels() |
||
53 | { |
||
54 | return [ |
||
55 | 'token' => Yii::t('app', 'Access Token'), |
||
56 | 'user_id' => Yii::t('app', 'User ID'), |
||
57 | 'ip_address' => Yii::t('app', 'Ip Address'), |
||
58 | 'user_agent' => Yii::t('app', 'User Agent'), |
||
59 | 'expire_time' => Yii::t('app', 'Expire Time'), |
||
60 | 'create_time' => Yii::t('app', 'Create Time'), |
||
61 | 'update_time' => Yii::t('app', 'Update Time'), |
||
62 | ]; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return \yii\db\ActiveQuery |
||
67 | */ |
||
68 | public function getUser() |
||
69 | { |
||
70 | return $this->hasOne(User::class, ['id' => 'user_id']); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Returns a bool value indicates whether the session is expired. |
||
75 | * |
||
76 | * @param int|null $now current timestamp. |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function isExpired(?int $now = null): bool |
||
81 | { |
||
82 | $now = $now ?? time(); |
||
83 | return $this->expire_time < $now; |
||
84 | } |
||
85 | |||
86 | 1 | public function getExpiresIn(?int $now = null): int |
|
87 | { |
||
88 | 1 | $now = $now ?? time(); |
|
89 | 1 | $remaining = $this->expire_time - $now; |
|
90 | 1 | return $remaining > 0 ? $remaining : 0; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Returns a bool value indicates whether the session is expired. |
||
95 | * |
||
96 | * @param int|null $now current timestamp. |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function isRefreshTokenExpired(?int $now = null): bool |
||
101 | { |
||
102 | $now = $now ?? time(); |
||
103 | return $this->refresh_token_expire_time < $now; |
||
104 | } |
||
105 | |||
106 | 1 | public function getRefreshTokenExpiresIn(?int $now = null): int |
|
107 | { |
||
108 | 1 | $now = $now ?? time(); |
|
109 | 1 | $remaining = $this->refresh_token_expire_time - $now; |
|
110 | 1 | return $remaining > 0 ? $remaining : 0; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * Finds session by token. |
||
115 | * |
||
116 | * @param string $token access token. |
||
117 | * |
||
118 | * @return self|null |
||
119 | */ |
||
120 | public static function findByToken(string $token): ?self |
||
121 | { |
||
122 | return static::findOne(['token' => $token]); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Finds session by refresh token. |
||
127 | * |
||
128 | * @param string $token refresh token. |
||
129 | * |
||
130 | * @return self|null |
||
131 | */ |
||
132 | public static function findByRefreshToken(string $token): ?self |
||
133 | { |
||
134 | return static::findOne(['refresh_token' => $token]); |
||
0 ignored issues
–
show
|
|||
135 | } |
||
136 | } |
||
137 |