-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGuard.php
More file actions
176 lines (153 loc) · 5.18 KB
/
Guard.php
File metadata and controls
176 lines (153 loc) · 5.18 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace WebmanTech\Auth\Guard;
use WebmanTech\Auth\Authentication\Method\CompositeMethod;
use WebmanTech\Auth\Authentication\Method\SessionMethod;
use WebmanTech\Auth\Interfaces\AuthenticationFailureHandlerInterface;
use WebmanTech\Auth\Interfaces\AuthenticationMethodInterface;
use WebmanTech\Auth\Interfaces\GuardInterface;
use WebmanTech\Auth\Interfaces\IdentityInterface;
use WebmanTech\Auth\Interfaces\IdentityRepositoryInterface;
use WebmanTech\CommonUtils\Session;
class Guard implements GuardInterface
{
public const SESSION_AUTH_ID = '__auth_id';
protected array $config = [
'identityRepository' => null,
'authenticationMethod' => null,
'authenticationFailureHandler' => null,
'sessionEnable' => false,
];
public function __construct(array $config = [])
{
$this->config = array_merge($this->config, $config);
}
protected ?IdentityRepositoryInterface $identityRepository = null;
/**
* @return IdentityRepositoryInterface
*/
protected function getIdentityRepository(): IdentityRepositoryInterface
{
if ($this->identityRepository === null) {
$value = call_user_func($this->config['identityRepository']);
if (!$value instanceof IdentityRepositoryInterface) {
throw new \InvalidArgumentException('identityRepository must be IdentityRepositoryInterface');
}
$this->identityRepository = $value;
}
return $this->identityRepository;
}
/**
* @var AuthenticationMethodInterface|null
*/
protected ?AuthenticationMethodInterface $authenticationMethod = null;
/**
* @inheritDoc
*/
public function getAuthenticationMethod(): AuthenticationMethodInterface
{
if ($this->authenticationMethod === null) {
$value = call_user_func($this->config['authenticationMethod'], $this->getIdentityRepository());
if (!$value instanceof AuthenticationMethodInterface) {
throw new \InvalidArgumentException('authenticationMethod must be AuthenticationMethodInterface');
}
$this->authenticationMethod = $value;
}
return $this->authenticationMethod;
}
/**
* @var AuthenticationFailureHandlerInterface|null
*/
protected ?AuthenticationFailureHandlerInterface $authenticationFailureHandler = null;
/**
* @inheritDoc
*/
public function getAuthenticationFailedHandler(): AuthenticationFailureHandlerInterface
{
if ($this->authenticationFailureHandler === null) {
$value = call_user_func($this->config['authenticationFailureHandler'], $this->getAuthenticationMethod());
if (!$value instanceof AuthenticationFailureHandlerInterface) {
throw new \InvalidArgumentException('authenticationFailureHandler must be AuthenticationFailureHandlerInterface');
}
$this->authenticationFailureHandler = $value;
}
return $this->authenticationFailureHandler;
}
protected ?IdentityInterface $identity = null;
/**
* @inheritDoc
*/
public function login(IdentityInterface $identity): void
{
$this->identity = $identity;
if ($this->isSessionEnable()) {
Session::getCurrent()->set(static::SESSION_AUTH_ID, $this->getId());
}
}
/**
* @inheritDoc
*/
public function logout(): void
{
if ($this->isGuest()) {
return;
}
$this->identity = null;
if ($this->isSessionEnable()) {
Session::getCurrent()->delete(static::SESSION_AUTH_ID);
}
}
protected ?bool $isSessionEnable = null;
/**
* 是否允许 session
* @return bool
*/
protected function isSessionEnable(): bool
{
if ($this->isSessionEnable !== null) {
return $this->isSessionEnable;
}
$sessionEnable = $this->config['sessionEnable'];
if ($sessionEnable) {
return $this->isSessionEnable = true;
}
$authenticationMethod = $this->getAuthenticationMethod();
if ($authenticationMethod instanceof SessionMethod) {
return $this->isSessionEnable = true;
}
if ($authenticationMethod instanceof CompositeMethod) {
foreach ($authenticationMethod->getMethods() as $method) {
if ($method instanceof SessionMethod) {
return $this->isSessionEnable = true;
}
}
}
return $this->isSessionEnable = false;
}
/**
* @inheritDoc
*/
public function isGuest(): bool
{
return $this->identity === null;
}
/**
* @inheritDoc
*/
public function getUser(bool $refresh = false): ?IdentityInterface
{
if (!$this->identity instanceof IdentityInterface) {
return null;
}
if ($refresh) {
$this->identity = $this->identity->refreshIdentity();
}
return $this->identity;
}
/**
* @inheritDoc
*/
public function getId(): ?string
{
return $this->identity instanceof IdentityInterface ? $this->identity->getId() : null;
}
}