role.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from typing import Annotated, List
  4. from pydantic import BeforeValidator, Field
  5. from ruoyi_common.constant import UserConstants
  6. from ruoyi_common.base.transformer import ids_to_list
  7. from ruoyi_common.base.model import AjaxResponse, TableResponse
  8. from ruoyi_common.domain.entity import SysRole
  9. from ruoyi_common.domain.enum import BusinessType
  10. from ruoyi_common.descriptor.serializer import BaseSerializer, JsonSerializer
  11. from ruoyi_common.descriptor.validator import BodyValidator, QueryValidator,PathValidator
  12. from ruoyi_common.utils import security_util as SecurityUtil
  13. from ruoyi_system.domain.entity import SysUserRole
  14. from ruoyi_system.service import SysRoleService,SysUserService
  15. from ruoyi_framework.descriptor.log import Log
  16. from ruoyi_framework.descriptor.permission import HasPerm, PreAuthorize
  17. from ... import reg
  18. @reg.api.route("/system/role/list", methods=["GET"])
  19. @QueryValidator(is_page=True)
  20. @PreAuthorize(HasPerm("system:role:list"))
  21. @JsonSerializer()
  22. def system_role_list(dto:SysRole):
  23. '''
  24. 获取角色列表
  25. '''
  26. rows = SysRoleService.select_role_list(dto)
  27. return TableResponse(rows=rows)
  28. @reg.api.route("/system/role/<int:id>", methods=["GET"])
  29. @PathValidator()
  30. @PreAuthorize(HasPerm("system:role:query"))
  31. @JsonSerializer()
  32. def system_role_detail(id:int):
  33. '''
  34. 获取角色详情
  35. '''
  36. SysRoleService.check_role_data_scope(id)
  37. eo = SysRoleService.select_role_by_id(id)
  38. return AjaxResponse.from_success(data=eo) \
  39. if eo else AjaxResponse.from_error()
  40. @reg.api.route("/system/role/export", methods=["POST"])
  41. @BodyValidator()
  42. @PreAuthorize(HasPerm("system:role:export"))
  43. @Log(title="角色管理",business_type=BusinessType.EXPORT)
  44. @BaseSerializer()
  45. def system_role_export(dto:SysRole):
  46. '''
  47. 导出角色
  48. '''
  49. # todo
  50. rows = SysRoleService.select_role_list(dto)
  51. table_response = TableResponse(rows=rows)
  52. return table_response
  53. @reg.api.route("/system/role", methods=["POST"])
  54. @BodyValidator()
  55. @PreAuthorize(HasPerm("system:role:add"))
  56. @Log(title="角色管理",business_type=BusinessType.INSERT)
  57. @JsonSerializer()
  58. def system_role_create(dto:SysRole):
  59. '''
  60. 创建角色
  61. '''
  62. if UserConstants.NOT_UNIQUE == SysRoleService.check_role_name_unique(dto):
  63. return AjaxResponse.from_error(f"新增角色'{dto.role_name}'失败,角色名称已存在")
  64. elif UserConstants.NOT_UNIQUE == \
  65. SysRoleService.check_role_key_unique(dto):
  66. return AjaxResponse.from_error(f"新增角色'{dto.role_name}'失败,角色权限已存在")
  67. dto.create_by_user(SecurityUtil.get_username())
  68. SysRoleService.insert_role(dto)
  69. return AjaxResponse.from_success()
  70. @reg.api.route("/system/role", methods=["PUT"])
  71. @BodyValidator()
  72. @PreAuthorize(HasPerm("system:role:edit"))
  73. @Log(title="角色管理",business_type=BusinessType.UPDATE)
  74. @JsonSerializer()
  75. def system_role_update(dto:SysRole):
  76. '''
  77. 修改角色
  78. '''
  79. SysRoleService.check_role_allowed(dto)
  80. SysRoleService.check_role_data_scope(dto.role_id)
  81. if UserConstants.NOT_UNIQUE == SysRoleService.check_role_name_unique(dto):
  82. return AjaxResponse.from_error(f"新增角色'{dto.role_name}'失败,角色名称已存在")
  83. elif UserConstants.NOT_UNIQUE == \
  84. SysRoleService.check_role_key_unique(dto):
  85. return AjaxResponse.from_error(f"新增角色'{dto.role_name}'失败,角色权限已存在")
  86. dto.update_by_user(SecurityUtil.get_username())
  87. SysRoleService.update_role(dto)
  88. return AjaxResponse.from_success()
  89. @reg.api.route("/system/role/dataScope", methods=["PUT"])
  90. @BodyValidator()
  91. @PreAuthorize(HasPerm("system:role:edit"))
  92. @Log(title="角色管理",business_type=BusinessType.UPDATE)
  93. @JsonSerializer()
  94. def system_data_scope_update(dto:SysRole):
  95. '''
  96. 修改数据权限
  97. '''
  98. # 校验是否允许操作该角色、以及当前用户是否有该角色的数据权限
  99. SysRoleService.check_role_allowed(dto)
  100. SysRoleService.check_role_data_scope(dto.role_id)
  101. # 记录操作人
  102. dto.update_by_user(SecurityUtil.get_username())
  103. # 保存数据范围与角色-部门关联
  104. flag = SysRoleService.auth_data_scope(dto)
  105. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()
  106. @reg.api.route("/system/role/changeStatus", methods=["PUT"])
  107. @BodyValidator()
  108. @PreAuthorize(HasPerm("system:role:edit"))
  109. @Log(title="角色管理",business_type=BusinessType.UPDATE)
  110. @JsonSerializer()
  111. def system_role_change_status(dto:SysRole):
  112. '''
  113. 修改角色状态
  114. '''
  115. SysRoleService.check_role_allowed(dto)
  116. SysRoleService.check_role_data_scope(dto.role_id)
  117. flag = SysRoleService.update_role_status(dto)
  118. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()
  119. @reg.api.route("/system/role/<ids>", methods=["DELETE"])
  120. @PathValidator()
  121. @PreAuthorize(HasPerm("system:role:remove"))
  122. @Log(title="角色管理",business_type=BusinessType.DELETE)
  123. @JsonSerializer()
  124. def system_role_delete(
  125. ids: Annotated[List[int],BeforeValidator(ids_to_list)]
  126. ):
  127. '''
  128. 删除角色
  129. '''
  130. SysRoleService.delete_role_by_ids(ids)
  131. return AjaxResponse.from_success()
  132. @reg.api.route("/system/role/optionselect", methods=["GET"])
  133. @PreAuthorize(HasPerm("system:role:query"))
  134. @JsonSerializer()
  135. def system_role_options():
  136. '''
  137. 获取角色选择框列表
  138. '''
  139. rows = SysRoleService.select_role_all()
  140. return AjaxResponse.from_success(data=rows)
  141. @reg.api.route("/system/role/authUser/allocatedList", methods=["GET"])
  142. @QueryValidator(is_page=True)
  143. @PreAuthorize(HasPerm("system:role:list"))
  144. @JsonSerializer()
  145. def system_role_user_allocated_list(dto:SysRole):
  146. '''
  147. 获取角色选择框列表
  148. '''
  149. rows = SysUserService.select_allocated_list(dto)
  150. return TableResponse(rows=rows)
  151. @reg.api.route("/system/role/authUser/unallocatedList", methods=["GET"])
  152. @QueryValidator(is_page=True)
  153. @PreAuthorize(HasPerm("system:role:list"))
  154. @JsonSerializer()
  155. def system_role_user_unallocated_list(dto:SysRole):
  156. '''
  157. 查询未分配用户角色列表
  158. '''
  159. rows = SysUserService.select_unallocated_list(dto)
  160. return TableResponse(rows=rows)
  161. @reg.api.route("/system/role/authUser/cancel", methods=["PUT"])
  162. @BodyValidator()
  163. @PreAuthorize(HasPerm("system:role:edit"))
  164. @Log(title="角色管理",business_type=BusinessType.GRANT)
  165. @JsonSerializer()
  166. def system_role_user_cancel(dto:SysUserRole):
  167. '''
  168. 取消授权用户
  169. '''
  170. flag = SysRoleService.delete_auth_user(dto)
  171. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()
  172. @reg.api.route("/system/role/authUser/cancelAll", methods=["PUT"])
  173. @BodyValidator()
  174. @PreAuthorize(HasPerm("system:role:edit"))
  175. @Log(title="角色管理",business_type=BusinessType.GRANT)
  176. @JsonSerializer()
  177. def system_role_user_cancel_all(
  178. role_id:Annotated[int,Field(gt=0)],
  179. user_ids:Annotated[List[int],Field(default_factory=List)]
  180. ):
  181. '''
  182. 批量取消授权用户
  183. '''
  184. flag = SysRoleService.delete_auth_users(
  185. role_id=role_id,
  186. user_ids=user_ids
  187. )
  188. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()
  189. @reg.api.route("/system/role/authUser/selectAll", methods=["PUT"])
  190. @BodyValidator()
  191. @PreAuthorize(HasPerm("system:role:edit"))
  192. @Log(title="角色管理",business_type=BusinessType.GRANT)
  193. @JsonSerializer()
  194. def system_role_user_select_all(
  195. role_id:Annotated[int,Field(gt=0)],
  196. user_ids:Annotated[List[int],Field(default_factory=List)]
  197. ):
  198. '''
  199. 批量选择授权用户
  200. '''
  201. SysRoleService.check_role_data_scope(role_id)
  202. flag = SysRoleService.insert_auth_users(
  203. role_id=role_id,
  204. user_ids=user_ids
  205. )
  206. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()