2020-04-29 17:37:45 +08:00
|
|
|
<?php
|
2020-06-24 09:09:37 +08:00
|
|
|
namespace catchAdmin\permissions\middleware;
|
2020-04-29 17:37:45 +08:00
|
|
|
|
|
|
|
use app\Request;
|
|
|
|
use catchAdmin\permissions\model\Permissions;
|
|
|
|
use catcher\CatchCacheKeys;
|
|
|
|
use catcher\Code;
|
|
|
|
use catcher\exceptions\PermissionForbiddenException;
|
2021-09-14 14:45:51 +08:00
|
|
|
use Closure;
|
|
|
|
use think\db\exception\DataNotFoundException;
|
|
|
|
use think\db\exception\DbException;
|
|
|
|
use think\db\exception\ModelNotFoundException;
|
2020-04-29 17:37:45 +08:00
|
|
|
use think\facade\Cache;
|
2020-05-06 17:40:51 +08:00
|
|
|
use catcher\Utils;
|
2020-04-29 17:37:45 +08:00
|
|
|
|
|
|
|
class PermissionsMiddleware
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月12日
|
|
|
|
* @param Request $request
|
2021-09-14 14:45:51 +08:00
|
|
|
* @param Closure $next
|
2020-04-29 17:37:45 +08:00
|
|
|
* @return mixed
|
2021-09-14 14:45:51 +08:00
|
|
|
* @throws DataNotFoundException
|
|
|
|
* @throws DbException
|
|
|
|
* @throws ModelNotFoundException
|
|
|
|
* @throws PermissionForbiddenException|\ReflectionException
|
2020-04-29 17:37:45 +08:00
|
|
|
*/
|
2021-09-14 14:45:51 +08:00
|
|
|
public function handle(Request $request, Closure $next)
|
2020-04-29 17:37:45 +08:00
|
|
|
{
|
|
|
|
$rule = $request->rule()->getName();
|
|
|
|
|
|
|
|
if (!$rule) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
2020-05-06 17:40:51 +08:00
|
|
|
|
2020-04-29 17:37:45 +08:00
|
|
|
// 模块忽略
|
2020-05-06 17:40:51 +08:00
|
|
|
[$module, $controller, $action] = Utils::parseRule($rule);
|
2020-10-12 12:22:05 +08:00
|
|
|
|
2020-04-29 17:37:45 +08:00
|
|
|
// toad
|
|
|
|
if (in_array($module, $this->ignoreModule())) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
// 用户未登录
|
|
|
|
$user = $request->user();
|
|
|
|
if (!$user) {
|
|
|
|
throw new PermissionForbiddenException('Login is invalid', Code::LOST_LOGIN);
|
|
|
|
}
|
|
|
|
// 超级管理员
|
2020-10-12 12:22:05 +08:00
|
|
|
if (Utils::isSuperAdmin()) {
|
2020-04-29 17:37:45 +08:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
// Get 请求
|
2020-10-12 12:22:05 +08:00
|
|
|
if ($this->allowGet($request)) {
|
2020-04-29 17:37:45 +08:00
|
|
|
return $next($request);
|
|
|
|
}
|
2020-05-06 17:40:51 +08:00
|
|
|
// 判断权限
|
|
|
|
$permission = property_exists($request, 'permission') ? $request->permission :
|
|
|
|
$this->getPermission($module, $controller, $action);
|
2020-04-29 17:37:45 +08:00
|
|
|
|
2021-09-14 14:45:51 +08:00
|
|
|
$permissionIds = Cache::get(CatchCacheKeys::USER_PERMISSIONS . $user->id);
|
|
|
|
if (!$permission || ! in_array($permission->id, (array)$permissionIds)) {
|
2020-04-29 17:37:45 +08:00
|
|
|
throw new PermissionForbiddenException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2019年12月14日
|
|
|
|
* @param $module
|
|
|
|
* @param $controllerName
|
|
|
|
* @param $action
|
2021-09-14 14:45:51 +08:00
|
|
|
* @return array|\think\Model|null
|
|
|
|
* @throws DbException
|
|
|
|
* @throws ModelNotFoundException
|
|
|
|
* @throws DataNotFoundException
|
2020-04-29 17:37:45 +08:00
|
|
|
*/
|
|
|
|
protected function getPermission($module, $controllerName, $action)
|
|
|
|
{
|
|
|
|
$permissionMark = sprintf('%s@%s', $controllerName, $action);
|
|
|
|
|
|
|
|
return Permissions::where('module', $module)->where('permission_mark', $permissionMark)->find();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 忽略模块
|
|
|
|
*
|
|
|
|
* @time 2020年04月16日
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function ignoreModule()
|
|
|
|
{
|
|
|
|
return ['login'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 操作日志
|
|
|
|
*
|
|
|
|
* @time 2020年04月16日
|
|
|
|
* @param $creatorId
|
|
|
|
* @param $permission
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function operateEvent($creatorId, $permission)
|
|
|
|
{
|
|
|
|
// 操作日志
|
|
|
|
$permission && event('operateLog', [
|
|
|
|
'creator_id' => $creatorId,
|
|
|
|
'permission' => $permission,
|
|
|
|
]);
|
|
|
|
}
|
2020-10-12 12:22:05 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get allow
|
|
|
|
*
|
|
|
|
* @time 2020年10月12日
|
|
|
|
* @param $request
|
|
|
|
* @return bool
|
|
|
|
* @throws \ReflectionException
|
|
|
|
*/
|
|
|
|
protected function allowGet($request)
|
|
|
|
{
|
|
|
|
if (Utils::isMethodNeedAuth($request->rule()->getName())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->isGet() && config('catch.permissions.is_allow_get');
|
|
|
|
}
|
2020-04-29 17:37:45 +08:00
|
|
|
}
|