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