123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Models\System;
- use App\Enums\Common\Status;
- use Illuminate\Support\Facades\DB;
- use Spatie\Permission\Models\Role as PermissionRole;
- use Spatie\Permission\PermissionRegistrar;
- class Role extends PermissionRole
- {
- public function menus(): \Illuminate\Database\Eloquent\Relations\MorphToMany
- {
- return $this->morphedByMany(
- Menu::class,
- 'model',
- config('permission.table_names.model_has_roles'),
- app(PermissionRegistrar::class)->pivotRole,
- config('permission.column_names.model_morph_key')
- );
- }
- public static function getMenus(array $role_ids): \Illuminate\Support\Collection
- {
- $tableNames = config('permission.table_names');
- $menu_ids = DB::table($tableNames['model_has_roles'])->where('model_type', Menu::class)->whereIn('role_id', $role_ids)->pluck('model_id')->all();
- return DB::table('system_menus')->whereIn('id', $menu_ids)->where('status', Status::ENABLE)->get();
- }
- // public function __construct(array $attributes = [])
- // {
- // parent::__construct($attributes);
- // }
- //
- // /**
- // * @throws ApiException
- // */
- // public static function create(array $attributes = [])
- // {
- // // 校验角色的唯一字段是否重复
- // $role = self::getRoleByParam($attributes);
- // $role && throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'ROLE_CODE_DUPLICATE']);
- // return static::query()->create($attributes);
- // }
- //
- // /**
- // * @throws ApiException
- // */
- // public function update(array $attributes = [], array $options = []): bool|int
- // {
- // // 校验角色的唯一字段是否重复
- // $role = self::getRoleByParam($attributes);
- // $role && $role->id !== $attributes['id'] && throw new ApiException(['code' => Response::HTTP_UNPROCESSABLE_ENTITY, 'message' => 'ROLE_CODE_DUPLICATE']);
- // // 获取角色的所有权限
- // $permissions = $role->getPermissionNames();
- // // 存在权限并清空权限缓存
- // count($permissions) && Cache::forget(config('permission.cache.key'));
- // return static::query()->where('id', $attributes['id'])->update($attributes, $options);
- // }
- //
- // private static function getRoleByParam(array &$attributes = []): PermissionRole|null
- // {
- // // 校验角色的唯一字段是否重复
- // $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
- // $params = ['code' => $attributes['code'], 'guard_name' => $attributes['guard_name']];
- // if (app(PermissionRegistrar::class)->teams) {
- // $teamsKey = app(PermissionRegistrar::class)->teamsKey;
- // if (array_key_exists($teamsKey, $attributes)) {
- // $params[$teamsKey] = $attributes[$teamsKey];
- // } else {
- // $attributes[$teamsKey] = getPermissionsTeamId();
- // }
- // }
- // return static::findByParam($params);
- // }
- }
|