role.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. SysRoleService.check_role_allowed(dto)
  99. SysRoleService.check_role_data_scope(dto.role_id)
  100. return AjaxResponse.from_success()
  101. @reg.api.route("/system/role/changeStatus", methods=["PUT"])
  102. @BodyValidator()
  103. @PreAuthorize(HasPerm("system:role:edit"))
  104. @Log(title="角色管理",business_type=BusinessType.UPDATE)
  105. @JsonSerializer()
  106. def system_role_change_status(dto:SysRole):
  107. '''
  108. 修改角色状态
  109. '''
  110. SysRoleService.check_role_allowed(dto)
  111. SysRoleService.check_role_data_scope(dto.role_id)
  112. flag = SysRoleService.update_role_status(dto)
  113. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()
  114. @reg.api.route("/system/role/<ids>", methods=["DELETE"])
  115. @PathValidator()
  116. @PreAuthorize(HasPerm("system:role:remove"))
  117. @Log(title="角色管理",business_type=BusinessType.DELETE)
  118. @JsonSerializer()
  119. def system_role_delete(
  120. ids: Annotated[List[int],BeforeValidator(ids_to_list)]
  121. ):
  122. '''
  123. 删除角色
  124. '''
  125. SysRoleService.delete_role_by_ids(ids)
  126. return AjaxResponse.from_success()
  127. @reg.api.route("/system/role/optionselect", methods=["GET"])
  128. @PreAuthorize(HasPerm("system:role:query"))
  129. @JsonSerializer()
  130. def system_role_options():
  131. '''
  132. 获取角色选择框列表
  133. '''
  134. rows = SysRoleService.select_role_all()
  135. return AjaxResponse.from_success(data=rows)
  136. @reg.api.route("/system/role/authUser/allocatedList", methods=["GET"])
  137. @QueryValidator(is_page=True)
  138. @PreAuthorize(HasPerm("system:role:list"))
  139. @JsonSerializer()
  140. def system_role_user_allocated_list(dto:SysRole):
  141. '''
  142. 获取角色选择框列表
  143. '''
  144. rows = SysUserService.select_allocated_list(dto)
  145. return TableResponse(rows=rows)
  146. @reg.api.route("/system/role/authUser/unallocatedList", methods=["GET"])
  147. @QueryValidator(is_page=True)
  148. @PreAuthorize(HasPerm("system:role:list"))
  149. @JsonSerializer()
  150. def system_role_user_unallocated_list(dto:SysRole):
  151. '''
  152. 查询未分配用户角色列表
  153. '''
  154. rows = SysUserService.select_unallocated_list(dto)
  155. return TableResponse(rows=rows)
  156. @reg.api.route("/system/role/authUser/cancel", methods=["PUT"])
  157. @BodyValidator()
  158. @PreAuthorize(HasPerm("system:role:edit"))
  159. @Log(title="角色管理",business_type=BusinessType.GRANT)
  160. @JsonSerializer()
  161. def system_role_user_cancel(dto:SysUserRole):
  162. '''
  163. 取消授权用户
  164. '''
  165. flag = SysRoleService.delete_auth_user(dto)
  166. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()
  167. @reg.api.route("/system/role/authUser/cancelAll", methods=["PUT"])
  168. @BodyValidator()
  169. @PreAuthorize(HasPerm("system:role:edit"))
  170. @Log(title="角色管理",business_type=BusinessType.GRANT)
  171. @JsonSerializer()
  172. def system_role_user_cancel_all(
  173. role_id:Annotated[int,Field(gt=0)],
  174. user_ids:Annotated[List[int],Field(default_factory=List)]
  175. ):
  176. '''
  177. 批量取消授权用户
  178. '''
  179. flag = SysRoleService.delete_auth_users(
  180. role_id=role_id,
  181. user_ids=user_ids
  182. )
  183. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()
  184. @reg.api.route("/system/role/authUser/selectAll", methods=["PUT"])
  185. @BodyValidator()
  186. @PreAuthorize(HasPerm("system:role:edit"))
  187. @Log(title="角色管理",business_type=BusinessType.GRANT)
  188. @JsonSerializer()
  189. def system_role_user_select_all(
  190. role_id:Annotated[int,Field(gt=0)],
  191. user_ids:Annotated[List[int],Field(default_factory=List)]
  192. ):
  193. '''
  194. 批量选择授权用户
  195. '''
  196. SysRoleService.check_role_data_scope(role_id)
  197. flag = SysRoleService.insert_auth_users(
  198. role_id=role_id,
  199. user_ids=user_ids
  200. )
  201. return AjaxResponse.from_success() if flag else AjaxResponse.from_error()