RecordOperateMiddleware.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace catchAdmin\permissions\middleware;
  3. use app\Request;
  4. use catchAdmin\permissions\model\Permissions;
  5. use catcher\CatchCacheKeys;
  6. use catcher\Code;
  7. use catcher\exceptions\PermissionForbiddenException;
  8. use think\facade\Cache;
  9. use catcher\Utils;
  10. class RecordOperateMiddleware
  11. {
  12. /**
  13. *
  14. * @time 2019年12月12日
  15. * @param Request $request
  16. * @param \Closure $next
  17. * @return mixed
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. * @throws PermissionForbiddenException
  22. */
  23. public function handle(Request $request, \Closure $next)
  24. {
  25. $rule = $request->rule()->getName();
  26. // 模块忽略
  27. [$module, $controller, $action] = Utils::parseRule($rule);
  28. $permission = $this->getPermission($module, $controller, $action);
  29. $this->operateEvent($request->user()->id, $permission);
  30. // 将权限带入
  31. $request->permission = $permission;
  32. return $next($request);
  33. }
  34. /**
  35. *
  36. * @time 2019年12月14日
  37. * @param $module
  38. * @param $controllerName
  39. * @param $action
  40. * @param $request
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @return array|bool|\think\Model|null
  45. */
  46. protected function getPermission($module, $controllerName, $action)
  47. {
  48. $permissionMark = sprintf('%s@%s', $controllerName, $action);
  49. return Permissions::where('module', $module)->where('permission_mark', $permissionMark)->find();
  50. }
  51. /**
  52. * 操作日志
  53. *
  54. * @time 2020年04月16日
  55. * @param $creatorId
  56. * @param $permission
  57. * @return void
  58. */
  59. protected function operateEvent($creatorId, $permission)
  60. {
  61. // 操作日志
  62. $permission && event('operateLog', [
  63. 'creator_id' => $creatorId,
  64. 'permission' => $permission,
  65. ]);
  66. }
  67. }