ScopePermissionValidator.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace App\Module\OpenAPI\Validators;
  3. use App\Module\OpenAPI\Models\OpenApiApp;
  4. use App\Module\OpenAPI\Enums\SCOPE_TYPE;
  5. use UCore\Validator;
  6. /**
  7. * 权限范围验证器
  8. */
  9. class ScopePermissionValidator extends Validator
  10. {
  11. /**
  12. * 验证权限范围是否有效
  13. *
  14. * @param mixed $value 权限范围字符串(空格分隔)
  15. * @param array $data 验证数据
  16. * @return bool 验证是否通过
  17. */
  18. public function validate(mixed $value, array $data): bool
  19. {
  20. // 从 args 获取参数
  21. $appKey = $this->args[0] ?? 'app';
  22. $scopesKey = $this->args[1] ?? 'scopes';
  23. /** @var OpenApiApp|null $app */
  24. $app = $this->validation->$appKey ?? null;
  25. if (!$app) {
  26. $this->addError('应用信息不存在,请先验证应用');
  27. return false;
  28. }
  29. // 解析请求的权限范围
  30. $requestedScopes = $this->parseScopes($value);
  31. // 如果没有请求权限,使用默认权限
  32. if (empty($requestedScopes)) {
  33. $requestedScopes = $this->getDefaultScopes();
  34. }
  35. // 验证权限范围格式
  36. if (!$this->validateScopeFormat($requestedScopes)) {
  37. return false;
  38. }
  39. // 验证应用是否拥有请求的权限
  40. if (!$this->validateAppPermissions($app, $requestedScopes)) {
  41. return false;
  42. }
  43. // 将处理后的权限范围保存到验证对象中
  44. $this->validation->$scopesKey = $requestedScopes;
  45. return true;
  46. }
  47. /**
  48. * 解析权限范围字符串
  49. *
  50. * @param string $scopeString
  51. * @return array
  52. */
  53. protected function parseScopes(string $scopeString): array
  54. {
  55. if (empty($scopeString)) {
  56. return [];
  57. }
  58. // 按空格分割权限范围
  59. $scopes = array_filter(array_map('trim', explode(' ', $scopeString)));
  60. // 去重并重新索引
  61. return array_values(array_unique($scopes));
  62. }
  63. /**
  64. * 获取默认权限范围
  65. *
  66. * @return array
  67. */
  68. protected function getDefaultScopes(): array
  69. {
  70. return ['USER_READ', 'GAME_READ'];
  71. }
  72. /**
  73. * 验证权限范围格式
  74. *
  75. * @param array $scopes
  76. * @return bool
  77. */
  78. protected function validateScopeFormat(array $scopes): bool
  79. {
  80. $validScopes = $this->getAllValidScopes();
  81. foreach ($scopes as $scope) {
  82. if (!in_array($scope, $validScopes)) {
  83. $this->addError("无效的权限范围: {$scope}");
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89. /**
  90. * 验证应用是否拥有请求的权限
  91. *
  92. * @param OpenApiApp $app
  93. * @param array $requestedScopes
  94. * @return bool
  95. */
  96. protected function validateAppPermissions(OpenApiApp $app, array $requestedScopes): bool
  97. {
  98. $appScopes = $app->scopes ?? [];
  99. if (empty($appScopes)) {
  100. $this->addError('应用没有配置权限范围');
  101. return false;
  102. }
  103. // 检查是否有管理员权限(拥有所有权限)
  104. if (in_array('ADMIN', $appScopes) || in_array('*', $appScopes)) {
  105. return true;
  106. }
  107. // 检查每个请求的权限
  108. foreach ($requestedScopes as $scope) {
  109. if (!$this->hasScope($appScopes, $scope)) {
  110. $this->addError("应用缺少必需的权限范围: {$scope}");
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. /**
  117. * 检查应用是否拥有指定权限
  118. *
  119. * @param array $appScopes
  120. * @param string $requiredScope
  121. * @return bool
  122. */
  123. protected function hasScope(array $appScopes, string $requiredScope): bool
  124. {
  125. // 直接拥有权限
  126. if (in_array($requiredScope, $appScopes)) {
  127. return true;
  128. }
  129. // 检查权限依赖关系
  130. return $this->checkScopeDependencies($appScopes, $requiredScope);
  131. }
  132. /**
  133. * 检查权限依赖关系
  134. *
  135. * @param array $appScopes
  136. * @param string $requiredScope
  137. * @return bool
  138. */
  139. protected function checkScopeDependencies(array $appScopes, string $requiredScope): bool
  140. {
  141. // 权限层级关系
  142. $hierarchies = [
  143. 'USER_READ' => ['USER_WRITE', 'USER_DELETE'],
  144. 'USER_WRITE' => ['USER_DELETE'],
  145. 'GAME_READ' => ['GAME_WRITE', 'GAME_ADMIN'],
  146. 'GAME_WRITE' => ['GAME_ADMIN'],
  147. 'ITEM_READ' => ['ITEM_WRITE', 'ITEM_TRANSFER'],
  148. 'ITEM_WRITE' => ['ITEM_TRANSFER'],
  149. 'FUND_READ' => ['FUND_WRITE', 'FUND_TRANSFER'],
  150. 'FUND_WRITE' => ['FUND_TRANSFER'],
  151. 'TRADE_READ' => ['TRADE_WRITE', 'TRADE_CANCEL'],
  152. 'TRADE_WRITE' => ['TRADE_CANCEL'],
  153. 'STATS_READ' => ['STATS_EXPORT'],
  154. 'SYSTEM_READ' => ['SYSTEM_ADMIN'],
  155. ];
  156. // 检查是否有更高级的权限包含所需权限
  157. if (isset($hierarchies[$requiredScope])) {
  158. foreach ($hierarchies[$requiredScope] as $higherScope) {
  159. if (in_array($higherScope, $appScopes)) {
  160. return true;
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. /**
  167. * 获取所有有效的权限范围
  168. *
  169. * @return array
  170. */
  171. protected function getAllValidScopes(): array
  172. {
  173. $scopes = array_column(SCOPE_TYPE::cases(), 'value');
  174. // 添加特殊权限
  175. $scopes[] = '*'; // 通配符权限
  176. $scopes[] = 'ADMIN'; // 管理员权限
  177. return $scopes;
  178. }
  179. }