-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuth.php
More file actions
46 lines (41 loc) · 1.26 KB
/
Auth.php
File metadata and controls
46 lines (41 loc) · 1.26 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
<?php
namespace WebmanTech\Auth;
use WebmanTech\Auth\Interfaces\GuardInterface;
use WebmanTech\Auth\Middleware\SetAuthGuard;
use WebmanTech\CommonUtils\Request;
class Auth
{
const REQUEST_AUTH_MANAGER = 'auth_manager';
/**
* guard
* 当使用 Middleware/SetAuthGuard 后,可以获取到当前的 Guard
* @param string|null $name
* @return GuardInterface
*/
public static function guard(?string $name = null): GuardInterface
{
if ($authManager = static::getAuthManager()) {
$name = $name ?: Request::getCurrent()?->getCustomData(SetAuthGuard::REQUEST_GUARD_NAME);
return $authManager->guard($name);
}
throw new \InvalidArgumentException('获取当前 guard 失败,请确认配置');
}
/**
* @return AuthManager|null
*/
public static function getAuthManager(): ?AuthManager
{
$request = Request::getCurrent();
if (!$request) {
return null;
}
$value = $request->getCustomData(static::REQUEST_AUTH_MANAGER);
if (!$value) {
$value = new AuthManager();
$request->withCustomData([
static::REQUEST_AUTH_MANAGER => $value,
]);
}
return $value;
}
}