AuthRepository.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace crmeb\repositories;
  3. use app\models\system\SystemRole;
  4. use crmeb\utils\ApiErrorCode;
  5. use crmeb\exceptions\AuthException;
  6. use app\Request;
  7. use think\facade\Cache;
  8. class AuthRepository
  9. {
  10. /**
  11. * 验证权限
  12. * @param Request $request
  13. */
  14. public static function verifiAuth(Request $request)
  15. {
  16. $auth = (new SystemRole())->getRolesByAuth($request->adminInfo()['roles'], 2);
  17. $rule = trim(strtolower($request->rule()->getRule()));
  18. $method = trim(strtolower($request->method()));
  19. if ($rule == 'setting/admin/logout') {
  20. return true;
  21. }
  22. //验证访问接口是否存在
  23. if (!in_array($rule, array_map(function ($item) {
  24. return str_replace(' ', '', $item);
  25. }, array_column($auth, 'api_url')))) {
  26. throw new AuthException(ApiErrorCode::ERR_RULE);
  27. }
  28. //验证访问接口是否有权限
  29. if (empty(array_filter($auth, function ($item) use ($rule, $method) {
  30. if (trim(strtolower($item['api_url'])) === $rule && $method === trim(strtolower($item['methods'])))
  31. return true;
  32. }))) {
  33. throw new AuthException(ApiErrorCode::ERR_AUTH);
  34. }
  35. }
  36. }