SysRoleController.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.xunmei.system.controller;
  2. import com.xunmei.common.core.domain.IdName;
  3. import com.xunmei.common.core.vo.IdNameVo;
  4. import com.xunmei.common.core.web.controller.BaseController;
  5. import com.xunmei.common.core.web.domain.AjaxResult;
  6. import com.xunmei.common.core.web.page.TableDataInfo;
  7. import com.xunmei.common.log.annotation.Log;
  8. import com.xunmei.common.log.enums.BusinessType;
  9. import com.xunmei.common.security.annotation.InnerAuth;
  10. import com.xunmei.common.security.annotation.RequiresPermissions;
  11. import com.xunmei.common.security.utils.SecurityUtils;
  12. import com.xunmei.system.api.Eto.RoleConditionEto;
  13. import com.xunmei.system.api.domain.SysOrg;
  14. import com.xunmei.system.api.domain.SysRole;
  15. import com.xunmei.system.domain.SysUserRole;
  16. import com.xunmei.system.domain.vo.SysUserVO;
  17. import com.xunmei.system.service.ISysDeptService;
  18. import com.xunmei.system.service.ISysRoleService;
  19. import com.xunmei.system.service.ISysUserService;
  20. import io.swagger.annotations.ApiOperation;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.validation.annotation.Validated;
  23. import org.springframework.web.bind.annotation.*;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.stream.Collectors;
  27. /**
  28. * 角色信息
  29. *
  30. * @author xunmei
  31. */
  32. @RestController
  33. @RequestMapping("/role")
  34. public class SysRoleController extends BaseController {
  35. @Autowired
  36. private ISysRoleService roleService;
  37. @Autowired
  38. private ISysUserService userService;
  39. @Autowired
  40. private ISysDeptService deptService;
  41. @RequiresPermissions("system:role:list")
  42. @GetMapping("/list")
  43. public TableDataInfo<SysRole> list(SysRole role) {
  44. return roleService.selectRoleList(role);
  45. }
  46. /**
  47. * 根据角色编号获取详细信息
  48. */
  49. @RequiresPermissions("system:role:query")
  50. @GetMapping(value = "/{roleId}")
  51. public AjaxResult getInfo(@PathVariable Long roleId) {
  52. roleService.checkRoleDataScope(roleId);
  53. return success(roleService.selectRoleById(roleId));
  54. }
  55. /**
  56. * 新增角色
  57. */
  58. @RequiresPermissions("system:role:add")
  59. @Log(title = "角色管理", businessType = BusinessType.INSERT)
  60. @PostMapping
  61. public AjaxResult add(@Validated @RequestBody SysRole role) {
  62. if (!roleService.checkRoleNameUnique(role)) {
  63. return error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
  64. } else if (!roleService.checkRoleKeyUnique(role)) {
  65. return error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
  66. }
  67. role.setCreateBy(SecurityUtils.getUsername());
  68. return toAjax(roleService.insertRole(role));
  69. }
  70. /**
  71. * 修改保存角色
  72. */
  73. @RequiresPermissions("system:role:edit")
  74. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  75. @PutMapping
  76. public AjaxResult edit(@Validated @RequestBody SysRole role) {
  77. roleService.checkRoleAllowed(role);
  78. roleService.checkRoleDataScope(role.getId());
  79. if (!roleService.checkRoleNameUnique(role)) {
  80. return error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
  81. } else if (!roleService.checkRoleKeyUnique(role)) {
  82. return error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
  83. }
  84. role.setUpdateBy(SecurityUtils.getUsername());
  85. return toAjax(roleService.updateRole(role));
  86. }
  87. /**
  88. * 修改保存数据权限
  89. */
  90. @RequiresPermissions("system:role:edit")
  91. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  92. @PutMapping("/dataScope")
  93. public AjaxResult dataScope(@RequestBody SysRole role) {
  94. roleService.checkRoleAllowed(role);
  95. roleService.checkRoleDataScope(role.getId());
  96. return toAjax(roleService.authDataScope(role));
  97. }
  98. /**
  99. * 状态修改
  100. */
  101. @RequiresPermissions("system:role:edit")
  102. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  103. @PutMapping("/changeStatus")
  104. public AjaxResult changeStatus(@RequestBody SysRole role) {
  105. roleService.checkRoleAllowed(role);
  106. roleService.checkRoleDataScope(role.getId());
  107. role.setUpdateBy(SecurityUtils.getUsername());
  108. return toAjax(roleService.updateRoleStatus(role));
  109. }
  110. /**
  111. * 删除角色
  112. */
  113. @RequiresPermissions("system:role:remove")
  114. @Log(title = "角色管理", businessType = BusinessType.DELETE)
  115. @DeleteMapping("/{ids}")
  116. public AjaxResult remove(@PathVariable Long[] ids) {
  117. return toAjax(roleService.deleteRoleByIds(ids));
  118. }
  119. /**
  120. * 获取角色选择框列表
  121. */
  122. @RequiresPermissions("system:role:query")
  123. @GetMapping("/optionselect")
  124. public AjaxResult optionselect() {
  125. return success(roleService.selectRoleAll());
  126. }
  127. /**
  128. * 查询已分配用户角色列表
  129. */
  130. @RequiresPermissions("system:role:list")
  131. @GetMapping("/authUser/allocatedList")
  132. public TableDataInfo allocatedList(SysUserVO user) {
  133. return userService.selectAllocatedList(user);
  134. }
  135. /**
  136. * 查询未分配用户角色列表
  137. */
  138. @RequiresPermissions("system:role:list")
  139. @GetMapping("/authUser/unallocatedList")
  140. public TableDataInfo unallocatedList(SysUserVO user) {
  141. return userService.selectUnallocatedList(user);
  142. }
  143. /**
  144. * 取消授权用户
  145. */
  146. @RequiresPermissions("system:role:edit")
  147. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  148. @PutMapping("/authUser/cancel")
  149. public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole) {
  150. return toAjax(roleService.deleteAuthUser(userRole));
  151. }
  152. /**
  153. * 批量取消授权用户
  154. */
  155. @RequiresPermissions("system:role:edit")
  156. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  157. @PutMapping("/authUser/cancelAll")
  158. public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds) {
  159. return toAjax(roleService.deleteAuthUsers(roleId, userIds));
  160. }
  161. /**
  162. * 批量选择用户授权
  163. */
  164. @RequiresPermissions("system:role:edit")
  165. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  166. @PutMapping("/authUser/selectAll")
  167. public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds) {
  168. roleService.checkRoleDataScope(roleId);
  169. return toAjax(roleService.insertAuthUsers(roleId, userIds));
  170. }
  171. /**
  172. * 获取对应角色部门树列表
  173. */
  174. @RequiresPermissions("system:role:query")
  175. @GetMapping(value = "/deptTree/{roleId}")
  176. public AjaxResult deptTree(@PathVariable("roleId") Long roleId) {
  177. AjaxResult ajax = AjaxResult.success();
  178. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  179. ajax.put("depts", deptService.selectDeptTreeList(new ArrayList<>()));
  180. return ajax;
  181. }
  182. /**
  183. * 获取所有可用的角色信息
  184. */
  185. @GetMapping(value = "/all")
  186. public AjaxResult all(@RequestParam(value = "orgType", required = false) Integer orgType) {
  187. return success(roleService.selectSimpleRoleAll(null, orgType));
  188. }
  189. @ApiOperation(value = "根据id查询角色")
  190. @InnerAuth
  191. @GetMapping(value = "/getRoleById")
  192. public SysRole getRoleById(Long id) {
  193. return roleService.getById(id);
  194. }
  195. // @InnerAuth
  196. @PostMapping ("/getNames")
  197. public List<IdNameVo> getNames(RoleConditionEto condition) {
  198. List<IdName<Long, String>> r = roleService.selectSimpleRoleAll(condition, null);
  199. return r.stream().map(i->new IdNameVo(i.getId(),i.getName(),null)).collect(Collectors.toList());
  200. }
  201. }