SysRoleController.java 7.7 KB

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