OpenApiScope.php 6.5 KB

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