From 98c99e05f31630929c4285b714c0ce99c3bf97e1 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Wed, 6 May 2020 17:40:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E6=97=A5=E5=BF=97=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/PermissionsMiddleware.php | 35 ++------- catch/permissions/RecordOperateMiddleware.php | 76 +++++++++++++++++++ 2 files changed, 82 insertions(+), 29 deletions(-) create mode 100644 catch/permissions/RecordOperateMiddleware.php diff --git a/catch/permissions/PermissionsMiddleware.php b/catch/permissions/PermissionsMiddleware.php index 7e25446..05f834c 100644 --- a/catch/permissions/PermissionsMiddleware.php +++ b/catch/permissions/PermissionsMiddleware.php @@ -7,7 +7,7 @@ use catcher\CatchCacheKeys; use catcher\Code; use catcher\exceptions\PermissionForbiddenException; use think\facade\Cache; -use think\helper\Str; +use catcher\Utils; class PermissionsMiddleware { @@ -29,8 +29,9 @@ class PermissionsMiddleware if (!$rule) { return $next($request); } + // 模块忽略 - [$module, $controller, $action] = $this->parseRule($rule); + [$module, $controller, $action] = Utils::parseRule($rule); // toad if (in_array($module, $this->ignoreModule())) { return $next($request); @@ -40,10 +41,6 @@ class PermissionsMiddleware if (!$user) { throw new PermissionForbiddenException('Login is invalid', Code::LOST_LOGIN); } - //dd($this->parseRule($rule)); - $permission = $this->getPermission($module, $controller, $action); - // 记录操作 - $this->operateEvent($request->user()->id, $permission); // 超级管理员 if ($request->user()->id === config('catch.permissions.super_admin_id')) { return $next($request); @@ -52,6 +49,9 @@ class PermissionsMiddleware if ($request->isGet() && config('catch.permissions.is_allow_get')) { return $next($request); } + // 判断权限 + $permission = property_exists($request, 'permission') ? $request->permission : + $this->getPermission($module, $controller, $action); if (!$permission || !in_array($permission->id, Cache::get(CatchCacheKeys::USER_PERMISSIONS . $user->id))) { throw new PermissionForbiddenException(); @@ -60,29 +60,6 @@ class PermissionsMiddleware return $next($request); } - /** - * 解析规则 - * - * @time 2020年04月16日 - * @param $rule - * @return array - */ - protected function parseRule($rule) - { - [$controller, $action] = explode(Str::contains($rule, '@') ? '@' : '/', $rule); - - $controller = explode('\\', $controller); - - $controllerName = strtolower(array_pop($controller)); - - array_pop($controller); - - $module = array_pop($controller); - - return [$module, $controllerName, $action]; - } - - /** * * @time 2019年12月14日 diff --git a/catch/permissions/RecordOperateMiddleware.php b/catch/permissions/RecordOperateMiddleware.php new file mode 100644 index 0000000..c7f1175 --- /dev/null +++ b/catch/permissions/RecordOperateMiddleware.php @@ -0,0 +1,76 @@ +rule()->getName(); + + // 模块忽略 + [$module, $controller, $action] = Utils::parseRule($rule); + + $permission = $this->getPermission($module, $controller, $action); + + $this->operateEvent($request->user()->id, $permission); + + // 将权限带入 + $request->permission = $permission; + return $next($request); + } + + /** + * + * @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 + */ + 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日 + * @param $creatorId + * @param $permission + * @return void + */ + protected function operateEvent($creatorId, $permission) + { + // 操作日志 + $permission && event('operateLog', [ + 'creator_id' => $creatorId, + 'permission' => $permission, + ]); + } +}