2019-12-11 20:59:59 +08:00
|
|
|
<?php
|
2019-12-11 21:00:14 +08:00
|
|
|
namespace catchAdmin\permissions;
|
|
|
|
|
2020-01-09 22:20:36 +08:00
|
|
|
use app\Request;
|
2019-12-12 18:52:33 +08:00
|
|
|
use catchAdmin\permissions\model\Permissions;
|
2020-01-17 11:29:33 +08:00
|
|
|
use catcher\CatchCacheKeys;
|
2019-12-26 09:03:09 +08:00
|
|
|
use catcher\Code;
|
2019-12-12 18:52:33 +08:00
|
|
|
use catcher\exceptions\PermissionForbiddenException;
|
2020-01-17 11:29:33 +08:00
|
|
|
use think\facade\Cache;
|
2019-12-12 18:52:33 +08:00
|
|
|
use think\helper\Str;
|
2019-12-11 21:00:14 +08:00
|
|
|
|
|
|
|
class PermissionsMiddleware
|
|
|
|
{
|
2019-12-12 18:52:33 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月12日
|
|
|
|
* @param Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
* @throws PermissionForbiddenException
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, \Closure $next)
|
2019-12-11 21:00:14 +08:00
|
|
|
{
|
2020-01-17 11:29:33 +08:00
|
|
|
$rule = $request->rule()->getName();
|
2019-12-14 22:54:41 +08:00
|
|
|
|
|
|
|
if (!$rule) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
[$module, $controller, $action] = $this->parseRule($rule);
|
|
|
|
|
|
|
|
if (in_array($module, $this->ignoreModule())) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:29:33 +08:00
|
|
|
$user = $request->user();
|
|
|
|
if (!$user) {
|
2019-12-26 09:03:09 +08:00
|
|
|
throw new PermissionForbiddenException('Login is invalid', Code::LOST_LOGIN);
|
2019-12-14 17:37:45 +08:00
|
|
|
}
|
|
|
|
|
2020-01-17 11:29:33 +08:00
|
|
|
// toad
|
|
|
|
$permission = $this->getPermission($module, $controller, $action);
|
|
|
|
if (!$permission || !in_array($permission->id, Cache::get(CatchCacheKeys::USER_PERMISSIONS . $user->id))) {
|
|
|
|
throw new PermissionForbiddenException();
|
2019-12-12 18:52:33 +08:00
|
|
|
}
|
2019-12-11 21:00:14 +08:00
|
|
|
|
2020-01-17 11:29:33 +08:00
|
|
|
// 操作日志
|
|
|
|
event('operateLog', [
|
|
|
|
'request' => $request,
|
|
|
|
'permission' => $permission,
|
|
|
|
]);
|
|
|
|
|
2019-12-11 21:00:14 +08:00
|
|
|
return $next($request);
|
|
|
|
}
|
2019-12-12 18:52:33 +08:00
|
|
|
|
2019-12-14 22:54:41 +08:00
|
|
|
protected function parseRule($rule)
|
2019-12-12 18:52:33 +08:00
|
|
|
{
|
|
|
|
[$controller, $action] = explode(Str::contains($rule, '@') ? '@' : '/', $rule);
|
|
|
|
|
|
|
|
$controller = explode('\\', $controller);
|
|
|
|
|
|
|
|
$controllerName = strtolower(array_pop($controller));
|
|
|
|
|
|
|
|
array_pop($controller);
|
|
|
|
|
|
|
|
$module = array_pop($controller);
|
2019-12-14 22:54:41 +08:00
|
|
|
|
|
|
|
return [$module, $controllerName, $action];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月14日
|
|
|
|
* @param $module
|
|
|
|
* @param $controllerName
|
|
|
|
* @param $action
|
|
|
|
* @param $request
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
* @return array|bool|\think\Model|null
|
|
|
|
*/
|
2020-01-17 11:29:33 +08:00
|
|
|
protected function getPermission($module, $controllerName, $action)
|
2019-12-14 22:54:41 +08:00
|
|
|
{
|
2019-12-13 17:26:09 +08:00
|
|
|
$permissionMark = sprintf('%s:%s', $controllerName, $action);
|
2019-12-12 22:33:58 +08:00
|
|
|
|
2020-01-17 11:29:33 +08:00
|
|
|
$permission = Permissions::where('module', $module)->where('permission_mark', $permissionMark)->find();
|
2019-12-13 17:26:09 +08:00
|
|
|
|
2019-12-14 22:54:41 +08:00
|
|
|
return $permission;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function ignoreModule()
|
|
|
|
{
|
|
|
|
return ['login'];
|
2019-12-12 18:52:33 +08:00
|
|
|
}
|
2019-12-26 09:03:09 +08:00
|
|
|
}
|