SysRoleController.java 9.2 KB

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