first commit
This commit is contained in:
47
app/index/middleware/system/IndexInitialize.php
Normal file
47
app/index/middleware/system/IndexInitialize.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
declare (strict_types=1);
|
||||
// +----------------------------------------------------------------------
|
||||
// | swiftAdmin 极速开发框架 [基于WebMan开发]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
|
||||
// +----------------------------------------------------------------------
|
||||
// | swiftAdmin.net High Speed Development Framework
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\index\middleware\system;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use Webman\MiddlewareInterface;
|
||||
use function redirect;
|
||||
use function root_path;
|
||||
|
||||
/**
|
||||
* 前台应用中间件
|
||||
* Class AppInitialize
|
||||
* @package app\index\middleware\system
|
||||
* @author meystack <
|
||||
*/
|
||||
|
||||
class IndexInitialize implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
try {
|
||||
if (saenv('site_status')) {
|
||||
$content = file_get_contents(root_path('extend/conf/tpl') . 'close.tpl');
|
||||
$content = str_replace('{text}',saenv('site_notice'),$content);
|
||||
return \response($content, 503);
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
return \response('Web site has been closed', 503);
|
||||
}
|
||||
|
||||
if (!is_file(root_path('extend/conf').'install.lock')) {
|
||||
return redirect('/install/index');
|
||||
}
|
||||
|
||||
return $handler($request);
|
||||
}
|
||||
}
|
||||
85
app/index/middleware/system/IndexPermissions.php
Normal file
85
app/index/middleware/system/IndexPermissions.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace app\index\middleware\system;
|
||||
|
||||
use app\common\library\Auth;
|
||||
use support\View;
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Response;
|
||||
use Webman\Http\Request;
|
||||
|
||||
/**
|
||||
* 管理员权限
|
||||
* @package app\admin\middleware\system
|
||||
* @author meystack <
|
||||
*/
|
||||
class IndexPermissions implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* 控制器登录鉴权
|
||||
* @var bool
|
||||
*/
|
||||
public $needLogin = false;
|
||||
|
||||
/**
|
||||
* 禁止登录重复
|
||||
* @var array
|
||||
*/
|
||||
public $repeatLogin = ['login', 'register'];
|
||||
|
||||
/**
|
||||
* 非鉴权方法
|
||||
* @var array
|
||||
*/
|
||||
public $noNeedAuth = [];
|
||||
|
||||
/**
|
||||
* 跳转URL地址
|
||||
* @var string
|
||||
*/
|
||||
public $JumpUrl = '/user/index';
|
||||
|
||||
/**
|
||||
* 校验权限
|
||||
* @param Request $request
|
||||
* @param callable $handler
|
||||
* @return Response
|
||||
*/
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
$app = request()->getApp();
|
||||
$controller = request()->getController();
|
||||
$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';
|
||||
}
|
||||
|
||||
// 是否验证登录器
|
||||
$auth = Auth::instance();
|
||||
if ($auth->isLogin()) {
|
||||
$request->userId = $auth->userInfo['id'];
|
||||
$request->userInfo = $auth->userInfo;
|
||||
if (in_array($action, $this->repeatLogin)) {
|
||||
return redirect($this->JumpUrl);
|
||||
}
|
||||
|
||||
View::assign('user', $auth->userInfo);
|
||||
} else {
|
||||
if ($this->needLogin && !in_array($action, $this->noNeedAuth)) {
|
||||
return redirect('/');
|
||||
}
|
||||
}
|
||||
|
||||
return $handler($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user