Skip to content

Commit ca786fb

Browse files
committed
feature: 移动 Auth
1 parent bbc5307 commit ca786fb

7 files changed

Lines changed: 49 additions & 40 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ composer require kriss/webman-auth
3535
### 认证授权方法
3636

3737
```php
38-
use Kriss\WebmanAuth\facade\Auth;
38+
use Kriss\WebmanAuth\Auth;
3939

4040
$guard = Auth::guard(); // 获取默认的 guard
4141
$guard = Auth::guard('admin'); // 获取指定名称的 guard

examples/multi-user/app/admin/controller/AuthController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace app\admin\controller;
44

55
use app\model\Admin;
6-
use Kriss\WebmanAuth\facade\Auth;
6+
use Kriss\WebmanAuth\Auth;
77
use support\Request;
88
use support\Response;
99

examples/multi-user/app/api/controller/AuthController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace app\api\controller;
44

55
use app\model\User;
6-
use Kriss\WebmanAuth\facade\Auth;
6+
use Kriss\WebmanAuth\Auth;
77
use support\Request;
88
use support\Response;
99

examples/multi-user/app/middleware/AuthenticateAdmin.php renamed to examples/multi-user/app/middleware/SetAuthGuardAdmin.php

File renamed without changes.

src/Auth.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Kriss\WebmanAuth;
4+
5+
use Kriss\WebmanAuth\Interfaces\GuardInterface;
6+
use Kriss\WebmanAuth\Middleware\SetAuthGuard;
7+
8+
class Auth
9+
{
10+
const REQUEST_AUTH_MANAGER = 'auth_manager';
11+
12+
/**
13+
* guard
14+
* 当使用 Middleware/SetAuthGuard 后,可以获取到当前的 Guard
15+
* @param string|null $name
16+
* @return GuardInterface|null
17+
*/
18+
public static function guard(string $name = null): ?GuardInterface
19+
{
20+
if ($authManager = static::getAuthManager()) {
21+
$name = $name ?: request()->{SetAuthGuard::REQUEST_GUARD_NAME};
22+
return $authManager->guard($name);
23+
}
24+
return null;
25+
}
26+
27+
/**
28+
* @return AuthManager|null
29+
*/
30+
public static function getAuthManager(): ?AuthManager
31+
{
32+
$request = request();
33+
if (!$request) {
34+
return null;
35+
}
36+
if (!$request->{static::REQUEST_AUTH_MANAGER}) {
37+
$request->{static::REQUEST_AUTH_MANAGER} = new AuthManager();
38+
}
39+
return $request->{static::REQUEST_AUTH_MANAGER};
40+
}
41+
}

src/Middleware/Authentication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Kriss\WebmanAuth\Middleware;
44

5-
use Kriss\WebmanAuth\facade\Auth;
5+
use Kriss\WebmanAuth\Auth;
66
use Kriss\WebmanAuth\Interfaces\GuardInterface;
77
use Kriss\WebmanAuth\Interfaces\IdentityInterface;
88
use Webman\Http\Request;

src/facade/Auth.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,9 @@
22

33
namespace Kriss\WebmanAuth\facade;
44

5-
use Kriss\WebmanAuth\AuthManager;
6-
use Kriss\WebmanAuth\Interfaces\GuardInterface;
7-
use Kriss\WebmanAuth\Middleware\SetAuthGuard;
8-
9-
class Auth
5+
/**
6+
* @deprecated
7+
*/
8+
class Auth extends \Kriss\WebmanAuth\Auth
109
{
11-
const REQUEST_AUTH_MANAGER = 'auth_manager';
12-
13-
/**
14-
* guard
15-
* 当使用 Middleware/SetAuthGuard 后,可以获取到当前的 Guard
16-
* @param string|null $name
17-
* @return GuardInterface|null
18-
*/
19-
public static function guard(string $name = null): ?GuardInterface
20-
{
21-
if ($authManager = static::getAuthManager()) {
22-
$name = $name ?: request()->{SetAuthGuard::REQUEST_GUARD_NAME};
23-
return $authManager->guard($name);
24-
}
25-
return null;
26-
}
27-
28-
/**
29-
* @return AuthManager|null
30-
*/
31-
public static function getAuthManager(): ?AuthManager
32-
{
33-
$request = request();
34-
if (!$request) {
35-
return null;
36-
}
37-
if (!$request->{static::REQUEST_AUTH_MANAGER}) {
38-
$request->{static::REQUEST_AUTH_MANAGER} = new AuthManager();
39-
}
40-
return $request->{static::REQUEST_AUTH_MANAGER};
41-
}
4210
}

0 commit comments

Comments
 (0)