SysRoleController.java 7.9 KB

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