Auth.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\service;
  13. use app\store\model\store\Api as ApiModel;
  14. use app\store\model\store\MenuApi as MenuApiModel;
  15. use app\store\model\store\UserRole as UserRoleModel;
  16. use app\store\model\store\RoleMenu as RoleMenuModel;
  17. use app\store\service\store\User as StoreUserService;
  18. /**
  19. * 商家后台权限业务
  20. * Class Auth
  21. * @package app\admin\service
  22. */
  23. class Auth
  24. {
  25. // 实例句柄
  26. static public $instance;
  27. // 商家登录信息
  28. private $store;
  29. // 商家用户信息
  30. private $user;
  31. // 商家用户权限url
  32. private array $apiUrls = [];
  33. /**
  34. * 公有化获取实例方法
  35. * @return Auth
  36. */
  37. public static function getInstance(): self
  38. {
  39. if (!(self::$instance instanceof Auth)) {
  40. self::$instance = new self;
  41. }
  42. return self::$instance;
  43. }
  44. /**
  45. * 私有化构造方法
  46. * Auth constructor.
  47. */
  48. private function __construct()
  49. {
  50. // 商家登录信息
  51. $this->store = StoreUserService::getLoginInfo();
  52. // 当前用户信息
  53. !empty($this->store) && $this->user = $this->store['user'];
  54. }
  55. /**
  56. * 私有化克隆方法
  57. */
  58. private function __clone()
  59. {
  60. }
  61. /**
  62. * 验证指定url是否有访问权限
  63. * @param $url
  64. * @param bool $strict 严格模式($url必须全部有权)
  65. * @return bool
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function checkPrivilege($url, bool $strict = true): bool
  71. {
  72. if (!is_array($url)) {
  73. return $this->checkAccess($url);
  74. }
  75. foreach ($url as $val) {
  76. $status = $this->checkAccess($val);
  77. if ($strict && !$status) return false;
  78. if (!$strict && $status) return true;
  79. }
  80. return true;
  81. }
  82. /**
  83. * 验证url的权限
  84. * @param $url
  85. * @return bool
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. private function checkAccess($url): bool
  91. {
  92. // 域名白名单
  93. // config/allowapi.php
  94. $allowApis = config('allowapi');
  95. // 验证当前请求是否在白名单
  96. if (in_array($url, $allowApis)) {
  97. return true;
  98. }
  99. // 用户不存在 禁止访问
  100. if (empty($this->user)) {
  101. return false;
  102. }
  103. // 超级管理员无需验证
  104. if ($this->user['is_super']) {
  105. return true;
  106. }
  107. // 通配符支持
  108. foreach ($allowApis as $action) {
  109. if (strpos($action, '*') !== false
  110. && preg_match('/^' . str_replace('/', '\/', $action) . '/', $url)
  111. ) {
  112. return true;
  113. }
  114. }
  115. // 获取当前用户的权限url列表
  116. if (!in_array($url, $this->getAccessUrls())) {
  117. return false;
  118. }
  119. return true;
  120. }
  121. /**
  122. * 获取当前用户的权限url列表
  123. * @return array
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\DbException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. */
  128. private function getAccessUrls(): array
  129. {
  130. if (empty($this->apiUrls)) {
  131. // 获取当前用户的角色ID集
  132. $roleIds = UserRoleModel::getRoleIdsByUserId($this->user['store_user_id']);
  133. // 获取已分配的菜单ID集
  134. $menuIds = RoleMenuModel::getMenuIds($roleIds);
  135. // 获取已分配的API的ID集
  136. $apiIds = MenuApiModel::getApiIds($menuIds);
  137. // 获取当前角色所有权限链接
  138. $this->apiUrls = ApiModel::getApiUrls($apiIds);
  139. }
  140. return $this->apiUrls;
  141. }
  142. }