AdminAuthMiddleware.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace app\admin\middleware;
  3. use app\admin\model\SystemMenu;
  4. use app\admin\model\SystemRole;
  5. use Webman\Http\Request;
  6. use Webman\Http\Response;
  7. use Webman\MiddlewareInterface;
  8. class AdminAuthMiddleware implements MiddlewareInterface
  9. {
  10. public function process(Request $request, callable $handler) : Response
  11. {
  12. try {
  13. $admin_info = $request->admin_info;
  14. //非超级管理员 鉴权处理
  15. if($admin_info->admin_is_boos != 1)
  16. {
  17. if(empty($admin_info->admin_roles))
  18. {
  19. throw new \Exception('管理员暂未配置权限');
  20. }
  21. $rules = SystemRole::getRuleList(explode(',', $admin_info->admin_roles));
  22. if(empty($rules))
  23. {
  24. throw new \Exception('管理员暂未配置角色');
  25. }
  26. $menu_list_ids = array();
  27. foreach ($rules as $row)
  28. {
  29. $menu_list_ids = empty($menu_list_ids) ? explode(',', $row['role_rules']) : array_merge($menu_list_ids, explode(',', $row['role_rules']));
  30. }
  31. if(empty($menu_list_ids))
  32. {
  33. throw new \Exception('没有权限访问1');
  34. }
  35. $menu_list = array();
  36. $menu_list = SystemMenu::getMenuListRule(array_unique($menu_list_ids));
  37. if(empty($menu_list))
  38. {
  39. throw new \Exception('没有权限访问2');
  40. }
  41. $rule_path = $request->path();
  42. if(!in_array($rule_path, array_column($menu_list, 'menu_route')))
  43. {
  44. throw new \Exception(json_encode($menu_list));
  45. }
  46. }
  47. return $handler($request);
  48. }catch (\Exception $exception){
  49. throw new \Exception($exception->getMessage(), $exception->getCode());
  50. }
  51. }
  52. }