fix:优化auth权限,登录逻辑获取信息
This commit is contained in:
@@ -38,50 +38,45 @@ class IndexPermissions implements MiddlewareInterface
|
||||
* 跳转URL地址
|
||||
* @var string
|
||||
*/
|
||||
public string $JumpUrl = '/user/index';
|
||||
public string $JumpUrl = '/index/user/index';
|
||||
|
||||
/**
|
||||
* 校验权限
|
||||
* @param Request $request
|
||||
* @param callable $handler
|
||||
* @return Response
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
$app = request()->getApp();
|
||||
$app = request()->getApp();
|
||||
$controller = request()->getController();
|
||||
$action = request()->getAction();
|
||||
$action = request()->getAction();
|
||||
|
||||
// 控制器是否存在
|
||||
$className = '\app' . $app . '\\controller\\' . $controller;
|
||||
$className = str_replace('/', '\\', $className);
|
||||
|
||||
if (class_exists($className)) {
|
||||
$refClass = new \ReflectionClass($className);
|
||||
$property = $refClass->getDefaultProperties();
|
||||
$this->needLogin = $property['needLogin'] ?? false;
|
||||
$this->noNeedAuth = $property['noNeedAuth'] ?? [];
|
||||
$this->repeatLogin = $property['repeatLogin'] ?? ['login', 'register'];
|
||||
$this->JumpUrl = $property['JumpUrl'] ?? '/user/index';
|
||||
}
|
||||
$refClass = new \ReflectionClass($request->controller);
|
||||
$property = $refClass->getDefaultProperties();
|
||||
$this->needLogin = $property['needLogin'] ?? false;
|
||||
$this->noNeedAuth = $property['noNeedAuth'] ?? $this->noNeedAuth;
|
||||
$this->repeatLogin = $property['repeatLogin'] ?? $this->repeatLogin;
|
||||
$this->JumpUrl = $property['JumpUrl'] ?? $this->JumpUrl;
|
||||
|
||||
// 是否验证登录器
|
||||
$auth = Auth::instance();
|
||||
if ($auth->isLogin()) {
|
||||
$request->user_id = $auth->userData['id'];
|
||||
$request->userData = $auth->userData;
|
||||
|
||||
// 禁止重复登录
|
||||
if (in_array($action, $this->repeatLogin)) {
|
||||
return redirect($this->JumpUrl);
|
||||
}
|
||||
|
||||
View::assign('user', $auth->userData);
|
||||
View::assign('user', $auth->userInfo);
|
||||
} else {
|
||||
|
||||
if ($this->needLogin && !in_array($action, $this->noNeedAuth)) {
|
||||
if (\request()->isAjax()) {
|
||||
return json(ResultCode::PLEASELOGININ);
|
||||
} else {
|
||||
return redirect('/user/login');
|
||||
return redirect('/index/user/login');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user