彻底将鉴权部分挪移到切面处

master
ArgonarioD 2023-01-13 22:45:21 +08:00
parent ba6f19544e
commit f607650c73
12 changed files with 43 additions and 57 deletions

View File

@ -4,10 +4,7 @@ import cn.edu.hfut.rmdjzz.projectmanagement.annotation.ProjectAuthorize;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Announcement;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.AnnouncementDTO;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IAnnouncementService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseList;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
import lombok.SneakyThrows;
@ -24,8 +21,6 @@ public class AnnouncementController {
@Autowired
private IAnnouncementService announcementService;
@Autowired
private IProjectGroupService projectGroupService;
@SneakyThrows
@ProjectAuthorize("a != 0")

View File

@ -5,10 +5,7 @@ import cn.edu.hfut.rmdjzz.projectmanagement.annotation.RateLimit;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Project;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.dto.ProjectDTO;
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.IProjectService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.RequestPage;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseList;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
@ -35,8 +32,6 @@ public class ProjectController {
@Autowired
private IProjectService projectService;
@Autowired
private IProjectGroupService projectGroupService;
@Operation(summary = "根据Token获取该员工的ProjectList")
@RateLimit(key = "ProjectPage", permitsPerSecond = 50, maxBurstSeconds = 15, timeout = 1200)
@ -50,7 +45,7 @@ public class ProjectController {
@Parameter(description = "参数列表见Project实体类时间可以用xxxxStart与xxxxEnd来确定区间"
, required = true) @RequestParam("paramMap") Map<String, Object> paramMap
) {
if(globalAccessLevel == 1) staffId = null;
if (globalAccessLevel == 1) staffId = null;
Page<ProjectDTO> result = projectService.pageMyProjects(staffId, page, paramMap);
return ResponseList.ofSuccess(result);
}
@ -81,9 +76,9 @@ public class ProjectController {
@PostMapping("/complete/{projectId}")
public ResponseMap completeProject(
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@RequestAttribute Project targetProject,
@RequestAttribute Project targetProject/*,
@Parameter(description = "只需要传projectId即可{\"projectId\": 1}")
@PathVariable Integer projectId
@PathVariable Integer projectId*/
) {
if (targetProject.getCompleted())
throw new BadRequestException(PROJECT_COMPLETED);

View File

@ -8,7 +8,6 @@ import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IStaffService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.ITaskService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.ValidateUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.RequestPage;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseList;
@ -85,6 +84,7 @@ public class ProjectGroupController {
@Operation(description = "body中只传staffUsername和projectStaffPosition")
@SneakyThrows
@ProjectAuthorize("a == 1")
@PostMapping
public ResponseMap addGroupMember(
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@ -99,6 +99,7 @@ public class ProjectGroupController {
}
@SneakyThrows
@ProjectAuthorize("a == 1")
@DeleteMapping("/{staffId}")
public ResponseMap deleteGroupMember(
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@ -114,6 +115,7 @@ public class ProjectGroupController {
@Operation(description = "body中只传projectStaffPosition")
@SneakyThrows
@ProjectAuthorize("a > 0 && a <= 2")
@PutMapping("/{staffId}")
public ResponseMap modifyDesignatedStaffPosition(
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@ -140,14 +142,15 @@ public class ProjectGroupController {
@Operation(description = "请求体是一个key为taskIdvalue为staffId的map")
@SneakyThrows
@ProjectAuthorize("a > 0 && a <= 2")
@PutMapping("/{staffId}/transfer")
public ResponseMap transferStaffTasks(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId,
@PathVariable Integer staffId,
@RequestBody Map<Long, Integer> transferMap
) {
if (taskService.transferStaffTasks(token, projectId, staffId, transferMap)) {
if (taskService.transferStaffTasks(projectId, staffId, transferMap)) {
return ResponseMap.ofSuccess();
}
throw new BadRequestException(BadRequestException.OPERATE_FAILED);
@ -155,14 +158,15 @@ public class ProjectGroupController {
@Operation(description = "将staffId的所有工作项转移至targetStaffId")
@SneakyThrows
@ProjectAuthorize("a > 0 && a <= 2")
@PutMapping("/{staffId}/transfer/{targetStaffId}")
public ResponseMap transferTasksToSingleStaff(
@RequestHeader(TokenUtils.HEADER_TOKEN) String token,
// @RequestHeader(TokenUtils.HEADER_TOKEN) String token,
@PathVariable Integer projectId,
@PathVariable Integer staffId,
@PathVariable Integer targetStaffId
) {
if (taskService.transferTasksToSingleStaff(token, projectId, staffId, targetStaffId)) {
if (taskService.transferTasksToSingleStaff(projectId, staffId, targetStaffId)) {
return ResponseMap.ofSuccess();
}
throw new BadRequestException(BadRequestException.OPERATE_FAILED);

View File

@ -10,8 +10,6 @@ import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.ITaskService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.impl.TaskServiceImpl;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseList;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
import lombok.SneakyThrows;

View File

@ -2,7 +2,6 @@ package cn.edu.hfut.rmdjzz.projectmanagement.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**

View File

@ -1,7 +1,5 @@
package cn.edu.hfut.rmdjzz.projectmanagement.entity.dto;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data;
import java.time.LocalDateTime;

View File

@ -2,7 +2,6 @@ package cn.edu.hfut.rmdjzz.projectmanagement.entity.dto;
import cn.edu.hfut.rmdjzz.projectmanagement.entity.Task;
import lombok.Data;
import lombok.NonNull;
import java.time.LocalDate;
import java.util.Objects;
@ -18,15 +17,16 @@ public class TaskTrendDTO {
Long taskNum;
public void changeForUnclosed(TaskTrendDTO taskTrend) {
if(Objects.equals(Task.STATUS_UNCLOSED, taskStatus)) {
if (Objects.equals(Task.STATUS_UNCLOSED, taskStatus)) {
taskNum += taskTrend.getTaskNum();
}
}
public void changeForClosed(TaskTrendDTO taskTrend) {
if(Objects.equals(Task.STATUS_UNCLOSED, taskStatus)) {
if (Objects.equals(Task.STATUS_UNCLOSED, taskStatus)) {
taskNum -= taskTrend.getTaskNum();
} else {
if(Objects.equals(taskTrend.getTaskStatus(), taskStatus)) {
if (Objects.equals(taskTrend.getTaskStatus(), taskStatus)) {
taskNum += taskTrend.getTaskNum();
}
}

View File

@ -1,5 +1,6 @@
package cn.edu.hfut.rmdjzz.projectmanagement.interceptor;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@ -7,8 +8,6 @@ import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Iterator;
/**
* @author
@ -18,7 +17,7 @@ import java.util.Iterator;
public class CorsInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, @NotNull Object handler) {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "*");
response.setHeader("Access-Control-Allow-Headers", "Content-Type,Token");

View File

@ -15,6 +15,7 @@ import java.util.Map;
* @author
* created at 2022/7/4 14:49
*/
@SuppressWarnings("UnusedReturnValue")
public interface ITaskService extends IService<Task> {
List<TaskDTO> listSubtasks(Integer projectId, Long fatherId) throws BadRequestException, ForbiddenException;
@ -48,9 +49,9 @@ public interface ITaskService extends IService<Task> {
Map<String, List<TaskTrendDTO>> getProjectTaskTrend(Integer projectId) throws ForbiddenException;
Boolean transferStaffTasks(String token, Integer projectId, Integer transferredStaffId, Map<Long, Integer> transferMap) throws ForbiddenException, BadRequestException;
Boolean transferStaffTasks(Integer projectId, Integer transferredStaffId, Map<Long, Integer> transferMap) throws ForbiddenException, BadRequestException;
Boolean transferTasksToSingleStaff(String token, Integer projectId, Integer transferredStaffId, Integer targetStaffId) throws ForbiddenException, BadRequestException;
Boolean transferTasksToSingleStaff(Integer projectId, Integer transferredStaffId, Integer targetStaffId) throws ForbiddenException, BadRequestException;
Map<String, Long> getProjectProcessOfEveryone(Integer projectId) throws ForbiddenException;
}

View File

@ -114,12 +114,12 @@ public class ProjectGroupServiceImpl extends ServiceImpl<ProjectGroupMapper, Pro
}
int targetLevel = 3;
if (accessLevel == 0) {
/*if (accessLevel == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
}
if (accessLevel > 2 || accessLevel >= originProjectGroup.getProjectAccessLevel()) {
throw new ForbiddenException(ForbiddenException.UNABLE_TO_OPERATE);
}
}*/
String[] positionArray = positions.split(",");
for (String position : positionArray) {
position = position.strip();

View File

@ -8,7 +8,6 @@ import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.mapper.ProjectMapper;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.WrapperUtils;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.RequestPage;
import com.baomidou.mybatisplus.core.metadata.IPage;
@ -63,8 +62,7 @@ public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> impl
@Override
public Boolean setProjectCompleted(
Project targetProject
) throws BadRequestException, ForbiddenException
{
) throws BadRequestException, ForbiddenException {
// Project targetProject = baseMapper.selectById(project.getProjectId());
// if (targetProject == null)
// throw new BadRequestException(BadRequestException.WRONG_PARAMETERS);

View File

@ -10,7 +10,6 @@ import cn.edu.hfut.rmdjzz.projectmanagement.exception.ForbiddenException;
import cn.edu.hfut.rmdjzz.projectmanagement.mapper.TaskMapper;
import cn.edu.hfut.rmdjzz.projectmanagement.service.IProjectGroupService;
import cn.edu.hfut.rmdjzz.projectmanagement.service.ITaskService;
import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SimpleQuery;
@ -106,7 +105,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
}
@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
public Boolean closeTaskAndSubTask(Integer staffId, Integer globalAccessLevel, Integer projectId, Long taskId) throws BadRequestException {
public Boolean closeTaskAndSubTask(Integer staffId, Integer globalAccessLevel, Long taskId) throws BadRequestException {
Integer level = getHolderLevel(staffId, globalAccessLevel, taskId);
// if (projectGroupService.getProjectAccessLevel(token, projectId) == 0) {
// throw new BadRequestException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
@ -367,7 +366,7 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
task.setTaskClosedTime(LocalDateTime.now());
}
if (typeChangeValue == 2) {
closed = closeTaskAndSubTask(staffId, globalAccessLevel, task.getTaskProjectId(), task.getTaskId());
closed = closeTaskAndSubTask(staffId, globalAccessLevel, task.getTaskId());
}
if (Objects.equals(rawTask, task)) {
return task;
@ -410,8 +409,8 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
List<TaskTrendDTO> taskList = new ArrayList<>();
ListIterator<TaskTrendDTO> closedIter = closedList.listIterator();
ListIterator<TaskTrendDTO> unclosedIter = unclosedList.listIterator();
TaskTrendDTO closedTaskTrend = null;
TaskTrendDTO unclosedTaskTrend = null;
TaskTrendDTO closedTaskTrend;
TaskTrendDTO unclosedTaskTrend;
// 关闭与未关闭的任务的中间累加对象
closedTaskTrend = closedIter.next();
@ -453,8 +452,8 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
private List<TaskTrendDTO> getResultTaskStatus(Integer projectId, LocalDate startDate) {
List<TaskTrendDTO> resList = baseMapper.selectClosedTaskTrendBeforeDate(projectId, startDate);
List<TaskTrendDTO> taskList = new ArrayList<>();
Boolean existClosed = false;
Boolean existCompleted = false;
boolean existClosed = false;
boolean existCompleted = false;
for (TaskTrendDTO taskTrendDTO : resList) {
if (Objects.equals(Task.STATUS_CLOSED, taskTrendDTO.getTaskStatus())) {
existClosed = true;
@ -486,12 +485,12 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
// FIXME
@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
@Override
public Boolean transferStaffTasks(String token, Integer projectId, Integer transferredStaffId, Map<Long, Integer> transferMap) throws ForbiddenException, BadRequestException {
if (
// projectGroupService.getProjectAccessLevel(token, projectId) == 0 ||
projectGroupService.getProjectAccessLevelIgnoreGlobalLevel(transferredStaffId, projectId) == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
}
public Boolean transferStaffTasks(Integer projectId, Integer transferredStaffId, Map<Long, Integer> transferMap) throws ForbiddenException, BadRequestException {
// if (
//// projectGroupService.getProjectAccessLevel(token, projectId) == 0 ||
// projectGroupService.getProjectAccessLevelIgnoreGlobalLevel(transferredStaffId, projectId) == 0) {
// throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
// }
// if (projectGroupService.compareProjectAccessLevel(projectId, token, transferredStaffId) > 0) {
// throw new ForbiddenException(ForbiddenException.UNABLE_TO_OPERATE);
// }
@ -542,13 +541,13 @@ public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements IT
// FIXME
@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
@Override
public Boolean transferTasksToSingleStaff(String token, Integer projectId, Integer transferredStaffId, Integer targetStaffId) throws ForbiddenException, BadRequestException {
if (
// projectGroupService.getProjectAccessLevel(token, projectId) == 0 ||
projectGroupService.getProjectAccessLevelIgnoreGlobalLevel(transferredStaffId, projectId) == 0
|| projectGroupService.getProjectAccessLevelIgnoreGlobalLevel(targetStaffId, projectId) == 0) {
throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
}
public Boolean transferTasksToSingleStaff(Integer projectId, Integer transferredStaffId, Integer targetStaffId) throws ForbiddenException, BadRequestException {
// if (
//// projectGroupService.getProjectAccessLevel(token, projectId) == 0 ||
// projectGroupService.getProjectAccessLevelIgnoreGlobalLevel(transferredStaffId, projectId) == 0
// || projectGroupService.getProjectAccessLevelIgnoreGlobalLevel(targetStaffId, projectId) == 0) {
// throw new ForbiddenException(IProjectGroupService.UNABLE_TO_ACCESS_PROJECT);
// }
// if (projectGroupService.compareProjectAccessLevel(projectId, token, transferredStaffId) > 0) {
// throw new ForbiddenException(ForbiddenException.UNABLE_TO_OPERATE);
// }