RoleService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2024/8/18 22:44
  7. */
  8. namespace App\Http\Services\Backend\Server\System;
  9. use App\Enums\System\Role\DataScope;
  10. use App\Exceptions\ApiException;
  11. use App\Http\Services\Service;
  12. use App\Models\System\DictType;
  13. use App\Models\System\Menu;
  14. use App\Models\System\Role;
  15. use Ramsey\Uuid\Type\Integer;
  16. use Spatie\Permission\Guard;
  17. use Symfony\Component\HttpFoundation\Response;
  18. class RoleService extends Service
  19. {
  20. protected array $selectColumn = ['id', 'name', 'guard_name as guardName', 'code', 'sort', 'status', 'type', 'remark'];
  21. protected array $appendColumn = ['created_at as createTime'];
  22. public function getRoleList($data): array
  23. {
  24. $role = Role::query();
  25. isset($data['name']) && $role->whereLike('name', "%{$data['name']}%");
  26. isset($data['status']) && $role->where('status', $data['status']);
  27. $rolePage = $role->select([...$this->selectColumn, ...$this->appendColumn])->orderBy('sort')->paginate($data['pageSize'], ['*'], 'page', $data['pageNo']);
  28. return ['list' => $rolePage->items(), 'total' => $rolePage->total()];
  29. }
  30. public function getRole(int $id): array
  31. {
  32. return Role::query()->where('guard_name', Guard::getDefaultName(static::class))->select($this->selectColumn)->find($id)->toArray();
  33. }
  34. /**
  35. * @throws ApiException
  36. */
  37. public function createRole($params)
  38. {
  39. $params['guard_name'] = $params['guard_name'] ?? Guard::getDefaultName(static::class);
  40. // 1. 校验角色
  41. self::validateRoleDuplicate($params['name'], $params['code'], $params['guard_name']);;
  42. // $params['data_scope'] = DataScope::ALL;
  43. $params['type'] = $params['type'] ?? 1;
  44. // 2. 插入到数据库
  45. $role = self::toModel($params, Role::class);
  46. return $role->create($role->getAttributes())->id;
  47. // 3. 记录操作日志上下文
  48. }
  49. /**
  50. * @throws ApiException
  51. */
  52. public function updateRole(array $params, int $id): bool|int
  53. {
  54. $params['guard_name'] = $params['guard_name'] ?? Guard::getDefaultName(static::class);
  55. // 校验是否可以更新
  56. // $role = self::validateRoleForUpdate($id);
  57. // 校验角色名称是否重复
  58. self::validateRoleDuplicate($params['name'], $params['guard_name'], $id);
  59. // 更新到数据库
  60. $role = self::toModel([...$params, 'id' => $id], Role::class);
  61. return $role->update($role->getAttributes());
  62. // 记录操作日志上下文
  63. }
  64. public function deleteRole($id): ?bool
  65. {
  66. // 1. 校验是否可以更新
  67. // $role = self::validateRoleForUpdate($id);
  68. $role = self::toModel(['id' => $id], Role::class);
  69. // 2.1 标记删除
  70. // 2.2 删除相关数据
  71. // permissionService.processRoleDeleted(id);
  72. $role->syncPermissions([]);
  73. $role->flushPermissionCache();
  74. // 3. 记录操作日志上下文
  75. // LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(role, RoleSaveReqVO.class));
  76. // LogRecordContext.putVariable("role", role);
  77. return $role->delete();
  78. }
  79. // public function getRolePermissions($id)
  80. // {
  81. // $role = Role::findById($id);
  82. // return $role->getAllPermissions()->pluck('id');
  83. // }
  84. /**
  85. * 校验角色的唯一字段是否重复
  86. *
  87. * 1. 是否存在相同名字的角色
  88. * 2. 是否存在相同编码的角色
  89. *
  90. * @param String $name 角色名字
  91. * @param String $guard_name
  92. * @param int $id 角色编号
  93. * @throws ApiException
  94. */
  95. public static function validateRoleDuplicate(string $name, string $code, string $guard_name, int $id = 0): void
  96. {
  97. // 0. 超级管理员,不允许创建
  98. // if (RoleCodeEnum.isSuperAdmin(code)) {
  99. // throw exception(ROLE_ADMIN_CODE_ERROR, code);
  100. // }
  101. // 1. 该 name 名字被其它角色所使用
  102. $role = self::selectByName($name, $guard_name);
  103. $role && $role->id !== $id && self::error('ROLE_NAME_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
  104. $role = self::selectByCode($code, $guard_name);
  105. $role && $role->id !== $id && self::error('ROLE_CODE_DUPLICATE', Response::HTTP_UNPROCESSABLE_ENTITY);
  106. }
  107. public static function validateRoleForUpdate($id): Role
  108. {
  109. $role = Role::findById($id);
  110. // throw exception(ROLE_NOT_EXISTS);
  111. // 内置角色,不允许删除
  112. // if (RoleTypeEnum.SYSTEM.getType().equals(role.getType())) {
  113. // throw exception(ROLE_CAN_NOT_UPDATE_SYSTEM_TYPE_ROLE);
  114. // }
  115. return $role;
  116. }
  117. protected static function selectByName($name, $guard_name = null)
  118. {
  119. $attributes = ['name' => $name];
  120. $attributes['guard_name'] = $guard_name ?? config('auth.defaults.guard');
  121. return Role::query()->where($attributes)->first();
  122. }
  123. protected static function selectByCode($code, $guard_name = null)
  124. {
  125. $attributes = ['code' => $code];
  126. $attributes['guard_name'] = $guard_name ?? config('auth.defaults.guard');
  127. return Role::query()->where($attributes)->first();
  128. }
  129. }