|
@@ -0,0 +1,241 @@
|
|
|
|
|
+package cn.seecoder.web.infrastructure.config;
|
|
|
|
|
+
|
|
|
|
|
+import cn.seecoder.common.util.SpringUtil;
|
|
|
|
|
+import cn.seecoder.web.dao.APITest.APITestMapper;
|
|
|
|
|
+import cn.seecoder.web.dao.bug_list.BugListMapper;
|
|
|
|
|
+import cn.seecoder.web.dao.func_test.FuncTestCaseMapper;
|
|
|
|
|
+import cn.seecoder.web.dao.func_test.FuncTestStepMapper;
|
|
|
|
|
+import cn.seecoder.web.dao.pipeline.PipelineMapper;
|
|
|
|
|
+import cn.seecoder.web.dao.pipeline.PipelineRecordMapper;
|
|
|
|
|
+import cn.seecoder.web.dao.tree.TreeNodeMapper;
|
|
|
|
|
+import cn.seecoder.web.model.po.APITest.TestInfo;
|
|
|
|
|
+import cn.seecoder.web.model.po.bug_list.BugListPO;
|
|
|
|
|
+import cn.seecoder.web.model.po.func_test.FuncTestCasePO;
|
|
|
|
|
+import cn.seecoder.web.model.po.func_test.FuncTestStepPO;
|
|
|
|
|
+import cn.seecoder.web.model.po.pipeline.PipelinePO;
|
|
|
|
|
+import cn.seecoder.web.model.po.pipeline.PipelineRecordPO;
|
|
|
|
|
+import cn.seecoder.web.model.po.tree.TreeNodePO;
|
|
|
|
|
+import cn.seecoder.web.model.po.user.UserPO;
|
|
|
|
|
+import cn.seecoder.web.model.vo.pipeline.PipelineUpdateConfigVO;
|
|
|
|
|
+import cn.seecoder.web.model.vo.tree.TreeNodeUpdateBasicVO;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import com.nju.edu.gitlab.SeecoderGitlabApi;
|
|
|
|
|
+import com.nju.edu.gitlab.SeecoderGitlabException;
|
|
|
|
|
+import com.nju.edu.gitlab.vo.ProjectVO;
|
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@Component
|
|
|
|
|
+public class AuthTools {
|
|
|
|
|
+ private final APITestMapper apiTestMapper;
|
|
|
|
|
+ private final FuncTestCaseMapper funcTestCaseMapper;
|
|
|
|
|
+ private final FuncTestStepMapper funcTestStepMapper;
|
|
|
|
|
+ private final TreeNodeMapper treeNodeMapper;
|
|
|
|
|
+ private final BugListMapper bugListMapper;
|
|
|
|
|
+ private final PipelineMapper pipelineMapper;
|
|
|
|
|
+ private final PipelineRecordMapper pipelineRecordMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ public AuthTools(APITestMapper apiTestMapper,
|
|
|
|
|
+ FuncTestCaseMapper funcTestCaseMapper,
|
|
|
|
|
+ FuncTestStepMapper funcTestStepMapper,
|
|
|
|
|
+ TreeNodeMapper treeNodeMapper,
|
|
|
|
|
+ BugListMapper bugListMapper,
|
|
|
|
|
+ PipelineMapper pipelineMapper,
|
|
|
|
|
+ PipelineRecordMapper pipelineRecordMapper) {
|
|
|
|
|
+ this.apiTestMapper = apiTestMapper;
|
|
|
|
|
+ this.funcTestCaseMapper = funcTestCaseMapper;
|
|
|
|
|
+ this.funcTestStepMapper = funcTestStepMapper;
|
|
|
|
|
+ this.treeNodeMapper = treeNodeMapper;
|
|
|
|
|
+ this.bugListMapper = bugListMapper;
|
|
|
|
|
+ this.pipelineMapper = pipelineMapper;
|
|
|
|
|
+ this.pipelineRecordMapper = pipelineRecordMapper;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private UserPO getCurrentUser() {
|
|
|
|
|
+ return (UserPO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkProjOwnershipParam(HttpServletRequest request) {
|
|
|
|
|
+ String projectIdStr = request.getParameter("projectId");
|
|
|
|
|
+ if (!StringUtils.isNumeric(projectIdStr)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer projectId = Integer.parseInt(projectIdStr);
|
|
|
|
|
+ return checkProjOwnership(projectId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkProjOwnership (Integer projectId) {
|
|
|
|
|
+ UserPO userPO = getCurrentUser();
|
|
|
|
|
+ SeecoderGitlabApi seecoderGitlabApi = SpringUtil.getBean(SeecoderGitlabApi.class);
|
|
|
|
|
+ try {
|
|
|
|
|
+ List<ProjectVO> projects = seecoderGitlabApi.getAllProjectsByUserId(userPO.getId());
|
|
|
|
|
+ if (projects.stream().noneMatch(project -> project.getProjectId().equals(projectId))){
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (SeecoderGitlabException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTestOwnershipParam(HttpServletRequest request) {
|
|
|
|
|
+ String testIdStr = request.getParameter("testId");
|
|
|
|
|
+ if (!StringUtils.isNumeric(testIdStr)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer testId = Integer.parseInt(testIdStr);
|
|
|
|
|
+ return checkTestOwnership(testId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTestOwnership (Integer testId) {
|
|
|
|
|
+ TestInfo testInfo = apiTestMapper.selectByTestId(testId);
|
|
|
|
|
+ if (testInfo == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer projectId = testInfo.getProjectId();
|
|
|
|
|
+ return checkProjOwnership(projectId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTestCaseOwnershipParam(HttpServletRequest request) {
|
|
|
|
|
+ String testCaseIdStr = request.getParameter("testCaseId");
|
|
|
|
|
+ if (!StringUtils.isNumeric(testCaseIdStr)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer testCaseId = Integer.parseInt(testCaseIdStr);
|
|
|
|
|
+ return checkTestCaseOwnership(testCaseId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTestCaseOwnership (Integer testCaseId) {
|
|
|
|
|
+ FuncTestCasePO funcTestCasePO = funcTestCaseMapper.selectById(testCaseId);
|
|
|
|
|
+ Integer projectId = funcTestCasePO.getProjectId();
|
|
|
|
|
+ return checkProjOwnership(projectId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTestStepOwnershipParam (HttpServletRequest request) {
|
|
|
|
|
+ String testStepIdStr = request.getParameter("testStepId");
|
|
|
|
|
+ if (!StringUtils.isNumeric(testStepIdStr)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer testStepId = Integer.parseInt(testStepIdStr);
|
|
|
|
|
+ return checkTestStepOwnership(testStepId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTestStepOwnership (Integer testStepId) {
|
|
|
|
|
+ List<FuncTestStepPO> funcTestStepPOS = funcTestStepMapper.selectByTestCaseId(testStepId);
|
|
|
|
|
+ if (funcTestStepPOS == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer testCaseId = funcTestStepPOS.get(0).getTestCaseId();
|
|
|
|
|
+ return checkTestCaseOwnership(testCaseId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTreeNodeOwnershipBody (HttpServletRequest request) throws IOException {
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+ String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
|
|
|
|
|
+ TreeNodeUpdateBasicVO treeNodeUpdateBasicVO = mapper.readValue(requestBody, TreeNodeUpdateBasicVO.class);
|
|
|
|
|
+ return checkTreeNodeOwnership(treeNodeUpdateBasicVO.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTreeNodeOwnershipParam (HttpServletRequest request) {
|
|
|
|
|
+ String treeNodeIdStr = request.getParameter("treeId");
|
|
|
|
|
+ if (!StringUtils.isNumeric(treeNodeIdStr)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer treeNodeId = Integer.parseInt(treeNodeIdStr);
|
|
|
|
|
+ return checkTreeNodeOwnership(treeNodeId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkTreeNodeOwnership (Integer treeNodeId) {
|
|
|
|
|
+ TreeNodePO treeNodeById = treeNodeMapper.getTreeNodeById(treeNodeId);
|
|
|
|
|
+ if (treeNodeById == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer projectId = treeNodeById.getProjectId();
|
|
|
|
|
+ return checkProjOwnership(projectId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkBugOwnershipParam (HttpServletRequest request) {
|
|
|
|
|
+ String bugIdStr = request.getParameter("bugId");
|
|
|
|
|
+ if (!StringUtils.isNumeric(bugIdStr)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer bugId = Integer.parseInt(bugIdStr);
|
|
|
|
|
+ return checkBugOwnership(bugId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkBugOwnership (Integer bugId) {
|
|
|
|
|
+ BugListPO bugListPO = bugListMapper.selectById(bugId);
|
|
|
|
|
+ if (bugListPO == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer projectId = bugListPO.getProjectId();
|
|
|
|
|
+ return checkProjOwnership(projectId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkProjPipelineBody (HttpServletRequest request) throws IOException {
|
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
|
+ String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
|
|
|
|
|
+ PipelineUpdateConfigVO pipelineUpdateConfigVO = mapper.readValue(requestBody, PipelineUpdateConfigVO.class);
|
|
|
|
|
+ return checkProjPipeline(pipelineUpdateConfigVO.getProjectId(), pipelineUpdateConfigVO.getPipelineId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkProjPipelineParam (HttpServletRequest request) {
|
|
|
|
|
+ String projectIdStr = request.getParameter("projectId");
|
|
|
|
|
+ String pipelineIdStr = request.getParameter("pipelineId");
|
|
|
|
|
+ if (!StringUtils.isNumeric(projectIdStr) || !StringUtils.isNumeric(pipelineIdStr)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer projectId = Integer.parseInt(projectIdStr);
|
|
|
|
|
+ Integer pipelineId = Integer.parseInt(pipelineIdStr);
|
|
|
|
|
+ return checkProjPipeline(projectId, pipelineId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkProjPipeline (Integer projectId, Integer pipelineId) {
|
|
|
|
|
+ // check if pipelineId owned by projectId
|
|
|
|
|
+ PipelinePO pipelinePO = pipelineMapper.selectById(pipelineId);
|
|
|
|
|
+ Integer projectId1 = pipelinePO.getProjectId();
|
|
|
|
|
+ if (!projectId1.equals(projectId)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ // check if own project
|
|
|
|
|
+ return checkProjOwnership(projectId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkPipelineOwnership (Integer pipelineId) {
|
|
|
|
|
+ PipelinePO pipelinePO = pipelineMapper.selectById(pipelineId);
|
|
|
|
|
+ if (pipelinePO == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer projectId = pipelinePO.getProjectId();
|
|
|
|
|
+ return checkProjOwnership(projectId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkPipelineRecordOwnershipParam (HttpServletRequest request) {
|
|
|
|
|
+ String pipelineRecordIdStr = request.getParameter("recordId");
|
|
|
|
|
+ if (!StringUtils.isNumeric(pipelineRecordIdStr)) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer pipelineRecordId = Integer.parseInt(pipelineRecordIdStr);
|
|
|
|
|
+ return checkPipelineRecordOwnership(pipelineRecordId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public boolean checkPipelineRecordOwnership (Integer pipelineRecordId) {
|
|
|
|
|
+ PipelineRecordPO pipelineRecordPO = pipelineRecordMapper.selectById(pipelineRecordId);
|
|
|
|
|
+ if (pipelineRecordPO == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Integer pipelineId = pipelineRecordPO.getPipelineId();
|
|
|
|
|
+ return checkPipelineOwnership(pipelineId);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|