SysRoleController.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package com.xunmei.system.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.xunmei.common.core.domain.IdName;
  4. import com.xunmei.common.core.vo.IdNameVo;
  5. import com.xunmei.common.core.web.controller.BaseController;
  6. import com.xunmei.common.core.web.domain.AjaxResult;
  7. import com.xunmei.common.core.web.page.TableDataInfo;
  8. import com.xunmei.common.log.annotation.Log;
  9. import com.xunmei.common.log.enums.BusinessType;
  10. import com.xunmei.common.security.annotation.InnerAuth;
  11. import com.xunmei.common.security.annotation.RequiresPermissions;
  12. import com.xunmei.common.security.utils.SecurityUtils;
  13. import com.xunmei.system.api.Eto.RoleConditionEto;
  14. import com.xunmei.system.api.domain.SysDept;
  15. import com.xunmei.system.api.domain.SysRole;
  16. import com.xunmei.system.api.domain.SysUser;
  17. import com.xunmei.system.domain.SysUserRole;
  18. import com.xunmei.system.service.ISysDeptService;
  19. import com.xunmei.system.service.ISysRoleService;
  20. import com.xunmei.system.service.ISysUserService;
  21. import io.swagger.annotations.ApiOperation;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.validation.annotation.Validated;
  24. import org.springframework.web.bind.annotation.*;
  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. }
  82. /* else if (!roleService.checkRoleKeyUnique(role)) {
  83. return error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
  84. }*/
  85. role.setUpdateBy(SecurityUtils.getUsername());
  86. return toAjax(roleService.updateRole(role));
  87. }
  88. /**
  89. * 修改保存数据权限
  90. */
  91. @RequiresPermissions("system:role:edit")
  92. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  93. @PutMapping("/dataScope")
  94. public AjaxResult dataScope(@RequestBody SysRole role) {
  95. roleService.checkRoleAllowed(role);
  96. roleService.checkRoleDataScope(role.getId());
  97. return toAjax(roleService.authDataScope(role));
  98. }
  99. /**
  100. * 状态修改
  101. */
  102. @RequiresPermissions("system:role:edit")
  103. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  104. @PutMapping("/changeStatus")
  105. public AjaxResult changeStatus(@RequestBody SysRole role) {
  106. roleService.checkRoleAllowed(role);
  107. roleService.checkRoleDataScope(role.getId());
  108. role.setUpdateBy(SecurityUtils.getUsername());
  109. return toAjax(roleService.updateRoleStatus(role));
  110. }
  111. /**
  112. * 删除角色
  113. */
  114. @RequiresPermissions("system:role:remove")
  115. @Log(title = "角色管理", businessType = BusinessType.DELETE)
  116. @DeleteMapping("/{ids}")
  117. public AjaxResult remove(@PathVariable Long[] ids) {
  118. return toAjax(roleService.deleteRoleByIds(ids));
  119. }
  120. /**
  121. * 获取角色选择框列表
  122. */
  123. @RequiresPermissions("system:role:query")
  124. @GetMapping("/optionselect")
  125. public AjaxResult optionselect() {
  126. return success(roleService.selectRoleAll());
  127. }
  128. @RequiresPermissions("system:role:query")
  129. @GetMapping("/allRole")
  130. public AjaxResult allRole() {
  131. return success(roleService.allRole());
  132. }
  133. /**
  134. * 查询已分配用户角色列表
  135. */
  136. /* @RequiresPermissions("system:role:list")
  137. @GetMapping("/authUser/allocatedList")
  138. public TableDataInfo allocatedList(SysUserVO user) {
  139. return userService.selectAllocatedList(user);
  140. }
  141. *//**
  142. * 查询未分配用户角色列表
  143. *//*
  144. @RequiresPermissions("system:role:list")
  145. @GetMapping("/authUser/unallocatedList")
  146. public TableDataInfo unallocatedList(SysUserVO user) {
  147. return userService.selectUnallocatedList(user);
  148. }
  149. */
  150. /**
  151. * 取消授权用户
  152. */
  153. @RequiresPermissions("system:role:edit")
  154. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  155. @PutMapping("/authUser/cancel")
  156. public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole) {
  157. return toAjax(roleService.deleteAuthUser(userRole));
  158. }
  159. /**
  160. * 批量取消授权用户
  161. */
  162. @RequiresPermissions("system:role:edit")
  163. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  164. @PutMapping("/authUser/cancelAll")
  165. public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds) {
  166. return toAjax(roleService.deleteAuthUsers(roleId, userIds));
  167. }
  168. /**
  169. * 批量选择用户授权
  170. */
  171. @RequiresPermissions("system:role:edit")
  172. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  173. @PutMapping("/authUser/selectAll")
  174. public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds) {
  175. roleService.checkRoleDataScope(roleId);
  176. return toAjax(roleService.insertAuthUsers(roleId, userIds));
  177. }
  178. /**
  179. * 获取对应角色机构树列表
  180. */
  181. @RequiresPermissions("system:role:query")
  182. @GetMapping(value = "/deptTree/{roleId}")
  183. public AjaxResult deptTree(@PathVariable("roleId") Long roleId) {
  184. AjaxResult ajax = AjaxResult.success();
  185. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  186. ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
  187. return ajax;
  188. }
  189. /**
  190. * 获取所有可用的角色信息
  191. */
  192. @GetMapping(value = "/all")
  193. public AjaxResult all(@RequestParam(value = "orgType", required = false) Integer orgType) {
  194. return success(roleService.selectSimpleRoleAll(null, orgType));
  195. }
  196. @ApiOperation(value = "根据id查询角色")
  197. @InnerAuth
  198. @GetMapping(value = "/getRoleById")
  199. public SysRole getRoleById(Long id) {
  200. return roleService.getById(id);
  201. }
  202. @ApiOperation(value = "根据userid查询角色")
  203. @InnerAuth
  204. @GetMapping(value = "/getRoleByUserId")
  205. public List<SysRole> getRoleByUserId(Long userId) {
  206. return roleService.selectRolesByUserId(userId);
  207. }
  208. @ApiOperation(value = "根据名称查询角色")
  209. @InnerAuth
  210. @GetMapping(value = "/getRoleByName")
  211. public SysRole getRoleByName(String name) {
  212. return roleService.getOne(new LambdaQueryWrapper<SysRole>()
  213. .like(SysRole::getRoleName, name).select(SysRole::getId));
  214. }
  215. /**
  216. * 根据用户查询角色
  217. *
  218. * @param userId
  219. * @return
  220. */
  221. @InnerAuth
  222. @GetMapping(value = "/selectRoleNameByUserId")
  223. public String selectRoleNameByUserId(Long userId) {
  224. return roleService.selectRoleNameByUserId(userId);
  225. }
  226. @RequiresPermissions("system:role:query")
  227. @ApiOperation(value = "根据机构类型查询角色")
  228. @GetMapping(value = "/getRoleByType/{type}")
  229. public List<SysRole> getRoleByType(@PathVariable(value = "type", required = false) String type) {
  230. return roleService.getRoleByType(type);
  231. }
  232. // @InnerAuth
  233. @PostMapping("/getNames")
  234. public List<IdNameVo> getNames(@RequestBody RoleConditionEto condition) {
  235. List<IdName<Long, String>> r = roleService.selectSimpleRoleAll(condition, null);
  236. return r.stream().map(i -> new IdNameVo(i.getId(), i.getName(), null)).collect(Collectors.toList());
  237. }
  238. @GetMapping("/getnamesbyorgid/{orgId}")
  239. public AjaxResult getNamesByOrgId(@PathVariable Long orgId) {
  240. return success(roleService.getNamesByOrgId(orgId));
  241. }
  242. @GetMapping("/selectUserByRoleNameAndOrgId")
  243. public List<SysUser> selectUserByRoleNameAndOrgId(@RequestParam(value = "roleName") String roleName, @RequestParam(value = "orgId") Long orgId) {
  244. return roleService.selectUserByRoleNameAndOrgId(roleName, orgId);
  245. }
  246. @GetMapping("/getRoles")
  247. public List<SysRole> getRoles() {
  248. return roleService.getRoles();
  249. }
  250. }