RoleController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2019-2020 Zheng Jie
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package me.zhengjie.application.admin.controller;
  17. import cn.hutool.core.lang.Dict;
  18. import io.swagger.annotations.Api;
  19. import io.swagger.annotations.ApiOperation;
  20. import lombok.RequiredArgsConstructor;
  21. import me.zhengjie.annotation.Log;
  22. import me.zhengjie.base.util.ApplicationContextUtil;
  23. import me.zhengjie.dao.mybatis.entity.Role;
  24. import me.zhengjie.exception.BadRequestException;
  25. import me.zhengjie.application.admin.service.RoleService;
  26. import me.zhengjie.application.admin.service.dto.RoleDto;
  27. import me.zhengjie.application.admin.service.dto.RoleQueryCriteria;
  28. import me.zhengjie.application.admin.service.dto.RoleSmallDto;
  29. import me.zhengjie.utils.SecurityUtils;
  30. import org.springframework.data.domain.Pageable;
  31. import org.springframework.http.HttpStatus;
  32. import org.springframework.http.ResponseEntity;
  33. import org.springframework.security.access.prepost.PreAuthorize;
  34. import org.springframework.validation.annotation.Validated;
  35. import org.springframework.web.bind.annotation.*;
  36. import javax.servlet.http.HttpServletResponse;
  37. import java.io.IOException;
  38. import java.util.Collections;
  39. import java.util.List;
  40. import java.util.Set;
  41. import java.util.stream.Collectors;
  42. /**
  43. * @author Zheng Jie
  44. * @date 2018-12-03
  45. */
  46. @RestController
  47. @RequiredArgsConstructor
  48. @Api(tags = "系统:角色管理")
  49. @RequestMapping("/api/roles")
  50. public class RoleController {
  51. private final RoleService roleService;
  52. private final ApplicationContextUtil contextUtil;
  53. private static final String ENTITY_NAME = "role";
  54. @ApiOperation("获取单个role")
  55. @GetMapping(value = "/{id}")
  56. @PreAuthorize("@el.check('roles:list')")
  57. public ResponseEntity<Object> findRoleById(@PathVariable Long id){
  58. return new ResponseEntity<>(roleService.findById(id), HttpStatus.OK);
  59. }
  60. @ApiOperation("导出角色数据")
  61. @GetMapping(value = "/download")
  62. @PreAuthorize("@el.check('role:list')")
  63. public void exportRole(HttpServletResponse response, RoleQueryCriteria criteria) throws IOException {
  64. roleService.download(roleService.queryAll(criteria), response);
  65. }
  66. @ApiOperation("返回全部的角色")
  67. @GetMapping(value = "/all")
  68. @PreAuthorize("@el.check('roles:list','user:add','user:edit')")
  69. public ResponseEntity<Object> queryAllRole(){
  70. return new ResponseEntity<>(roleService.queryAll(),HttpStatus.OK);
  71. }
  72. @ApiOperation("查询角色")
  73. @GetMapping
  74. @PreAuthorize("@el.check('roles:list')")
  75. public ResponseEntity<Object> queryRole(RoleQueryCriteria criteria, Pageable pageable){
  76. return new ResponseEntity<>(roleService.queryAll(criteria,pageable),HttpStatus.OK);
  77. }
  78. @ApiOperation("获取用户级别")
  79. @GetMapping(value = "/level")
  80. public ResponseEntity<Object> getRoleLevel(){
  81. return new ResponseEntity<>(Dict.create().set("level", getLevels(null)),HttpStatus.OK);
  82. }
  83. @Log("新增角色")
  84. @ApiOperation("新增角色")
  85. @PostMapping
  86. @PreAuthorize("@el.check('roles:add')")
  87. public ResponseEntity<Object> createRole(@Validated @RequestBody Role resources){
  88. if (resources.getId() != null) {
  89. throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
  90. }
  91. getLevels(resources.getLevel());
  92. roleService.create(resources);
  93. return new ResponseEntity<>(HttpStatus.CREATED);
  94. }
  95. @Log("修改角色")
  96. @ApiOperation("修改角色")
  97. @PutMapping
  98. @PreAuthorize("@el.check('roles:edit')")
  99. public ResponseEntity<Object> updateRole(@Validated(Role.Update.class) @RequestBody Role resources){
  100. getLevels(resources.getLevel());
  101. roleService.update(resources);
  102. return new ResponseEntity<>(HttpStatus.NO_CONTENT);
  103. }
  104. @Log("修改角色菜单")
  105. @ApiOperation("修改角色菜单")
  106. @PutMapping(value = "/menu")
  107. @PreAuthorize("@el.check('roles:edit')")
  108. public ResponseEntity<Object> updateRoleMenu(@RequestBody Role resources){
  109. RoleDto role = roleService.findById(resources.getId());
  110. getLevels(role.getLevel());
  111. roleService.updateMenu(resources,role);
  112. return new ResponseEntity<>(HttpStatus.NO_CONTENT);
  113. }
  114. @Log("删除角色")
  115. @ApiOperation("删除角色")
  116. @DeleteMapping
  117. @PreAuthorize("@el.check('roles:del')")
  118. public ResponseEntity<Object> deleteRole(@RequestBody Set<Long> ids){
  119. for (Long id : ids) {
  120. RoleDto role = roleService.findById(id);
  121. getLevels(role.getLevel());
  122. }
  123. // 验证是否被用户关联
  124. roleService.verification(ids);
  125. roleService.delete(ids);
  126. return new ResponseEntity<>(HttpStatus.OK);
  127. }
  128. /**
  129. * 获取用户的角色级别
  130. * @return /
  131. */
  132. private int getLevels(Integer level){
  133. List<Integer> levels = roleService.findByUsersId(contextUtil.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList());
  134. int min = Collections.min(levels);
  135. if(level != null){
  136. if(level < min){
  137. throw new BadRequestException("权限不足,你的角色级别:" + min + ",低于操作的角色级别:" + level);
  138. }
  139. }
  140. return min;
  141. }
  142. }