RoleController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.modules.system.rest;
  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.modules.system.domain.Role;
  23. import me.zhengjie.exception.BadRequestException;
  24. import me.zhengjie.modules.system.service.RoleService;
  25. import me.zhengjie.modules.system.service.dto.RoleDto;
  26. import me.zhengjie.modules.system.service.dto.RoleQueryCriteria;
  27. import me.zhengjie.modules.system.service.dto.RoleSmallDto;
  28. import me.zhengjie.utils.SecurityUtils;
  29. import org.springframework.data.domain.Pageable;
  30. import org.springframework.http.HttpStatus;
  31. import org.springframework.http.ResponseEntity;
  32. import org.springframework.security.access.prepost.PreAuthorize;
  33. import org.springframework.validation.annotation.Validated;
  34. import org.springframework.web.bind.annotation.*;
  35. import javax.servlet.http.HttpServletResponse;
  36. import java.io.IOException;
  37. import java.util.Collections;
  38. import java.util.List;
  39. import java.util.Set;
  40. import java.util.stream.Collectors;
  41. /**
  42. * @author Zheng Jie
  43. * @date 2018-12-03
  44. */
  45. @RestController
  46. @RequiredArgsConstructor
  47. @Api(tags = "系统:角色管理")
  48. @RequestMapping("/api/roles")
  49. public class RoleController {
  50. private final RoleService roleService;
  51. private static final String ENTITY_NAME = "role";
  52. @ApiOperation("获取单个role")
  53. @GetMapping(value = "/{id}")
  54. @PreAuthorize("@el.check('roles:list')")
  55. public ResponseEntity<Object> query(@PathVariable Long id){
  56. return new ResponseEntity<>(roleService.findById(id), HttpStatus.OK);
  57. }
  58. @ApiOperation("导出角色数据")
  59. @GetMapping(value = "/download")
  60. @PreAuthorize("@el.check('role:list')")
  61. public void download(HttpServletResponse response, RoleQueryCriteria criteria) throws IOException {
  62. roleService.download(roleService.queryAll(criteria), response);
  63. }
  64. @ApiOperation("返回全部的角色")
  65. @GetMapping(value = "/all")
  66. @PreAuthorize("@el.check('roles:list','user:add','user:edit')")
  67. public ResponseEntity<Object> query(){
  68. return new ResponseEntity<>(roleService.queryAll(),HttpStatus.OK);
  69. }
  70. @ApiOperation("查询角色")
  71. @GetMapping
  72. @PreAuthorize("@el.check('roles:list')")
  73. public ResponseEntity<Object> query(RoleQueryCriteria criteria, Pageable pageable){
  74. return new ResponseEntity<>(roleService.queryAll(criteria,pageable),HttpStatus.OK);
  75. }
  76. @ApiOperation("获取用户级别")
  77. @GetMapping(value = "/level")
  78. public ResponseEntity<Object> getLevel(){
  79. return new ResponseEntity<>(Dict.create().set("level", getLevels(null)),HttpStatus.OK);
  80. }
  81. @Log("新增角色")
  82. @ApiOperation("新增角色")
  83. @PostMapping
  84. @PreAuthorize("@el.check('roles:add')")
  85. public ResponseEntity<Object> create(@Validated @RequestBody Role resources){
  86. if (resources.getId() != null) {
  87. throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
  88. }
  89. getLevels(resources.getLevel());
  90. roleService.create(resources);
  91. return new ResponseEntity<>(HttpStatus.CREATED);
  92. }
  93. @Log("修改角色")
  94. @ApiOperation("修改角色")
  95. @PutMapping
  96. @PreAuthorize("@el.check('roles:edit')")
  97. public ResponseEntity<Object> update(@Validated(Role.Update.class) @RequestBody Role resources){
  98. getLevels(resources.getLevel());
  99. roleService.update(resources);
  100. return new ResponseEntity<>(HttpStatus.NO_CONTENT);
  101. }
  102. @Log("修改角色菜单")
  103. @ApiOperation("修改角色菜单")
  104. @PutMapping(value = "/menu")
  105. @PreAuthorize("@el.check('roles:edit')")
  106. public ResponseEntity<Object> updateMenu(@RequestBody Role resources){
  107. RoleDto role = roleService.findById(resources.getId());
  108. getLevels(role.getLevel());
  109. roleService.updateMenu(resources,role);
  110. return new ResponseEntity<>(HttpStatus.NO_CONTENT);
  111. }
  112. @Log("删除角色")
  113. @ApiOperation("删除角色")
  114. @DeleteMapping
  115. @PreAuthorize("@el.check('roles:del')")
  116. public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
  117. for (Long id : ids) {
  118. RoleDto role = roleService.findById(id);
  119. getLevels(role.getLevel());
  120. }
  121. // 验证是否被用户关联
  122. roleService.verification(ids);
  123. roleService.delete(ids);
  124. return new ResponseEntity<>(HttpStatus.OK);
  125. }
  126. /**
  127. * 获取用户的角色级别
  128. * @return /
  129. */
  130. private int getLevels(Integer level){
  131. List<Integer> levels = roleService.findByUsersId(SecurityUtils.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList());
  132. int min = Collections.min(levels);
  133. if(level != null){
  134. if(level < min){
  135. throw new BadRequestException("权限不足,你的角色级别:" + min + ",低于操作的角色级别:" + level);
  136. }
  137. }
  138. return min;
  139. }
  140. }