SysRoleController.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.ruoyi.web.controller.system;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.security.access.prepost.PreAuthorize;
  5. import org.springframework.validation.annotation.Validated;
  6. import org.springframework.web.bind.annotation.DeleteMapping;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.PutMapping;
  11. import org.springframework.web.bind.annotation.RequestBody;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.RestController;
  14. import com.ruoyi.common.annotation.Log;
  15. import com.ruoyi.common.constant.UserConstants;
  16. import com.ruoyi.common.core.controller.BaseController;
  17. import com.ruoyi.common.core.domain.AjaxResult;
  18. import com.ruoyi.common.core.domain.entity.SysRole;
  19. import com.ruoyi.common.core.domain.entity.SysUser;
  20. import com.ruoyi.common.core.domain.model.LoginUser;
  21. import com.ruoyi.common.core.page.TableDataInfo;
  22. import com.ruoyi.common.enums.BusinessType;
  23. import com.ruoyi.common.utils.ServletUtils;
  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. @GetMapping("/export")
  59. public AjaxResult export(SysRole role)
  60. {
  61. List<SysRole> list = roleService.selectRoleList(role);
  62. ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
  63. return util.exportExcel(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. return AjaxResult.success(roleService.selectRoleById(roleId));
  73. }
  74. /**
  75. * 新增角色
  76. */
  77. @PreAuthorize("@ss.hasPermi('system:role:add')")
  78. @Log(title = "角色管理", businessType = BusinessType.INSERT)
  79. @PostMapping
  80. public AjaxResult add(@Validated @RequestBody SysRole role)
  81. {
  82. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  83. {
  84. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
  85. }
  86. else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
  87. {
  88. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
  89. }
  90. role.setCreateBy(getUsername());
  91. return toAjax(roleService.insertRole(role));
  92. }
  93. /**
  94. * 修改保存角色
  95. */
  96. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  97. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  98. @PutMapping
  99. public AjaxResult edit(@Validated @RequestBody SysRole role)
  100. {
  101. roleService.checkRoleAllowed(role);
  102. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  103. {
  104. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
  105. }
  106. else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
  107. {
  108. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
  109. }
  110. role.setUpdateBy(getUsername());
  111. if (roleService.updateRole(role) > 0)
  112. {
  113. // 更新缓存用户权限
  114. LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
  115. if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin())
  116. {
  117. loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser()));
  118. loginUser.setUser(userService.selectUserByUserName(loginUser.getUser().getUserName()));
  119. tokenService.setLoginUser(loginUser);
  120. }
  121. return AjaxResult.success();
  122. }
  123. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
  124. }
  125. /**
  126. * 修改保存数据权限
  127. */
  128. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  129. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  130. @PutMapping("/dataScope")
  131. public AjaxResult dataScope(@RequestBody SysRole role)
  132. {
  133. roleService.checkRoleAllowed(role);
  134. return toAjax(roleService.authDataScope(role));
  135. }
  136. /**
  137. * 状态修改
  138. */
  139. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  140. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  141. @PutMapping("/changeStatus")
  142. public AjaxResult changeStatus(@RequestBody SysRole role)
  143. {
  144. roleService.checkRoleAllowed(role);
  145. role.setUpdateBy(getUsername());
  146. return toAjax(roleService.updateRoleStatus(role));
  147. }
  148. /**
  149. * 删除角色
  150. */
  151. @PreAuthorize("@ss.hasPermi('system:role:remove')")
  152. @Log(title = "角色管理", businessType = BusinessType.DELETE)
  153. @DeleteMapping("/{roleIds}")
  154. public AjaxResult remove(@PathVariable Long[] roleIds)
  155. {
  156. return toAjax(roleService.deleteRoleByIds(roleIds));
  157. }
  158. /**
  159. * 获取角色选择框列表
  160. */
  161. @PreAuthorize("@ss.hasPermi('system:role:query')")
  162. @GetMapping("/optionselect")
  163. public AjaxResult optionselect()
  164. {
  165. return AjaxResult.success(roleService.selectRoleAll());
  166. }
  167. /**
  168. * 查询已分配用户角色列表
  169. */
  170. @PreAuthorize("@ss.hasPermi('system:role:list')")
  171. @GetMapping("/authUser/allocatedList")
  172. public TableDataInfo allocatedList(SysUser user)
  173. {
  174. startPage();
  175. List<SysUser> list = userService.selectAllocatedList(user);
  176. return getDataTable(list);
  177. }
  178. /**
  179. * 查询未分配用户角色列表
  180. */
  181. @PreAuthorize("@ss.hasPermi('system:role:list')")
  182. @GetMapping("/authUser/unallocatedList")
  183. public TableDataInfo unallocatedList(SysUser user)
  184. {
  185. startPage();
  186. List<SysUser> list = userService.selectUnallocatedList(user);
  187. return getDataTable(list);
  188. }
  189. /**
  190. * 取消授权用户
  191. */
  192. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  193. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  194. @PutMapping("/authUser/cancel")
  195. public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole)
  196. {
  197. return toAjax(roleService.deleteAuthUser(userRole));
  198. }
  199. /**
  200. * 批量取消授权用户
  201. */
  202. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  203. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  204. @PutMapping("/authUser/cancelAll")
  205. public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds)
  206. {
  207. return toAjax(roleService.deleteAuthUsers(roleId, userIds));
  208. }
  209. /**
  210. * 批量选择用户授权
  211. */
  212. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  213. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  214. @PutMapping("/authUser/selectAll")
  215. public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds)
  216. {
  217. return toAjax(roleService.insertAuthUsers(roleId, userIds));
  218. }
  219. }