Role.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Models\System;
  3. use App\Enums\Common\Status;
  4. use Illuminate\Support\Facades\DB;
  5. use Spatie\Permission\Models\Role as PermissionRole;
  6. use Spatie\Permission\Traits\HasPermissions;
  7. use Spatie\Permission\Traits\RefreshesPermissionCache;
  8. class Role extends PermissionRole
  9. {
  10. use HasPermissions;
  11. use RefreshesPermissionCache;
  12. public static function getMenus(array $role_ids): \Illuminate\Support\Collection
  13. {
  14. $tableNames = config('permission.table_names');
  15. $menu_ids = DB::table($tableNames['model_has_roles'])->whereIn('role_id', $role_ids)->pluck('model_id')->all();
  16. return DB::table('system_menus')->whereIn('id', $menu_ids)->where('status', Status::ENABLE)->get();
  17. }
  18. // public function __construct(array $attributes = [])
  19. // {
  20. // parent::__construct($attributes);
  21. // }
  22. //
  23. // /**
  24. // * @throws ApiException
  25. // */
  26. // public static function create(array $attributes = [])
  27. // {
  28. // // 校验角色的唯一字段是否重复
  29. // $role = self::getRoleByParam($attributes);
  30. // $role && throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'ROLE_CODE_DUPLICATE']);
  31. // return static::query()->create($attributes);
  32. // }
  33. //
  34. // /**
  35. // * @throws ApiException
  36. // */
  37. // public function update(array $attributes = [], array $options = []): bool|int
  38. // {
  39. // // 校验角色的唯一字段是否重复
  40. // $role = self::getRoleByParam($attributes);
  41. // $role && $role->id !== $attributes['id'] && throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'ROLE_CODE_DUPLICATE']);
  42. // // 获取角色的所有权限
  43. // $permissions = $role->getPermissionNames();
  44. // // 存在权限并清空权限缓存
  45. // count($permissions) && Cache::forget(config('permission.cache.key'));
  46. // return static::query()->where('id', $attributes['id'])->update($attributes, $options);
  47. // }
  48. //
  49. // private static function getRoleByParam(array &$attributes = []): PermissionRole|null
  50. // {
  51. // // 校验角色的唯一字段是否重复
  52. // $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
  53. // $params = ['code' => $attributes['code'], 'guard_name' => $attributes['guard_name']];
  54. // if (app(PermissionRegistrar::class)->teams) {
  55. // $teamsKey = app(PermissionRegistrar::class)->teamsKey;
  56. // if (array_key_exists($teamsKey, $attributes)) {
  57. // $params[$teamsKey] = $attributes[$teamsKey];
  58. // } else {
  59. // $attributes[$teamsKey] = getPermissionsTeamId();
  60. // }
  61. // }
  62. // return static::findByParam($params);
  63. // }
  64. }