OpenApiScope.php 7.7 KB

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