SysRoleController.java 8.4 KB

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