2022-12-02 11:16:57 +08:00
|
|
|
<?php
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
namespace app\api\middleware\system;
|
|
|
|
|
use app\common\library\ResultCode;
|
2023-06-19 14:32:30 +08:00
|
|
|
use app\common\service\user\UserTokenService;
|
|
|
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
2022-08-19 19:48:37 +08:00
|
|
|
use Webman\Event\Event;
|
|
|
|
|
use Webman\MiddlewareInterface;
|
|
|
|
|
use Webman\Http\Response;
|
|
|
|
|
use Webman\Http\Request;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API权限中间件
|
|
|
|
|
* @package app\api\middleware\system
|
|
|
|
|
* @author meystack <
|
|
|
|
|
*/
|
|
|
|
|
class ApiPermissions implements MiddlewareInterface
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 控制器登录鉴权
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public bool $needLogin = false;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* API验证流程
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public bool $authWorkflow = true;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
2023-06-19 14:32:30 +08:00
|
|
|
/**
|
|
|
|
|
* 禁止登录重复
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
public array $repeatLogin = ['login', 'register'];
|
|
|
|
|
|
2022-08-19 19:48:37 +08:00
|
|
|
/**
|
|
|
|
|
* 非鉴权方法
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2023-06-19 14:32:30 +08:00
|
|
|
public array $noNeedLogin = [];
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 校验权限
|
2023-06-19 14:32:30 +08:00
|
|
|
* @param \support\Request|Request $request
|
2022-08-19 19:48:37 +08:00
|
|
|
* @param callable $handler
|
|
|
|
|
* @return Response
|
2023-06-19 14:32:30 +08:00
|
|
|
* @throws InvalidArgumentException
|
2022-12-02 11:16:57 +08:00
|
|
|
* @throws \ReflectionException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2023-06-19 14:32:30 +08:00
|
|
|
public function process(\support\Request|Request $request, callable $handler): Response
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
2022-12-02 11:16:57 +08:00
|
|
|
$app = request()->getApp();
|
2022-08-19 19:48:37 +08:00
|
|
|
$controller = request()->getController();
|
2022-12-02 11:16:57 +08:00
|
|
|
$action = request()->getAction();
|
|
|
|
|
$method = $controller . '/' . $action;
|
|
|
|
|
$refClass = new \ReflectionClass($request->controller);
|
|
|
|
|
$property = $refClass->getDefaultProperties();
|
|
|
|
|
$this->needLogin = $property['needLogin'] ?? $this->needLogin;
|
2023-06-19 14:32:30 +08:00
|
|
|
$this->noNeedLogin = $property['noNeedLogin'] ?? $this->noNeedLogin;
|
|
|
|
|
$this->repeatLogin = $property['repeatLogin'] ?? $this->repeatLogin;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
2023-06-19 14:32:30 +08:00
|
|
|
// 是否验证登录器
|
|
|
|
|
$userInfo = UserTokenService::isLogin();
|
2023-08-04 11:13:14 +08:00
|
|
|
if (!empty($userInfo) && isset($userInfo['id'])) {
|
2023-06-19 14:32:30 +08:00
|
|
|
$request->userId = $userInfo['id'];
|
|
|
|
|
$request->userInfo = $userInfo;
|
|
|
|
|
// 是否验证API权限
|
2022-08-19 19:48:37 +08:00
|
|
|
if ($this->authWorkflow && Event::hasListener('apiAuth')) {
|
2023-06-19 14:32:30 +08:00
|
|
|
$result = Event::emit('apiAuth', ['method' => $method, 'user_id' => $userInfo['id']], true);
|
2022-08-19 19:48:37 +08:00
|
|
|
if (isset($result['code']) && $result['code'] != 200) {
|
|
|
|
|
return json($result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-06-19 14:32:30 +08:00
|
|
|
if ($this->needLogin && !in_array($action, $this->noNeedLogin)) {
|
2022-08-19 19:48:37 +08:00
|
|
|
return json(ResultCode::AUTH_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $handler($request);
|
|
|
|
|
}
|
|
|
|
|
}
|