| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- 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 com.nju.edu.gitlab.SeecoderGitlabApi;
- import com.nju.edu.gitlab.SeecoderGitlabException;
- import com.nju.edu.gitlab.vo.ProjectVO;
- import com.nju.edu.gitlab.vo.VisibilityVO;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- 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.Objects;
- @RequiredArgsConstructor
- @Component
- @Slf4j
- 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;
- private final SeecoderGitlabApi gitlabApi;
- private UserPO getCurrentUser() {
- return (UserPO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
- }
- public boolean checkProjOwnershipParam(HttpServletRequest request) {
- String projectIdStr = request.getParameter("projectId");
- log.info("checkProjOwnershipParam verifying projectId: " + projectIdStr);
- 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);
- log.info("checkProjOwnership verifying projectId: " + projectId);
- try {
- List<ProjectVO> projects = seecoderGitlabApi.getAllProjectsByUserId(userPO.getId());
- if (projects.stream().noneMatch(project -> project.getProjectId().equals(projectId))){
- log.error("checkProjOwnership projectId none match " + projectId);
- return false;
- }
- } catch (SeecoderGitlabException e) {
- log.error("checkProjOwnership failed, projectId={}, userId={}", projectId, userPO.getId(), e);
- }
- log.info("Verified ProjectId " + projectId);
- return true;
- }
- public boolean getProjectValidator(Integer projectId) {
- try {
- VisibilityVO visibility = gitlabApi.getProjectVisibility(projectId);
- if (Objects.equals(visibility.getVisibility().toLowerCase(), "public")) {
- return true;
- } else {
- return checkProjOwnership(projectId);
- }
- } catch (SeecoderGitlabException e) {
- log.error("AuthTool getting visibility of " + projectId + " failed.");
- return false;
- }
- }
- public boolean checkTestOwnershipParam(HttpServletRequest request) {
- String testIdStr = request.getParameter("testId");
- log.info("checkTestOwnershipParam verifying testId: " + testIdStr);
- if (!StringUtils.isNumeric(testIdStr)) {
- return false;
- }
- Integer testId = Integer.parseInt(testIdStr);
- return checkTestOwnership(testId);
- }
- public boolean checkTestOwnership (Integer testId) {
- log.info("checkTestOwnership verifying testId: " + 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");
- log.info("checkTestCaseOwnershipParam verifying testCaseId: " + testCaseIdStr);
- if (!StringUtils.isNumeric(testCaseIdStr)) {
- return false;
- }
- Integer testCaseId = Integer.parseInt(testCaseIdStr);
- return checkTestCaseOwnership(testCaseId);
- }
- public boolean checkTestCaseOwnership (Integer testCaseId) {
- log.info("checkTestCaseOwnership verifying testCaseId: " + testCaseId);
- FuncTestCasePO funcTestCasePO = funcTestCaseMapper.selectById(testCaseId);
- Integer projectId = funcTestCasePO.getProjectId();
- return checkProjOwnership(projectId);
- }
- public boolean checkTestStepOwnershipParam (HttpServletRequest request) {
- String testStepIdStr = request.getParameter("testStepId");
- log.info("checkTestStepOwnershipParam verifying testStepId: " + testStepIdStr);
- if (!StringUtils.isNumeric(testStepIdStr)) {
- return false;
- }
- Integer testStepId = Integer.parseInt(testStepIdStr);
- return checkTestStepOwnership(testStepId);
- }
- public boolean checkTestStepOwnership (Integer testStepId) {
- log.info("checkTestStepOwnership verifying testStepId: " + testStepId);
- FuncTestStepPO funcTestStepPO = funcTestStepMapper.selectById(testStepId);
- if (funcTestStepPO == null) {
- return false;
- }
- Integer testCaseId = funcTestStepPO.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);
- // log.info("checkTreeNodeOwnershipBody verifying treeNodeId: " + treeNodeUpdateBasicVO.getId());
- // return checkTreeNodeOwnership(treeNodeUpdateBasicVO.getId());
- return true;
- }
- public boolean checkTreeNodeOwnershipParam (HttpServletRequest request) {
- String treeNodeIdStr = request.getParameter("treeId");
- log.info("checkTreeNodeOwnershipParam verifying treeNodeId: " + treeNodeIdStr);
- if (!StringUtils.isNumeric(treeNodeIdStr)) {
- return false;
- }
- Integer treeNodeId = Integer.parseInt(treeNodeIdStr);
- return checkTreeNodeOwnership(treeNodeId);
- }
- public boolean checkTreeNodeOwnership (Integer treeNodeId) {
- log.info("checkTreeNodeOwnership verifying treeNodeId: " + treeNodeId);
- TreeNodePO treeNodeById = treeNodeMapper.getTreeNodeById(treeNodeId);
- if (treeNodeById == null) {
- return false;
- }
- Integer projectId = treeNodeById.getProjectId();
- return checkProjOwnership(projectId);
- }
- public boolean checkBugOwnershipBody (HttpServletRequest request) throws IOException {
- // ObjectMapper mapper = new ObjectMapper();
- // String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
- // BugListUpdateBasicVO bugListUpdateBasicVO = mapper.readValue(requestBody, BugListUpdateBasicVO.class);
- // log.info("checkBugOwnershipBody verifying bugId: " + bugListUpdateBasicVO.getId());
- // return checkBugOwnership(bugListUpdateBasicVO.getId());
- return true;
- }
- public boolean checkBugOwnershipParam (HttpServletRequest request) {
- String bugIdStr = request.getParameter("bugId");
- log.info("checkBugOwnershipParam verifying bugId: " + bugIdStr);
- if (!StringUtils.isNumeric(bugIdStr)) {
- return false;
- }
- Integer bugId = Integer.parseInt(bugIdStr);
- return checkBugOwnership(bugId);
- }
- public boolean checkBugOwnership (Integer bugId) {
- BugListPO bugListPO = bugListMapper.selectById(bugId);
- log.info("checkBugOwnership verifying bugId: " + 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);
- // log.info("checkProjPipelineBody verifying projectId: " + pipelineUpdateConfigVO.getProjectId() + " pipelineId: " + pipelineUpdateConfigVO.getPipelineId());
- // return checkProjPipeline(pipelineUpdateConfigVO.getProjectId(), pipelineUpdateConfigVO.getPipelineId());
- return true;
- }
- public boolean checkProjPipelineParam (HttpServletRequest request) {
- String projectIdStr = request.getParameter("projectId");
- String pipelineIdStr = request.getParameter("pipelineId");
- log.info("checkProjPipelineParam verifying projectId: " + projectIdStr + " pipelineId: " + pipelineIdStr);
- 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) {
- log.info("checkProjPipeline verifying projectId: " + projectId + " pipelineId: " + 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) {
- log.info("checkPipelineOwnership verifying pipelineId: " + 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");
- log.info("checkPipelineRecordOwnershipParam verifying pipelineRecordId: " + pipelineRecordIdStr);
- if (!StringUtils.isNumeric(pipelineRecordIdStr)) {
- return false;
- }
- Integer pipelineRecordId = Integer.parseInt(pipelineRecordIdStr);
- return checkPipelineRecordOwnership(pipelineRecordId);
- }
- public boolean checkPipelineRecordOwnership (Integer pipelineRecordId) {
- log.info("checkPipelineRecordOwnershipParam verifying pipelineRecordId: " + pipelineRecordId);
- PipelineRecordPO pipelineRecordPO = pipelineRecordMapper.selectById(pipelineRecordId);
- if (pipelineRecordPO == null) {
- return false;
- }
- Integer pipelineId = pipelineRecordPO.getPipelineId();
- return checkPipelineOwnership(pipelineId);
- }
- }
|