123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- /**
- * @Name
- * @Description
- * @Author 刘学玺
- * @Date 2024/8/18 22:44
- */
- namespace App\Http\Services\Backend\Server\System;
- use App\Enums\System\Role\DataScope;
- use App\Exceptions\ApiException;
- use App\Http\Services\Service;
- use App\Models\System\DictType;
- use App\Models\System\Menu;
- use App\Models\System\Role;
- use Illuminate\Support\Collection;
- use Ramsey\Uuid\Type\Integer;
- use Spatie\Permission\Guard;
- use Symfony\Component\HttpFoundation\Response;
- class RoleService extends Service
- {
- protected array $simpleColumn = ['id', 'name'];
- protected array $selectColumn = ['id', 'name', 'guard_name as guardName', 'data_scope as dataScope', 'data_scope_dept_ids as dataScopeDeptIds', 'code', 'sort', 'status', 'type', 'remark'];
- protected array $appendColumn = ['created_at as createTime'];
- public function getRoleList($params): array
- {
- $role = Role::query();
- isset($params['name']) && filled($params['name']) && $role->whereLike('name', "%{$params['name']}%");
- isset($params['status']) && filled($params['status']) && $role->where('status', $params['status']);
- isset($params['code']) && filled($params['code']) && $role->whereLike('code', "%{$params['code']}%");
- !empty($params['createTime']) && $role->whereBetween('created_at', $params['createTime']);
- $rolePage = $role->select([...$this->selectColumn, ...$this->appendColumn])->orderBy('sort')->paginate($params['pageSize'], ['*'], 'page', $params['pageNo']);
- return ['list' => $rolePage->items(), 'total' => $rolePage->total()];
- }
- public function getRole(int $id): array
- {
- return Role::query()->where('guard_name', Guard::getDefaultName(static::class))->select($this->selectColumn)->find($id)->toArray();
- }
- /**
- * @throws ApiException
- */
- public function createRole($params)
- {
- $params['guard_name'] = $params['guard_name'] ?? Guard::getDefaultName(static::class);
- // 1. 校验角色
- self::validateRoleDuplicate($params['name'], $params['code'], $params['guard_name']);;
- // $params['data_scope'] = DataScope::ALL;
- $params['type'] = $params['type'] ?? 1;
- // 2. 插入到数据库
- $role = self::toModel($params, Role::class);
- return $role->create($role->getAttributes())->id;
- // 3. 记录操作日志上下文
- }
- /**
- * @throws ApiException
- */
- public function updateRole(array $params, int $id): bool|int
- {
- $params['guard_name'] = $params['guard_name'] ?? Guard::getDefaultName(static::class);
- // 校验是否可以更新
- // $role = self::validateRoleForUpdate($id);
- // 校验角色名称是否重复
- self::validateRoleDuplicate($params['name'], $params['guard_name'], $id);
- // 更新到数据库
- $role = self::toModel([...$params, 'id' => $id], Role::class);
- return $role->update($role->getAttributes());
- // 记录操作日志上下文
- }
- public function deleteRole($id): ?bool
- {
- // 1. 校验是否可以更新
- // $role = self::validateRoleForUpdate($id);
- $role = self::toModel(['id' => $id], Role::class);
- // 2.1 标记删除
- // 2.2 删除相关数据
- // permissionService.processRoleDeleted(id);
- $role->syncPermissions([]);
- $role->flushPermissionCache();
- // 3. 记录操作日志上下文
- // LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(role, RoleSaveReqVO.class));
- // LogRecordContext.putVariable("role", role);
- return $role->delete();
- }
- public function simpleList(): Collection
- {
- return Role::query()->select($this->simpleColumn)->get();
- }
- // public function getRolePermissions($id)
- // {
- // $role = Role::findById($id);
- // return $role->getAllPermissions()->pluck('id');
- // }
- /**
- * 校验角色的唯一字段是否重复
- *
- * 1. 是否存在相同名字的角色
- * 2. 是否存在相同编码的角色
- *
- * @param String $name 角色名字
- * @param String $guard_name
- * @param int $id 角色编号
- * @throws ApiException
- */
- public static function validateRoleDuplicate(string $name, string $code, string $guard_name, int $id = 0): void
- {
- // 0. 超级管理员,不允许创建
- // if (RoleCodeEnum.isSuperAdmin(code)) {
- // throw exception(ROLE_ADMIN_CODE_ERROR, code);
- // }
- // 1. 该 name 名字被其它角色所使用
- $role = self::selectByName($name, $guard_name);
- $role && $role->id !== $id && self::error('ROLE_NAME_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
- $role = self::selectByCode($code, $guard_name);
- $role && $role->id !== $id && self::error('ROLE_CODE_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
- }
- public static function validateRoleForUpdate($id): Role
- {
- $role = Role::findById($id);
- // throw exception(ROLE_NOT_EXISTS);
- // 内置角色,不允许删除
- // if (RoleTypeEnum.SYSTEM.getType().equals(role.getType())) {
- // throw exception(ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
- // }
- return $role;
- }
- protected static function selectByName($name, $guard_name = null)
- {
- $attributes = ['name' => $name];
- $attributes['guard_name'] = $guard_name ?? config('auth.defaults.guard');
- return Role::query()->where($attributes)->first();
- }
- protected static function selectByCode($code, $guard_name = null)
- {
- $attributes = ['code' => $code];
- $attributes['guard_name'] = $guard_name ?? config('auth.defaults.guard');
- return Role::query()->where($attributes)->first();
- }
- }
|