first commit
This commit is contained in:
28
app/admin/middleware/system/AdminLogin.php
Normal file
28
app/admin/middleware/system/AdminLogin.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\middleware\system;
|
||||
use app\common\library\Auth;
|
||||
use think\facade\Cache;
|
||||
use Webman\Http\Request;
|
||||
use Webman\Http\Response;
|
||||
use Webman\MiddlewareInterface;
|
||||
|
||||
/**
|
||||
* 管理员登录中间件
|
||||
* @package app\common\middleware
|
||||
* @author meystack
|
||||
*/
|
||||
class AdminLogin implements MiddlewareInterface
|
||||
{
|
||||
public function process(Request $request, callable $handler) : Response
|
||||
{
|
||||
$_security = Auth::instance()->getToken('_security');
|
||||
$_buildToken = 'salt_' . $_security;
|
||||
if (empty($_security) || !Cache::get($_buildToken)) {
|
||||
$request->session()->delete('AdminLogin');
|
||||
return response(request_error(), 404);
|
||||
}
|
||||
|
||||
return $handler($request);
|
||||
}
|
||||
}
|
||||
149
app/admin/middleware/system/AdminPermissions.php
Normal file
149
app/admin/middleware/system/AdminPermissions.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\middleware\system;
|
||||
|
||||
use app\admin\library\Auth;
|
||||
use app\common\library\ResultCode;
|
||||
use app\common\model\system\Admin as AdminModel;
|
||||
use app\common\model\system\SystemLog;
|
||||
use Psr\SimpleCache\InvalidArgumentException;
|
||||
use support\View;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
use think\db\exception\ModelNotFoundException;
|
||||
use Webman\MiddlewareInterface;
|
||||
use Webman\Http\Response;
|
||||
use Webman\Http\Request;
|
||||
|
||||
/**
|
||||
* 管理员权限
|
||||
* @package app\admin\middleware\system
|
||||
* @author meystack <
|
||||
*/
|
||||
class AdminPermissions implements MiddlewareInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* 不需要鉴权的方法
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedAuth = [
|
||||
'/Index/index',
|
||||
'/Login/index',
|
||||
'/Login/logout',
|
||||
];
|
||||
|
||||
/**
|
||||
* 校验权限
|
||||
* @param Request $request
|
||||
* @param callable $handler
|
||||
* @return Response
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function process(Request $request, callable $handler): Response
|
||||
{
|
||||
$app = request()->getApp();
|
||||
$controller = request()->getController();
|
||||
$action = request()->getAction();
|
||||
$AdminLogin = request()->session()->get('AdminLogin');
|
||||
if (!isset($AdminLogin['id']) && strtolower($controller) !== 'login') {
|
||||
return redirect(url('/login/index'));
|
||||
}
|
||||
|
||||
$method = '/' . $controller. '/' .$action;
|
||||
if (!in_array($method, $this->noNeedAuth) && !in_array('*', $this->noNeedAuth)) {
|
||||
if (!Auth::instance()->SuperAdmin() && !Auth::instance()->check($method, $AdminLogin['id'])) {
|
||||
if (request()->isAjax()) {
|
||||
return json(['code' => 101, 'msg' => '没有权限']);
|
||||
} else {
|
||||
return $this->abortPage('没有权限!', 401);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分发请求
|
||||
* 控制器中间件
|
||||
*/
|
||||
$id = input('id');
|
||||
if (\request()->isPost()) {
|
||||
if ($controller == 'system/Admin') {
|
||||
if ($data = AdminModel::getById($id)) {
|
||||
$group_id = input('group_id');
|
||||
$group_id = !empty($group_id) ? $group_id . ',' . $data['group_id'] : $data['group_id'];
|
||||
$group_id = array_unique(explode(',', $group_id));
|
||||
if (!Auth::instance()->checkRulesForGroup($group_id)) {
|
||||
return json(ResultCode::AUTH_ERROR);
|
||||
}
|
||||
}
|
||||
} else if ($controller == 'system/AdminGroup') {
|
||||
if (!empty($id) && $id >= 1) {
|
||||
if (!Auth::instance()->checkRulesForGroup((array)$id)) {
|
||||
return json(ResultCode::AUTH_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
View::assign('app', $app);
|
||||
View::assign('controller', $controller);
|
||||
View::assign('action', $action);
|
||||
View::assign('AdminLogin', $AdminLogin);
|
||||
$this->writeAdminRequestLogs();
|
||||
return $handler($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入后台操作日志
|
||||
* @return void
|
||||
* @throws InvalidArgumentException
|
||||
* @throws DataNotFoundException
|
||||
* @throws DbException
|
||||
* @throws ModelNotFoundException
|
||||
*/
|
||||
public function writeAdminRequestLogs()
|
||||
{
|
||||
if (saenv('system_logs')) {
|
||||
|
||||
$actionLogs = [
|
||||
'module' => request()->app,
|
||||
'controller' => request()->controller,
|
||||
'action' => request()->action,
|
||||
'params' => serialize(request()->all()),
|
||||
'method' => request()->method(),
|
||||
'code' => 200,
|
||||
'url' => request()->url(),
|
||||
'ip' => request()->getRemoteIp(),
|
||||
'name' => session('AdminLogin.name'),
|
||||
];
|
||||
|
||||
if (empty($actionLogs['name'])) {
|
||||
$actionLogs['name'] = 'system';
|
||||
}
|
||||
|
||||
$actionLogs['type'] = 2;
|
||||
SystemLog::write($actionLogs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误页面
|
||||
* @param int $code
|
||||
* @param string $msg
|
||||
* @return \support\Response
|
||||
*/
|
||||
public function abortPage(string $msg = '', int $code = 404): Response
|
||||
{
|
||||
$exception = config('app.exception_template');
|
||||
if (isset($exception[$code])) {
|
||||
$template = @file_get_contents($exception[$code]);
|
||||
} else {
|
||||
$template = $msg;
|
||||
}
|
||||
|
||||
return \response($template, $code);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user