ProjectGroupController、StaffController重构

master
shmily744 2022-12-27 19:35:56 +08:00
parent ee3e03941c
commit dd681a9f20
6 changed files with 51 additions and 37 deletions

View File

@ -1,10 +1,10 @@
package cn.edu.hfut.rmdjzz.projectmanagement.controller; package cn.edu.hfut.rmdjzz.projectmanagement.controller;
import cn.edu.hfut.rmdjzz.projectmanagement.annotation.ProjectAuthorize;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.ProjectGroup; import cn.edu.hfut.rmdjzz.projectmanagement.entity.ProjectGroup;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.ProjectGroupDTO; import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.ProjectGroupDTO;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.vo.GroupPositionVO; import cn.edu.hfut.rmdjzz.projectmanagement.entity.vo.GroupPositionVO;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException; import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService; import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IStaffService; import cn.edu.hfut.rmdjzz.projectmanagement.service.IStaffService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.ITaskService; import cn.edu.hfut.rmdjzz.projectmanagement.service.ITaskService;
@ -38,32 +38,41 @@ public class ProjectGroupController {
@Operation(description = "传入合法page参数时分页查询否则拉取整个列表") @Operation(description = "传入合法page参数时分页查询否则拉取整个列表")
@SneakyThrows @SneakyThrows
@ProjectAuthorize("a != 0")
@GetMapping @GetMapping
public ResponseList<ProjectGroupDTO> getGroupMembers( public ResponseList<ProjectGroupDTO> getGroupMembers(
@PathVariable Integer projectId, @PathVariable Integer projectId,
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
RequestPage page RequestPage page
) { ) {
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) { // if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT); // throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
} // }
// if (validateUtils.validate(page).isEmpty()) {
// return ResponseList.ofSuccess(projectGroupService.pageProjectMembers(page, projectId));
// }
//return ResponseList.ofSuccess(projectGroupService.listProjectMembers(projectId));
ResponseList<ProjectGroupDTO> groupMembers;
if (validateUtils.validate(page).isEmpty()) { if (validateUtils.validate(page).isEmpty()) {
return ResponseList.ofSuccess(projectGroupService.pageProjectMembers(page, projectId)); groupMembers = ResponseList.ofSuccess(projectGroupService.pageProjectMembers(page, projectId));
} else {
groupMembers = ResponseList.ofSuccess(projectGroupService.listProjectMembers(projectId));
} }
return ResponseList.ofSuccess(projectGroupService.listProjectMembers(projectId)); return groupMembers;
} }
@SneakyThrows @SneakyThrows
@ProjectAuthorize("a != 0")
@GetMapping("/{staffId}") @GetMapping("/{staffId}")
public ResponseMap getDesignatedStaffPosition( public ResponseMap getDesignatedStaffPosition(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId, @PathVariable Integer projectId,
@PathVariable Integer staffId @PathVariable Integer staffId
) { ) {
if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) { // if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT); // throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
} // }
ProjectGroup designatedStaff = projectGroupService.getOne( ProjectGroup designatedStaff = projectGroupService.getOne(
Wrappers.<ProjectGroup>lambdaQuery() Wrappers.<ProjectGroup>lambdaQuery()
.eq(ProjectGroup::getStaffId, staffId) .eq(ProjectGroup::getStaffId, staffId)
@ -78,11 +87,12 @@ public class ProjectGroupController {
@SneakyThrows @SneakyThrows
@PostMapping @PostMapping
public ResponseMap addGroupMember( public ResponseMap addGroupMember(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@RequestAttribute Integer accessLevel,
@PathVariable Integer projectId, @PathVariable Integer projectId,
@RequestBody GroupPositionVO groupPosition @RequestBody GroupPositionVO groupPosition
) { ) {
if (projectGroupService.insertNewMember(token, projectId, groupPosition.getStaffUsername(), groupPosition.getProjectStaffPosition())) { if (projectGroupService.insertNewMember(accessLevel, projectId, groupPosition.getStaffUsername(), groupPosition.getProjectStaffPosition())) {
return ResponseMap.ofSuccess(); return ResponseMap.ofSuccess();
} }
throw new BadRequestException(BadRequestException.OPERATE_FAILED); throw new BadRequestException(BadRequestException.OPERATE_FAILED);
@ -91,11 +101,12 @@ public class ProjectGroupController {
@SneakyThrows @SneakyThrows
@DeleteMapping("/{staffId}") @DeleteMapping("/{staffId}")
public ResponseMap deleteGroupMember( public ResponseMap deleteGroupMember(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@RequestAttribute Integer accessLevel,
@PathVariable Integer projectId, @PathVariable Integer projectId,
@PathVariable Integer staffId @PathVariable Integer staffId
) { ) {
if (projectGroupService.removeMember(token, projectId, staffId)) { if (projectGroupService.removeMember(accessLevel, projectId, staffId)) {
return ResponseMap.ofSuccess(); return ResponseMap.ofSuccess();
} }
throw new BadRequestException(BadRequestException.OPERATE_FAILED); throw new BadRequestException(BadRequestException.OPERATE_FAILED);
@ -105,12 +116,13 @@ public class ProjectGroupController {
@SneakyThrows @SneakyThrows
@PutMapping("/{staffId}") @PutMapping("/{staffId}")
public ResponseMap modifyDesignatedStaffPosition( public ResponseMap modifyDesignatedStaffPosition(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@RequestAttribute Integer accessLevel,
@PathVariable Integer projectId, @PathVariable Integer projectId,
@PathVariable Integer staffId, @PathVariable Integer staffId,
@RequestBody GroupPositionVO groupPosition @RequestBody GroupPositionVO groupPosition
) { ) {
if (projectGroupService.updateStaffPositions(token, projectId, staffId, groupPosition.getProjectStaffPosition())) { if (projectGroupService.updateStaffPositions(accessLevel, projectId, staffId, groupPosition.getProjectStaffPosition())) {
return ResponseMap.ofSuccess(); return ResponseMap.ofSuccess();
} }
throw new BadRequestException(BadRequestException.OPERATE_FAILED); throw new BadRequestException(BadRequestException.OPERATE_FAILED);
@ -119,10 +131,11 @@ public class ProjectGroupController {
@SneakyThrows @SneakyThrows
@GetMapping("/stats") @GetMapping("/stats")
public ResponseMap getGroupPositionsStatistics( public ResponseMap getGroupPositionsStatistics(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@RequestAttribute Integer accessLevel,
@PathVariable Integer projectId @PathVariable Integer projectId
) { ) {
return ResponseMap.ofSuccess(projectGroupService.collectStatsForGroupPositions(token, projectId)); return ResponseMap.ofSuccess(projectGroupService.collectStatsForGroupPositions(accessLevel, projectId));
} }
@Operation(description = "请求体是一个key为taskIdvalue为staffId的map") @Operation(description = "请求体是一个key为taskIdvalue为staffId的map")

View File

@ -52,7 +52,8 @@ public class StaffController {
@SneakyThrows @SneakyThrows
@PostMapping(value = "/import") @PostMapping(value = "/import")
public ResponseMap importStaffs( public ResponseMap importStaffs(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token, // @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@RequestAttribute Integer staffGlobalLevel,
@RequestParam("fileDigest") String digest, @RequestParam("fileDigest") String digest,
@RequestParam("uploadFile") MultipartFile uploadFile @RequestParam("uploadFile") MultipartFile uploadFile
) { ) {
@ -66,7 +67,7 @@ public class StaffController {
if (!Objects.equals(DigestUtils.md5DigestAsHex(uploadFile.getBytes()).toLowerCase(), digest.toLowerCase())) { if (!Objects.equals(DigestUtils.md5DigestAsHex(uploadFile.getBytes()).toLowerCase(), digest.toLowerCase())) {
throw new BadRequestException("文件传输错误"); throw new BadRequestException("文件传输错误");
} }
Integer successCount = staffService.multiImport(token, uploadFile); Integer successCount = staffService.multiImport(staffGlobalLevel, uploadFile);
return ResponseMap.ofSuccess("成功导入" + successCount + "条数据"); return ResponseMap.ofSuccess("成功导入" + successCount + "条数据");
} }

View File

@ -24,16 +24,16 @@ public interface IProjectGroupService extends IService<ProjectGroup> {
Boolean addCreator(Integer projectId, Integer staffId); Boolean addCreator(Integer projectId, Integer staffId);
Boolean insertNewMember(String token, Integer projectId, String staffUsername, String positions) throws ForbiddenException, BadRequestException; Boolean insertNewMember(int accessLevel, Integer projectId, String staffUsername, String positions) throws ForbiddenException, BadRequestException;
Boolean removeMember(String token, Integer projectId, Integer targetId) throws ForbiddenException, BadRequestException; Boolean removeMember(Integer accessLevel, Integer projectId, Integer targetId) throws ForbiddenException, BadRequestException;
Boolean updateStaffPositions(String token, Integer projectId, Integer targetId, String positions) throws ForbiddenException, BadRequestException; Boolean updateStaffPositions(Integer accessLevel, Integer projectId, Integer targetId, String positions) throws ForbiddenException, BadRequestException;
/** /**
* @return 0AccessLevel11 * @return 0AccessLevel11
*/ */
Integer getProjectAccessLevel(String token, Integer projectId); Integer getProjectAccessLevel(String staffId, Integer projectId);
Integer getProjectAccessLevel(Integer staffId, Integer staffGlobalLevel, Integer projectId); Integer getProjectAccessLevel(Integer staffId, Integer staffGlobalLevel, Integer projectId);
@ -60,6 +60,6 @@ public interface IProjectGroupService extends IService<ProjectGroup> {
* *
* @return <, > * @return <, >
*/ */
Map<String, Integer> collectStatsForGroupPositions(String token, Integer projectId) throws ForbiddenException; Map<String, Integer> collectStatsForGroupPositions(Integer accessLevel, Integer projectId) throws ForbiddenException;
} }

View File

@ -23,5 +23,5 @@ public interface IStaffService extends IService<Staff> {
Boolean logout(String token) throws TokenException; Boolean logout(String token) throws TokenException;
Integer multiImport(String token, MultipartFile file) throws BadRequestException, ForbiddenException; Integer multiImport(Integer staffGlobalLevel, MultipartFile file) throws BadRequestException, ForbiddenException;
} }

View File

@ -44,11 +44,11 @@ public class ProjectGroupServiceImpl extends ServiceImpl<ProjectGroupMapper, Pro
} }
@Override @Override
public Boolean insertNewMember(String token, Integer projectId, String targetUsername, String positions) throws ForbiddenException, BadRequestException { public Boolean insertNewMember(int accessLevel, Integer projectId, String targetUsername, String positions) throws ForbiddenException, BadRequestException {
if (targetUsername.equals("root")) { if (targetUsername.equals("root")) {
throw new BadRequestException(IStaffService.STAFF_DOES_NOT_EXIST); throw new BadRequestException(IStaffService.STAFF_DOES_NOT_EXIST);
} }
int accessLevel = getProjectAccessLevel(token, projectId); //int accessLevel = getProjectAccessLevel(token, projectId);
int targetLevel = 3; int targetLevel = 3;
Staff targetStaff = staffService.getOne(Wrappers.<Staff>lambdaQuery().eq(Staff::getStaffUsername, targetUsername)); Staff targetStaff = staffService.getOne(Wrappers.<Staff>lambdaQuery().eq(Staff::getStaffUsername, targetUsername));
@ -84,8 +84,8 @@ public class ProjectGroupServiceImpl extends ServiceImpl<ProjectGroupMapper, Pro
} }
@Override @Override
public Boolean removeMember(String token, Integer projectId, Integer targetId) throws ForbiddenException, BadRequestException { public Boolean removeMember(Integer accessLevel, Integer projectId, Integer targetId) throws ForbiddenException, BadRequestException {
if (compareProjectAccessLevel(projectId, token, targetId) >= 0) { if (accessLevel >= getProjectAccessLevelIgnoreGlobalLevel(targetId, projectId)) {
throw new ForbiddenException(ForbiddenException.UNABLE_TO_OPERATE); throw new ForbiddenException(ForbiddenException.UNABLE_TO_OPERATE);
} }
Long taskUnfinishedCount = baseMapper.selectUnfinishedTaskCountByStaffId(projectId, targetId); Long taskUnfinishedCount = baseMapper.selectUnfinishedTaskCountByStaffId(projectId, targetId);
@ -98,8 +98,8 @@ public class ProjectGroupServiceImpl extends ServiceImpl<ProjectGroupMapper, Pro
} }
@Override @Override
public Boolean updateStaffPositions(String token, Integer projectId, Integer targetId, String positions) throws ForbiddenException, BadRequestException { public Boolean updateStaffPositions(Integer accessLevel, Integer projectId, Integer targetId, String positions) throws ForbiddenException, BadRequestException {
int accessLevel = getProjectAccessLevel(token, projectId); //int accessLevel = getProjectAccessLevel(token, projectId);
//int originTargetLevel = getProjectAccessLevelIgnoreGlobalLevel(targetId, projectId); //int originTargetLevel = getProjectAccessLevelIgnoreGlobalLevel(targetId, projectId);
ProjectGroup originProjectGroup = baseMapper.selectOne(Wrappers.<ProjectGroup>lambdaQuery() ProjectGroup originProjectGroup = baseMapper.selectOne(Wrappers.<ProjectGroup>lambdaQuery()
.select(ProjectGroup::getProjectStaffPosition, ProjectGroup::getProjectAccessLevel) .select(ProjectGroup::getProjectStaffPosition, ProjectGroup::getProjectAccessLevel)
@ -199,8 +199,8 @@ public class ProjectGroupServiceImpl extends ServiceImpl<ProjectGroupMapper, Pro
} }
@Override @Override
public Map<String, Integer> collectStatsForGroupPositions(String token, Integer projectId) throws ForbiddenException { public Map<String, Integer> collectStatsForGroupPositions(Integer accessLevel, Integer projectId) throws ForbiddenException {
if (getProjectAccessLevel(token, projectId) == 0) { if (accessLevel == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT); throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
} }
Map<String, Integer> res = new HashMap<>(); Map<String, Integer> res = new HashMap<>();

View File

@ -120,8 +120,8 @@ public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements
@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class) @Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
@Override @Override
public Integer multiImport(String token, MultipartFile file) throws BadRequestException, ForbiddenException { public Integer multiImport(Integer staffGlobalLevel, MultipartFile file) throws BadRequestException, ForbiddenException {
if (TokenUtils.getStaffGlobalLevel(token) != 1) { if (staffGlobalLevel != 1) {
throw new ForbiddenException(ForbiddenException.UNABLE_TO_OPERATE); throw new ForbiddenException(ForbiddenException.UNABLE_TO_OPERATE);
} }