DistAuth.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use Dcat\Admin\Admin;
  5. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  6. class DistAuth
  7. {
  8. private $excludeList = [
  9. '/auth/users',
  10. '/auth/roles',
  11. '/auth/permissions',
  12. '/auth/menu',
  13. '/auth/extensions',
  14. '/helpers/scaffold',
  15. '/helpers/icons',
  16. ];
  17. public function handle($request, Closure $next)
  18. {
  19. //如果用户非管理员角色,判断是否含以上URL,含有则触发404
  20. foreach ($this->excludeList as $item) {
  21. if (strpos($request->url(), $item) !== false) {
  22. if (!Admin::user()->isAdministrator()) {
  23. throw new NotFoundHttpException; // 触发404
  24. }
  25. }
  26. }
  27. //判断是否登录,如果getDistributor不存在,则触发登录页面
  28. if (Admin::user()) {
  29. if (!getDistributor()) {
  30. if (strpos($request->url(), 'auth/logout') == false) {
  31. // 存在时的逻辑
  32. return redirect('/dist/auth/logout');
  33. }
  34. }
  35. }
  36. //否则继续处理当前请求
  37. return $next($request);
  38. }
  39. }