RoleService.php 5.5 KB

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