SysRoleController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.ruoyi.web.controller.system;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.security.access.prepost.PreAuthorize;
  5. import org.springframework.validation.annotation.Validated;
  6. import org.springframework.web.bind.annotation.DeleteMapping;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.PutMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.ruoyi.common.annotation.Log;
  15. import com.ruoyi.common.constant.UserConstants;
  16. import com.ruoyi.common.core.controller.BaseController;
  17. import com.ruoyi.common.core.domain.AjaxResult;
  18. import com.ruoyi.common.core.domain.entity.SysRole;
  19. import com.ruoyi.common.core.domain.entity.SysUser;
  20. import com.ruoyi.common.core.domain.model.LoginUser;
  21. import com.ruoyi.common.core.page.TableDataInfo;
  22. import com.ruoyi.common.enums.BusinessType;
  23. import com.ruoyi.common.utils.StringUtils;
  24. import com.ruoyi.common.utils.poi.ExcelUtil;
  25. import com.ruoyi.framework.web.service.SysPermissionService;
  26. import com.ruoyi.framework.web.service.TokenService;
  27. import com.ruoyi.system.domain.SysUserRole;
  28. import com.ruoyi.system.service.ISysRoleService;
  29. import com.ruoyi.system.service.ISysUserService;
  30. /**
  31. * 角色信息
  32. *
  33. * @author ruoyi
  34. */
  35. @RestController
  36. @RequestMapping("/system/role")
  37. public class SysRoleController extends BaseController
  38. {
  39. @Autowired
  40. private ISysRoleService roleService;
  41. @Autowired
  42. private TokenService tokenService;
  43. @Autowired
  44. private SysPermissionService permissionService;
  45. @Autowired
  46. private ISysUserService userService;
  47. @PreAuthorize("@ss.hasPermi('system:role:list')")
  48. @GetMapping("/list")
  49. public TableDataInfo list(SysRole role)
  50. {
  51. startPage();
  52. List<SysRole> list = roleService.selectRoleList(role);
  53. return getDataTable(list);
  54. }
  55. @Log(title = "角色管理", businessType = BusinessType.EXPORT)
  56. @PreAuthorize("@ss.hasPermi('system:role:export')")
  57. @GetMapping("/export")
  58. public AjaxResult export(SysRole role)
  59. {
  60. List<SysRole> list = roleService.selectRoleList(role);
  61. ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
  62. return util.exportExcel(list, "角色数据");
  63. }
  64. /**
  65. * 根据角色编号获取详细信息
  66. */
  67. @PreAuthorize("@ss.hasPermi('system:role:query')")
  68. @GetMapping(value = "/{roleId}")
  69. public AjaxResult getInfo(@PathVariable Long roleId)
  70. {
  71. return AjaxResult.success(roleService.selectRoleById(roleId));
  72. }
  73. /**
  74. * 新增角色
  75. */
  76. @PreAuthorize("@ss.hasPermi('system:role:add')")
  77. @Log(title = "角色管理", businessType = BusinessType.INSERT)
  78. @PostMapping
  79. public AjaxResult add(@Validated @RequestBody SysRole role)
  80. {
  81. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  82. {
  83. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
  84. }
  85. else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
  86. {
  87. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
  88. }
  89. role.setCreateBy(getUsername());
  90. return toAjax(roleService.insertRole(role));
  91. }
  92. /**
  93. * 修改保存角色
  94. */
  95. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  96. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  97. @PutMapping
  98. public AjaxResult edit(@Validated @RequestBody SysRole role)
  99. {
  100. roleService.checkRoleAllowed(role);
  101. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  102. {
  103. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
  104. }
  105. else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
  106. {
  107. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
  108. }
  109. role.setUpdateBy(getUsername());
  110. if (roleService.updateRole(role) > 0)
  111. {
  112. // 更新缓存用户权限
  113. LoginUser loginUser = getLoginUser();
  114. if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin())
  115. {
  116. loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser()));
  117. loginUser.setUser(userService.selectUserByUserName(loginUser.getUser().getUserName()));
  118. tokenService.setLoginUser(loginUser);
  119. }
  120. return AjaxResult.success();
  121. }
  122. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
  123. }
  124. /**
  125. * 修改保存数据权限
  126. */
  127. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  128. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  129. @PutMapping("/dataScope")
  130. public AjaxResult dataScope(@RequestBody SysRole role)
  131. {
  132. roleService.checkRoleAllowed(role);
  133. return toAjax(roleService.authDataScope(role));
  134. }
  135. /**
  136. * 状态修改
  137. */
  138. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  139. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  140. @PutMapping("/changeStatus")
  141. public AjaxResult changeStatus(@RequestBody SysRole role)
  142. {
  143. roleService.checkRoleAllowed(role);
  144. role.setUpdateBy(getUsername());
  145. return toAjax(roleService.updateRoleStatus(role));
  146. }
  147. /**
  148. * 删除角色
  149. */
  150. @PreAuthorize("@ss.hasPermi('system:role:remove')")
  151. @Log(title = "角色管理", businessType = BusinessType.DELETE)
  152. @DeleteMapping("/{roleIds}")
  153. public AjaxResult remove(@PathVariable Long[] roleIds)
  154. {
  155. return toAjax(roleService.deleteRoleByIds(roleIds));
  156. }
  157. /**
  158. * 获取角色选择框列表
  159. */
  160. @PreAuthorize("@ss.hasPermi('system:role:query')")
  161. @GetMapping("/optionselect")
  162. public AjaxResult optionselect()
  163. {
  164. return AjaxResult.success(roleService.selectRoleAll());
  165. }
  166. /**
  167. * 查询已分配用户角色列表
  168. */
  169. @PreAuthorize("@ss.hasPermi('system:role:list')")
  170. @GetMapping("/authUser/allocatedList")
  171. public TableDataInfo allocatedList(SysUser user)
  172. {
  173. startPage();
  174. List<SysUser> list = userService.selectAllocatedList(user);
  175. return getDataTable(list);
  176. }
  177. /**
  178. * 查询未分配用户角色列表
  179. */
  180. @PreAuthorize("@ss.hasPermi('system:role:list')")
  181. @GetMapping("/authUser/unallocatedList")
  182. public TableDataInfo unallocatedList(SysUser user)
  183. {
  184. startPage();
  185. List<SysUser> list = userService.selectUnallocatedList(user);
  186. return getDataTable(list);
  187. }
  188. /**
  189. * 取消授权用户
  190. */
  191. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  192. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  193. @PutMapping("/authUser/cancel")
  194. public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole)
  195. {
  196. return toAjax(roleService.deleteAuthUser(userRole));
  197. }
  198. /**
  199. * 批量取消授权用户
  200. */
  201. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  202. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  203. @PutMapping("/authUser/cancelAll")
  204. public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds)
  205. {
  206. return toAjax(roleService.deleteAuthUsers(roleId, userIds));
  207. }
  208. /**
  209. * 批量选择用户授权
  210. */
  211. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  212. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  213. @PutMapping("/authUser/selectAll")
  214. public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds)
  215. {
  216. return toAjax(roleService.insertAuthUsers(roleId, userIds));
  217. }
  218. }