| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace App\Module\OpenAPI\Validators;
- use App\Module\OpenAPI\Models\OpenApiApp;
- use App\Module\OpenAPI\Enums\SCOPE_TYPE;
- use UCore\Validator;
- /**
- * 权限范围验证器
- */
- class ScopePermissionValidator extends Validator
- {
- /**
- * 验证权限范围是否有效
- *
- * @param mixed $value 权限范围字符串(空格分隔)
- * @param array $data 验证数据
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- // 从 args 获取参数
- $appKey = $this->args[0] ?? 'app';
- $scopesKey = $this->args[1] ?? 'scopes';
- /** @var OpenApiApp|null $app */
- $app = $this->validation->$appKey ?? null;
- if (!$app) {
- $this->addError('应用信息不存在,请先验证应用');
- return false;
- }
- // 解析请求的权限范围
- $requestedScopes = $this->parseScopes($value);
-
- // 如果没有请求权限,使用默认权限
- if (empty($requestedScopes)) {
- $requestedScopes = $this->getDefaultScopes();
- }
- // 验证权限范围格式
- if (!$this->validateScopeFormat($requestedScopes)) {
- return false;
- }
- // 验证应用是否拥有请求的权限
- if (!$this->validateAppPermissions($app, $requestedScopes)) {
- return false;
- }
- // 将处理后的权限范围保存到验证对象中
- $this->validation->$scopesKey = $requestedScopes;
- return true;
- }
- /**
- * 解析权限范围字符串
- *
- * @param string $scopeString
- * @return array
- */
- protected function parseScopes(string $scopeString): array
- {
- if (empty($scopeString)) {
- return [];
- }
- // 按空格分割权限范围
- $scopes = array_filter(array_map('trim', explode(' ', $scopeString)));
-
- // 去重并重新索引
- return array_values(array_unique($scopes));
- }
- /**
- * 获取默认权限范围
- *
- * @return array
- */
- protected function getDefaultScopes(): array
- {
- return ['USER_READ', 'GAME_READ'];
- }
- /**
- * 验证权限范围格式
- *
- * @param array $scopes
- * @return bool
- */
- protected function validateScopeFormat(array $scopes): bool
- {
- $validScopes = $this->getAllValidScopes();
- foreach ($scopes as $scope) {
- if (!in_array($scope, $validScopes)) {
- $this->addError("无效的权限范围: {$scope}");
- return false;
- }
- }
- return true;
- }
- /**
- * 验证应用是否拥有请求的权限
- *
- * @param OpenApiApp $app
- * @param array $requestedScopes
- * @return bool
- */
- protected function validateAppPermissions(OpenApiApp $app, array $requestedScopes): bool
- {
- $appScopes = $app->scopes ?? [];
- if (empty($appScopes)) {
- $this->addError('应用没有配置权限范围');
- return false;
- }
- // 检查是否有管理员权限(拥有所有权限)
- if (in_array('ADMIN', $appScopes) || in_array('*', $appScopes)) {
- return true;
- }
- // 检查每个请求的权限
- foreach ($requestedScopes as $scope) {
- if (!$this->hasScope($appScopes, $scope)) {
- $this->addError("应用缺少必需的权限范围: {$scope}");
- return false;
- }
- }
- return true;
- }
- /**
- * 检查应用是否拥有指定权限
- *
- * @param array $appScopes
- * @param string $requiredScope
- * @return bool
- */
- protected function hasScope(array $appScopes, string $requiredScope): bool
- {
- // 直接拥有权限
- if (in_array($requiredScope, $appScopes)) {
- return true;
- }
- // 检查权限依赖关系
- return $this->checkScopeDependencies($appScopes, $requiredScope);
- }
- /**
- * 检查权限依赖关系
- *
- * @param array $appScopes
- * @param string $requiredScope
- * @return bool
- */
- protected function checkScopeDependencies(array $appScopes, string $requiredScope): bool
- {
- // 权限层级关系
- $hierarchies = [
- 'USER_READ' => ['USER_WRITE', 'USER_DELETE'],
- 'USER_WRITE' => ['USER_DELETE'],
- 'GAME_READ' => ['GAME_WRITE', 'GAME_ADMIN'],
- 'GAME_WRITE' => ['GAME_ADMIN'],
- 'ITEM_READ' => ['ITEM_WRITE', 'ITEM_TRANSFER'],
- 'ITEM_WRITE' => ['ITEM_TRANSFER'],
- 'FUND_READ' => ['FUND_WRITE', 'FUND_TRANSFER'],
- 'FUND_WRITE' => ['FUND_TRANSFER'],
- 'TRADE_READ' => ['TRADE_WRITE', 'TRADE_CANCEL'],
- 'TRADE_WRITE' => ['TRADE_CANCEL'],
- 'STATS_READ' => ['STATS_EXPORT'],
- 'SYSTEM_READ' => ['SYSTEM_ADMIN'],
- ];
- // 检查是否有更高级的权限包含所需权限
- if (isset($hierarchies[$requiredScope])) {
- foreach ($hierarchies[$requiredScope] as $higherScope) {
- if (in_array($higherScope, $appScopes)) {
- return true;
- }
- }
- }
- return false;
- }
- /**
- * 获取所有有效的权限范围
- *
- * @return array
- */
- protected function getAllValidScopes(): array
- {
- $scopes = array_column(SCOPE_TYPE::cases(), 'value');
-
- // 添加特殊权限
- $scopes[] = '*'; // 通配符权限
- $scopes[] = 'ADMIN'; // 管理员权限
- return $scopes;
- }
- }
|