OpenApiScope.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace App\Module\OpenAPI\Models;
  3. use UCore\ModelCore;
  4. use App\Module\OpenAPI\Enums\SCOPE_TYPE;
  5. /**
  6. * OpenAPI权限范围模型
  7. *
  8. * @property int $id
  9. * @property string $app_id 应用ID
  10. * @property string $scope 权限范围
  11. * @property string $name 权限名称
  12. * @property string $description 权限描述
  13. * @property string $category 权限分类
  14. * @property string $risk_level 风险级别
  15. * @property bool $is_active 是否激活
  16. * @property \Carbon\Carbon|null $granted_at 授权时间
  17. * @property \Carbon\Carbon|null $expires_at 过期时间
  18. * @property string|null $granted_by 授权人
  19. * @property string|null $notes 备注
  20. * @property \Carbon\Carbon $created_at
  21. * @property \Carbon\Carbon $updated_at
  22. */
  23. class OpenApiScope extends ModelCore
  24. {
  25. // field start
  26. protected $fillable = [
  27. 'app_id',
  28. 'scope',
  29. 'name',
  30. 'description',
  31. 'category',
  32. 'risk_level',
  33. 'is_active',
  34. 'granted_at',
  35. 'expires_at',
  36. 'granted_by',
  37. 'notes',
  38. ];
  39. // field end
  40. /**
  41. * 数据类型转换
  42. *
  43. * @var array
  44. */
  45. protected $casts = [
  46. 'is_active' => 'boolean',
  47. 'granted_at' => 'datetime',
  48. 'expires_at' => 'datetime',
  49. ];
  50. /**
  51. * 默认值
  52. *
  53. * @var array
  54. */
  55. protected $attributes = [
  56. 'is_active' => true,
  57. ];
  58. /**
  59. * 关联应用模型
  60. *
  61. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  62. */
  63. public function app()
  64. {
  65. return $this->belongsTo(OpenApiApp::class, 'app_id', 'app_id');
  66. }
  67. /**
  68. * 获取权限类型枚举
  69. *
  70. * @return SCOPE_TYPE|null
  71. */
  72. public function getScopeTypeAttribute(): ?SCOPE_TYPE
  73. {
  74. foreach (SCOPE_TYPE::cases() as $scopeType) {
  75. if ($scopeType->value === $this->scope) {
  76. return $scopeType;
  77. }
  78. }
  79. return null;
  80. }
  81. /**
  82. * 获取风险级别标签
  83. *
  84. * @return string
  85. */
  86. public function getRiskLevelLabelAttribute(): string
  87. {
  88. return match ($this->risk_level) {
  89. 'LOW' => '低风险',
  90. 'MEDIUM' => '中风险',
  91. 'HIGH' => '高风险',
  92. 'CRITICAL' => '极高风险',
  93. default => $this->risk_level,
  94. };
  95. }
  96. /**
  97. * 获取风险级别颜色
  98. *
  99. * @return string
  100. */
  101. public function getRiskLevelColorAttribute(): string
  102. {
  103. return match ($this->risk_level) {
  104. 'LOW' => 'success',
  105. 'MEDIUM' => 'warning',
  106. 'HIGH' => 'danger',
  107. 'CRITICAL' => 'dark',
  108. default => 'secondary',
  109. };
  110. }
  111. /**
  112. * 获取分类标签
  113. *
  114. * @return string
  115. */
  116. public function getCategoryLabelAttribute(): string
  117. {
  118. return match ($this->category) {
  119. 'USER' => '用户管理',
  120. 'GAME' => '游戏数据',
  121. 'ITEM' => '物品管理',
  122. 'FUND' => '资金管理',
  123. 'TRADE' => '交易管理',
  124. 'ADMIN' => '系统管理',
  125. 'SPECIAL' => '特殊权限',
  126. 'default' => $this->category
  127. ];
  128. }
  129. /**
  130. * 获取状态标签
  131. *
  132. * @return string
  133. */
  134. public function getStatusLabelAttribute(): string
  135. {
  136. if (!$this->is_active) {
  137. return '已停用';
  138. }
  139. if ($this->expires_at && $this->expires_at->isPast()) {
  140. return '已过期';
  141. }
  142. return '正常';
  143. }
  144. /**
  145. * 获取状态颜色
  146. *
  147. * @return string
  148. */
  149. public function getStatusColorAttribute(): string
  150. {
  151. if (!$this->is_active) {
  152. return 'warning';
  153. }
  154. if ($this->expires_at && $this->expires_at->isPast()) {
  155. return 'danger';
  156. }
  157. return 'success';
  158. }
  159. /**
  160. * 检查权限是否有效
  161. *
  162. * @return bool
  163. */
  164. public function isValid(): bool
  165. {
  166. if (!$this->is_active) {
  167. return false;
  168. }
  169. if ($this->expires_at && $this->expires_at->isPast()) {
  170. return false;
  171. }
  172. return true;
  173. }
  174. /**
  175. * 检查权限是否即将过期
  176. *
  177. * @param int $days 提前天数
  178. * @return bool
  179. */
  180. public function isExpiringSoon(int $days = 7): bool
  181. {
  182. if (!$this->expires_at) {
  183. return false;
  184. }
  185. return $this->expires_at->isBefore(now()->addDays($days));
  186. }
  187. /**
  188. * 激活权限
  189. *
  190. * @return void
  191. */
  192. public function activate(): void
  193. {
  194. $this->update([
  195. 'is_active' => true,
  196. 'granted_at' => now(),
  197. ]);
  198. }
  199. /**
  200. * 停用权限
  201. *
  202. * @return void
  203. */
  204. public function deactivate(): void
  205. {
  206. $this->update(['is_active' => false]);
  207. }
  208. /**
  209. * 延长权限有效期
  210. *
  211. * @param int $days
  212. * @return void
  213. */
  214. public function extend(int $days): void
  215. {
  216. $newExpiresAt = $this->expires_at
  217. ? $this->expires_at->addDays($days)
  218. : now()->addDays($days);
  219. $this->update(['expires_at' => $newExpiresAt]);
  220. }
  221. /**
  222. * 按应用ID查询
  223. *
  224. * @param \Illuminate\Database\Eloquent\Builder $query
  225. * @param string $appId
  226. * @return \Illuminate\Database\Eloquent\Builder
  227. */
  228. public function scopeByApp($query, string $appId)
  229. {
  230. return $query->where('app_id', $appId);
  231. }
  232. /**
  233. * 按权限范围查询
  234. *
  235. * @param \Illuminate\Database\Eloquent\Builder $query
  236. * @param string $scope
  237. * @return \Illuminate\Database\Eloquent\Builder
  238. */
  239. public function scopeByScope($query, string $scope)
  240. {
  241. return $query->where('scope', $scope);
  242. }
  243. /**
  244. * 查询激活的权限
  245. *
  246. * @param \Illuminate\Database\Eloquent\Builder $query
  247. * @return \Illuminate\Database\Eloquent\Builder
  248. */
  249. public function scopeActive($query)
  250. {
  251. return $query->where('is_active', true);
  252. }
  253. /**
  254. * 查询有效的权限(激活且未过期)
  255. *
  256. * @param \Illuminate\Database\Eloquent\Builder $query
  257. * @return \Illuminate\Database\Eloquent\Builder
  258. */
  259. public function scopeValid($query)
  260. {
  261. return $query->where('is_active', true)
  262. ->where(function ($q) {
  263. $q->whereNull('expires_at')
  264. ->orWhere('expires_at', '>', now());
  265. });
  266. }
  267. /**
  268. * 查询过期的权限
  269. *
  270. * @param \Illuminate\Database\Eloquent\Builder $query
  271. * @return \Illuminate\Database\Eloquent\Builder
  272. */
  273. public function scopeExpired($query)
  274. {
  275. return $query->where('expires_at', '<', now());
  276. }
  277. /**
  278. * 按风险级别查询
  279. *
  280. * @param \Illuminate\Database\Eloquent\Builder $query
  281. * @param string $riskLevel
  282. * @return \Illuminate\Database\Eloquent\Builder
  283. */
  284. public function scopeByRiskLevel($query, string $riskLevel)
  285. {
  286. return $query->where('risk_level', $riskLevel);
  287. }
  288. /**
  289. * 按分类查询
  290. *
  291. * @param \Illuminate\Database\Eloquent\Builder $query
  292. * @param string $category
  293. * @return \Illuminate\Database\Eloquent\Builder
  294. */
  295. public function scopeByCategory($query, string $category)
  296. {
  297. return $query->where('category', $category);
  298. }
  299. }