SysRoleController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package com.ozs.web.controller.system;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletResponse;
  4. import javax.validation.constraints.NotEmpty;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.metadata.IPage;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.ozs.system.domain.vo.requestVo.SysMenuIdsRequestVo;
  9. import com.ozs.system.domain.vo.requestVo.SysRoleRequestVo;
  10. import com.ozs.system.domain.vo.requestVo.SysStatusRequestVo;
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiOperation;
  13. import org.springframework.beans.BeanUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.security.access.prepost.PreAuthorize;
  16. import org.springframework.validation.annotation.Validated;
  17. import org.springframework.web.bind.annotation.DeleteMapping;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.PathVariable;
  20. import org.springframework.web.bind.annotation.PostMapping;
  21. import org.springframework.web.bind.annotation.PutMapping;
  22. import org.springframework.web.bind.annotation.RequestBody;
  23. import org.springframework.web.bind.annotation.RequestMapping;
  24. import org.springframework.web.bind.annotation.RestController;
  25. import com.ozs.common.annotation.Log;
  26. import com.ozs.common.constant.UserConstants;
  27. import com.ozs.common.core.controller.BaseController;
  28. import com.ozs.common.core.domain.AjaxResult;
  29. import com.ozs.common.core.domain.entity.SysDept;
  30. import com.ozs.common.core.domain.entity.SysRole;
  31. import com.ozs.common.core.domain.entity.SysUser;
  32. import com.ozs.common.core.domain.model.LoginUser;
  33. import com.ozs.common.core.page.TableDataInfo;
  34. import com.ozs.common.enums.BusinessType;
  35. import com.ozs.common.utils.StringUtils;
  36. import com.ozs.common.utils.poi.ExcelUtil;
  37. import com.ozs.framework.web.service.SysPermissionService;
  38. import com.ozs.framework.web.service.TokenService;
  39. import com.ozs.system.domain.SysUserRole;
  40. import com.ozs.system.service.ISysDeptService;
  41. import com.ozs.system.service.ISysRoleService;
  42. import com.ozs.system.service.ISysUserService;
  43. /**
  44. * 角色信息
  45. *
  46. * @author ruoyi
  47. */
  48. @RestController
  49. @RequestMapping("/system/role")
  50. @Api(tags = "角色信息")
  51. public class SysRoleController extends BaseController
  52. {
  53. @Autowired
  54. private ISysRoleService roleService;
  55. @Autowired
  56. private TokenService tokenService;
  57. @Autowired
  58. private SysPermissionService permissionService;
  59. @Autowired
  60. private ISysUserService userService;
  61. @Autowired
  62. private ISysDeptService deptService;
  63. // @PreAuthorize("@ss.hasPermi('system:role:list')")
  64. // @GetMapping("/list")
  65. // @ApiOperation("根据条件分页查询角色数据")
  66. // public TableDataInfo list(SysRole role)
  67. // {
  68. // startPage();
  69. // List<SysRole> list = roleService.selectRoleList(role);
  70. // return getDataTable(list);
  71. // }
  72. @PreAuthorize("@ss.hasPermi('system:role:list')")
  73. @PostMapping("/list")
  74. @ApiOperation("根据条件分页查询角色数据")
  75. public AjaxResult page(@NotEmpty(message = "数据为空")
  76. @RequestBody SysRoleRequestVo vo) {
  77. LambdaQueryWrapper<SysRole> lw = new LambdaQueryWrapper<SysRole>();
  78. lw.eq(SysRole::getDelFlag,0);
  79. if (!StringUtils.isBlank(vo.getRoleName())) {
  80. lw.like(SysRole::getRoleName, "%" +vo.getRoleName() + "%");
  81. }
  82. if (!StringUtils.isBlank(vo.getRoleKey())) {
  83. lw.like(SysRole::getRoleKey, "%" +vo.getRoleKey() + "%");
  84. }
  85. if (!StringUtils.isBlank(vo.getStatus())) {
  86. lw.eq(SysRole::getStatus, vo.getStatus());
  87. }
  88. IPage<SysRole> page = roleService.page(new Page<>(vo.getPageNum(), vo.getPageSize()), lw);
  89. return success(page);
  90. }
  91. @Log(title = "角色管理", businessType = BusinessType.EXPORT)
  92. @PreAuthorize("@ss.hasPermi('system:role:export')")
  93. @PostMapping("/export")
  94. @ApiOperation("角色管理")
  95. public void export(HttpServletResponse response, SysRole role)
  96. {
  97. List<SysRole> list = roleService.selectRoleList(role);
  98. ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
  99. util.exportExcel(response, list, "角色数据");
  100. }
  101. /**
  102. * 根据角色编号获取详细信息
  103. */
  104. @GetMapping(value = "/{roleId}")
  105. @ApiOperation("根据角色编号获取详细信息")
  106. public AjaxResult getInfo(@PathVariable Long roleId)
  107. {
  108. roleService.checkRoleDataScope(roleId);
  109. return success(roleService.selectRoleById(roleId));
  110. }
  111. /**
  112. * 新增角色
  113. */
  114. // @PreAuthorize("@ss.hasPermi('system:role:add')")
  115. // @Log(title = "角色管理", businessType = BusinessType.INSERT)
  116. // @PostMapping
  117. // @ApiOperation("新增角色")
  118. // public AjaxResult add(@Validated @RequestBody SysRole role)
  119. // {
  120. // if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  121. // {
  122. // return error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
  123. // }
  124. // else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
  125. // {
  126. // return error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
  127. // }
  128. // role.setCreateBy(getUsername());
  129. // return toAjax(roleService.insertRole(role));
  130. //
  131. // }
  132. @PreAuthorize("@ss.hasPermi('system:role:add')")
  133. @Log(title = "角色管理", businessType = BusinessType.INSERT)
  134. @PostMapping
  135. @ApiOperation("新增角色")
  136. public AjaxResult add(@Validated @RequestBody SysRoleRequestVo sysRoleRequestVo)
  137. {
  138. SysRole sysRole = new SysRole();
  139. BeanUtils.copyProperties(sysRoleRequestVo,sysRole);
  140. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(sysRole)))
  141. {
  142. return error("新增角色'" + sysRole.getRoleName() + "'失败,角色名称已存在");
  143. }
  144. if(org.apache.commons.lang3.StringUtils.isBlank(sysRole.getRoleKey())){
  145. sysRole.setRoleKey("common");
  146. }
  147. sysRole.setCreateBy(getUsername());
  148. return toAjax(roleService.addRole(sysRole));
  149. }
  150. @PreAuthorize("@ss.hasPermi('system:role:addDistribution')")
  151. @PostMapping("/saveDistributionModule")
  152. @ApiOperation("新增分配模块")
  153. public AjaxResult saveDistributionModule(@RequestBody SysMenuIdsRequestVo sysMenuIdsRequestVo)
  154. {
  155. return toAjax(roleService.distributionModule(sysMenuIdsRequestVo));
  156. }
  157. // /**
  158. // * 修改保存角色
  159. // */
  160. // @PreAuthorize("@ss.hasPermi('system:role:edit')")
  161. // @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  162. // @PutMapping
  163. // @ApiOperation("修改保存角色")
  164. // public AjaxResult edit(@Validated @RequestBody SysRole role)
  165. // {
  166. // roleService.checkRoleAllowed(role);
  167. // roleService.checkRoleDataScope(role.getRoleId());
  168. // if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  169. // {
  170. // return error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
  171. // }
  172. // else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
  173. // {
  174. // return error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
  175. // }
  176. // role.setUpdateBy(getUsername());
  177. //
  178. // if (roleService.updateRole(role) > 0)
  179. // {
  180. // // 更新缓存用户权限
  181. // LoginUser loginUser = getLoginUser();
  182. // if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin())
  183. // {
  184. // loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser()));
  185. // loginUser.setUser(userService.selectUserByUserName(loginUser.getUser().getUserName()));
  186. // tokenService.setLoginUser(loginUser);
  187. // }
  188. // return success();
  189. // }
  190. // return error("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
  191. // }
  192. /**
  193. * 修改保存角色
  194. */
  195. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  196. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  197. @PutMapping("/edit")
  198. @ApiOperation("修改保存角色")
  199. public AjaxResult edit(@Validated @RequestBody SysRoleRequestVo sysRoleRequestVo)
  200. {
  201. SysRole role = new SysRole();
  202. BeanUtils.copyProperties(sysRoleRequestVo,role);
  203. roleService.checkRoleAllowed(role);
  204. roleService.checkRoleDataScope(role.getRoleId());
  205. if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
  206. {
  207. return error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
  208. }
  209. role.setUpdateBy(getUsername());
  210. return toAjax(roleService.updateRoles(role));
  211. }
  212. /**
  213. * 修改保存数据权限
  214. */
  215. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  216. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  217. @PutMapping("/dataScope")
  218. @ApiOperation("修改保存数据权限")
  219. public AjaxResult dataScope(@RequestBody SysRole role)
  220. {
  221. roleService.checkRoleAllowed(role);
  222. roleService.checkRoleDataScope(role.getRoleId());
  223. return toAjax(roleService.authDataScope(role));
  224. }
  225. @PreAuthorize("@ss.hasPermi('system:role:editDistribution')")
  226. @PostMapping("/updateDistributionModule")
  227. @ApiOperation("修改分配模块")
  228. public AjaxResult updateDistributionModule(@RequestBody SysMenuIdsRequestVo sysMenuIdsRequestVo)
  229. {
  230. if (roleService.updateDistributionModule(sysMenuIdsRequestVo) > 0)
  231. {
  232. // 更新缓存用户权限
  233. LoginUser loginUser = getLoginUser();
  234. if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin())
  235. {
  236. loginUser.setPermissions(permissionService.getMenuPermission(loginUser.getUser()));
  237. loginUser.setUser(userService.selectUserByUserName(loginUser.getUser().getUserName()));
  238. tokenService.setLoginUser(loginUser);
  239. }
  240. return success();
  241. }
  242. return error("修改失败,请联系管理员");
  243. }
  244. // /**
  245. // * 状态修改
  246. // */
  247. // @PreAuthorize("@ss.hasPermi('system:role:edit')")
  248. // @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  249. // @PutMapping("/changeStatus")
  250. // @ApiOperation("状态修改")
  251. // public AjaxResult changeStatus(@RequestBody SysRole role)
  252. // {
  253. // roleService.checkRoleAllowed(role);
  254. // roleService.checkRoleDataScope(role.getRoleId());
  255. // role.setUpdateBy(getUsername());
  256. // return toAjax(roleService.updateRoleStatus(role));
  257. // }
  258. /**
  259. * 状态修改
  260. */
  261. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  262. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  263. @PutMapping("/changeStatus")
  264. @ApiOperation("批量状态修改")
  265. public AjaxResult changeStatus(@RequestBody List<SysStatusRequestVo> sysStatusRequestVoList)
  266. {
  267. return toAjax(roleService.changeStatus(sysStatusRequestVoList));
  268. }
  269. /**
  270. * 删除角色
  271. */
  272. @PreAuthorize("@ss.hasPermi('system:role:remove')")
  273. @Log(title = "角色管理", businessType = BusinessType.DELETE)
  274. @DeleteMapping("/{roleIds}")
  275. @ApiOperation("删除角色")
  276. public AjaxResult remove(@PathVariable Long[] roleIds)
  277. {
  278. return toAjax(roleService.deleteRoleByIds(roleIds));
  279. }
  280. /**
  281. * 获取角色选择框列表
  282. */
  283. @GetMapping("/optionselect")
  284. @ApiOperation("获取角色选择框列表")
  285. public AjaxResult optionselect()
  286. {
  287. return success(roleService.selectRoleAll());
  288. }
  289. /**
  290. * 查询已分配用户角色列表
  291. */
  292. @PreAuthorize("@ss.hasPermi('system:role:list')")
  293. @GetMapping("/authUser/allocatedList")
  294. @ApiOperation("查询已分配用户角色列表")
  295. public TableDataInfo allocatedList(SysUser user)
  296. {
  297. startPage();
  298. List<SysUser> list = userService.selectAllocatedList(user);
  299. return getDataTable(list);
  300. }
  301. /**
  302. * 查询未分配用户角色列表
  303. */
  304. @PreAuthorize("@ss.hasPermi('system:role:list')")
  305. @GetMapping("/authUser/unallocatedList")
  306. @ApiOperation("查询未分配用户角色列表")
  307. public TableDataInfo unallocatedList(SysUser user)
  308. {
  309. startPage();
  310. List<SysUser> list = userService.selectUnallocatedList(user);
  311. return getDataTable(list);
  312. }
  313. /**
  314. * 取消授权用户
  315. */
  316. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  317. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  318. @PutMapping("/authUser/cancel")
  319. @ApiOperation("取消授权用户")
  320. public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole)
  321. {
  322. return toAjax(roleService.deleteAuthUser(userRole));
  323. }
  324. /**
  325. * 批量取消授权用户
  326. */
  327. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  328. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  329. @PutMapping("/authUser/cancelAll")
  330. @ApiOperation("批量取消授权用户")
  331. public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds)
  332. {
  333. return toAjax(roleService.deleteAuthUsers(roleId, userIds));
  334. }
  335. /**
  336. * 批量选择用户授权
  337. */
  338. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  339. @Log(title = "角色管理", businessType = BusinessType.GRANT)
  340. @PutMapping("/authUser/selectAll")
  341. @ApiOperation("批量选择用户授权")
  342. public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds)
  343. {
  344. roleService.checkRoleDataScope(roleId);
  345. return toAjax(roleService.insertAuthUsers(roleId, userIds));
  346. }
  347. /**
  348. * 获取对应角色部门树列表
  349. */
  350. @PreAuthorize("@ss.hasPermi('system:role:query')")
  351. @GetMapping(value = "/deptTree/{roleId}")
  352. @ApiOperation("获取对应角色部门树列表")
  353. public AjaxResult deptTree(@PathVariable("roleId") Long roleId)
  354. {
  355. AjaxResult ajax = AjaxResult.success();
  356. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  357. ajax.put("depts", deptService.selectDeptTreeList(new SysDept()));
  358. return ajax;
  359. }
  360. }