| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- namespace App\Module\OpenAPI\Models;
- use UCore\ModelCore;
- use App\Module\OpenAPI\Enums\SCOPE_TYPE;
- /**
- * OpenAPI权限范围模型
- *
- * field start
- * field end
- */
- class OpenApiScope extends ModelCore
- {
- /**
- * 数据类型转换
- *
- * @var array
- */
- protected $casts = [
- 'is_active' => 'boolean',
- 'granted_at' => 'datetime',
- 'expires_at' => 'datetime',
- ];
- /**
- * 默认值
- *
- * @var array
- */
- protected $attributes = [
- 'is_active' => true,
- ];
- /**
- * 关联应用模型
- *
- * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
- */
- public function app()
- {
- return $this->belongsTo(OpenApiApp::class, 'app_id', 'app_id');
- }
- /**
- * 获取权限类型枚举
- *
- * @return SCOPE_TYPE|null
- */
- public function getScopeTypeAttribute(): ?SCOPE_TYPE
- {
- foreach (SCOPE_TYPE::cases() as $scopeType) {
- if ($scopeType->value === $this->scope) {
- return $scopeType;
- }
- }
- return null;
- }
- /**
- * 获取风险级别标签
- *
- * @return string
- */
- public function getRiskLevelLabelAttribute(): string
- {
- return match ($this->risk_level) {
- 'LOW' => '低风险',
- 'MEDIUM' => '中风险',
- 'HIGH' => '高风险',
- 'CRITICAL' => '极高风险',
- default => $this->risk_level,
- };
- }
- /**
- * 获取风险级别颜色
- *
- * @return string
- */
- public function getRiskLevelColorAttribute(): string
- {
- return match ($this->risk_level) {
- 'LOW' => 'success',
- 'MEDIUM' => 'warning',
- 'HIGH' => 'danger',
- 'CRITICAL' => 'dark',
- default => 'secondary',
- };
- }
- /**
- * 获取分类标签
- *
- * @return string
- */
- public function getCategoryLabelAttribute(): string
- {
- return match ($this->category) {
- 'USER' => '用户管理',
- 'GAME' => '游戏数据',
- 'ITEM' => '物品管理',
- 'FUND' => '资金管理',
- 'TRADE' => '交易管理',
- 'ADMIN' => '系统管理',
- 'SPECIAL' => '特殊权限',
- default => $this->category
- };
- }
- /**
- * 获取状态标签
- *
- * @return string
- */
- public function getStatusLabelAttribute(): string
- {
- if (!$this->is_active) {
- return '已停用';
- }
- if ($this->expires_at && $this->expires_at->isPast()) {
- return '已过期';
- }
- return '正常';
- }
- /**
- * 获取状态颜色
- *
- * @return string
- */
- public function getStatusColorAttribute(): string
- {
- if (!$this->is_active) {
- return 'warning';
- }
- if ($this->expires_at && $this->expires_at->isPast()) {
- return 'danger';
- }
- return 'success';
- }
- /**
- * 检查权限是否有效
- *
- * @return bool
- */
- public function isValid(): bool
- {
- if (!$this->is_active) {
- return false;
- }
- if ($this->expires_at && $this->expires_at->isPast()) {
- return false;
- }
- return true;
- }
- /**
- * 检查权限是否即将过期
- *
- * @param int $days 提前天数
- * @return bool
- */
- public function isExpiringSoon(int $days = 7): bool
- {
- if (!$this->expires_at) {
- return false;
- }
- return $this->expires_at->isBefore(now()->addDays($days));
- }
- /**
- * 激活权限
- *
- * @return void
- */
- public function activate(): void
- {
- $this->update([
- 'is_active' => true,
- 'granted_at' => now(),
- ]);
- }
- /**
- * 停用权限
- *
- * @return void
- */
- public function deactivate(): void
- {
- $this->update(['is_active' => false]);
- }
- /**
- * 延长权限有效期
- *
- * @param int $days
- * @return void
- */
- public function extend(int $days): void
- {
- $newExpiresAt = $this->expires_at
- ? $this->expires_at->addDays($days)
- : now()->addDays($days);
- $this->update(['expires_at' => $newExpiresAt]);
- }
- /**
- * 按应用ID查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $appId
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByApp($query, string $appId)
- {
- return $query->where('app_id', $appId);
- }
- /**
- * 按权限范围查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $scope
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByScope($query, string $scope)
- {
- return $query->where('scope', $scope);
- }
- /**
- * 查询激活的权限
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeActive($query)
- {
- return $query->where('is_active', true);
- }
- /**
- * 查询有效的权限(激活且未过期)
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeValid($query)
- {
- return $query->where('is_active', true)
- ->where(function ($q) {
- $q->whereNull('expires_at')
- ->orWhere('expires_at', '>', now());
- });
- }
- /**
- * 查询过期的权限
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeExpired($query)
- {
- return $query->where('expires_at', '<', now());
- }
- /**
- * 按风险级别查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $riskLevel
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByRiskLevel($query, string $riskLevel)
- {
- return $query->where('risk_level', $riskLevel);
- }
- /**
- * 按分类查询
- *
- * @param \Illuminate\Database\Eloquent\Builder $query
- * @param string $category
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function scopeByCategory($query, string $category)
- {
- return $query->where('category', $category);
- }
- }
|