Role.php 3.0 KB

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