123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace App\Models\System;
- use Illuminate\Support\Facades\DB;
- use Spatie\Permission\Models\Role as PermissionRole;
- use Spatie\Permission\Traits\HasPermissions;
- use Spatie\Permission\Traits\RefreshesPermissionCache;
- class Role extends PermissionRole
- {
- use HasPermissions;
- use RefreshesPermissionCache;
- public static function getMenus(array $role_ids)
- {
- $menu_ids = DB::table('system_role_has_menus')->whereIn('role_id',$role_ids)->pluck('menu_id')->all();
- return DB::table('system_menus')->whereIn('id',$menu_ids)->where('status', 0)->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);
- // }
- }
|