SysRoleController.java 8.3 KB

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