1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\User\CurrentUser\Storage; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Session\SessionInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Stores current identity ID in a session. |
11
|
|
|
*/ |
12
|
|
|
final class SessionCurrentIdentityStorage implements CurrentIdentityStorageInterface |
13
|
|
|
{ |
14
|
|
|
private const SESSION_AUTH_ID = '__auth_id'; |
15
|
|
|
private const SESSION_AUTH_EXPIRE = '__auth_expire'; |
16
|
|
|
private const SESSION_AUTH_ABSOLUTE_EXPIRE = '__auth_absolute_expire'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int|null the number of seconds in which the user will be logged out automatically in case of |
20
|
|
|
* remaining inactive. If this property is not set, the user will be logged out after |
21
|
|
|
* the current session expires. |
22
|
|
|
*/ |
23
|
|
|
private ?int $authTimeout = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int|null the number of seconds in which the user will be logged out automatically |
27
|
|
|
* regardless of activity. |
28
|
|
|
*/ |
29
|
|
|
private ?int $absoluteAuthTimeout = null; |
30
|
|
|
|
31
|
|
|
private SessionInterface $session; |
32
|
|
|
|
33
|
13 |
|
public function __construct(SessionInterface $session) |
34
|
|
|
{ |
35
|
13 |
|
$this->session = $session; |
36
|
13 |
|
} |
37
|
|
|
|
38
|
6 |
|
public function setAuthTimeout(int $timeout = null): self |
39
|
|
|
{ |
40
|
6 |
|
$this->authTimeout = $timeout; |
41
|
6 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
public function setAbsoluteAuthTimeout(int $timeout = null): self |
45
|
|
|
{ |
46
|
3 |
|
$this->absoluteAuthTimeout = $timeout; |
47
|
3 |
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
13 |
|
public function get(): ?string |
51
|
|
|
{ |
52
|
|
|
/** @var mixed $id */ |
53
|
13 |
|
$id = $this->session->get(self::SESSION_AUTH_ID); |
54
|
|
|
|
55
|
|
|
if ( |
56
|
13 |
|
$id !== null && |
57
|
13 |
|
($this->authTimeout !== null || $this->absoluteAuthTimeout !== null) |
58
|
|
|
) { |
59
|
7 |
|
$expire = $this->getExpire(); |
60
|
7 |
|
$expireAbsolute = $this->getExpireAbsoulte(); |
61
|
|
|
|
62
|
|
|
if ( |
63
|
7 |
|
($expire !== null && $expire < time()) || |
64
|
7 |
|
($expireAbsolute !== null && $expireAbsolute < time()) |
65
|
|
|
) { |
66
|
2 |
|
$this->clear(); |
67
|
2 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
if ($this->authTimeout !== null) { |
71
|
3 |
|
$this->session->set(self::SESSION_AUTH_EXPIRE, time() + $this->authTimeout); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
11 |
|
return $id === null ? null : (string)$id; |
76
|
|
|
} |
77
|
|
|
|
78
|
7 |
|
private function getExpire(): ?int |
79
|
|
|
{ |
80
|
|
|
/** @var mixed $expire */ |
81
|
7 |
|
$expire = $this->authTimeout !== null |
82
|
4 |
|
? $this->session->get(self::SESSION_AUTH_EXPIRE) |
83
|
7 |
|
: null; |
84
|
7 |
|
return $expire !== null ? (int)$expire : null; |
85
|
|
|
} |
86
|
|
|
|
87
|
7 |
|
private function getExpireAbsoulte(): ?int |
88
|
|
|
{ |
89
|
|
|
/** @var mixed $expire */ |
90
|
7 |
|
$expire = $this->absoluteAuthTimeout !== null |
91
|
3 |
|
? $this->session->get(self::SESSION_AUTH_ABSOLUTE_EXPIRE) |
92
|
7 |
|
: null; |
93
|
7 |
|
return $expire !== null ? (int)$expire : null; |
94
|
|
|
} |
95
|
|
|
|
96
|
4 |
|
public function set(string $id): void |
97
|
|
|
{ |
98
|
4 |
|
$this->switchId($id); |
99
|
4 |
|
} |
100
|
|
|
|
101
|
4 |
|
public function clear(): void |
102
|
|
|
{ |
103
|
4 |
|
$this->switchId(null); |
104
|
4 |
|
} |
105
|
|
|
|
106
|
7 |
|
private function switchId(?string $id): void |
107
|
|
|
{ |
108
|
7 |
|
$this->session->regenerateID(); |
109
|
|
|
|
110
|
7 |
|
$this->session->remove(self::SESSION_AUTH_ID); |
111
|
7 |
|
$this->session->remove(self::SESSION_AUTH_EXPIRE); |
112
|
|
|
|
113
|
7 |
|
if ($id === null) { |
114
|
4 |
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
4 |
|
$this->session->set(self::SESSION_AUTH_ID, $id); |
118
|
4 |
|
if ($this->authTimeout !== null) { |
119
|
2 |
|
$this->session->set(self::SESSION_AUTH_EXPIRE, time() + $this->authTimeout); |
120
|
|
|
} |
121
|
4 |
|
if ($this->absoluteAuthTimeout !== null) { |
122
|
1 |
|
$this->session->set(self::SESSION_AUTH_ABSOLUTE_EXPIRE, time() + $this->absoluteAuthTimeout); |
123
|
|
|
} |
124
|
4 |
|
} |
125
|
|
|
} |
126
|
|
|
|