AuthTools.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package cn.seecoder.web.infrastructure.config;
  2. import cn.seecoder.common.util.SpringUtil;
  3. import cn.seecoder.web.dao.APITest.APITestMapper;
  4. import cn.seecoder.web.dao.bug_list.BugListMapper;
  5. import cn.seecoder.web.dao.func_test.FuncTestCaseMapper;
  6. import cn.seecoder.web.dao.func_test.FuncTestStepMapper;
  7. import cn.seecoder.web.dao.pipeline.PipelineMapper;
  8. import cn.seecoder.web.dao.pipeline.PipelineRecordMapper;
  9. import cn.seecoder.web.dao.tree.TreeNodeMapper;
  10. import cn.seecoder.web.model.po.APITest.TestInfo;
  11. import cn.seecoder.web.model.po.bug_list.BugListPO;
  12. import cn.seecoder.web.model.po.func_test.FuncTestCasePO;
  13. import cn.seecoder.web.model.po.func_test.FuncTestStepPO;
  14. import cn.seecoder.web.model.po.pipeline.PipelinePO;
  15. import cn.seecoder.web.model.po.pipeline.PipelineRecordPO;
  16. import cn.seecoder.web.model.po.tree.TreeNodePO;
  17. import cn.seecoder.web.model.po.user.UserPO;
  18. import cn.seecoder.web.model.vo.bug_list.BugListUpdateBasicVO;
  19. import cn.seecoder.web.model.vo.pipeline.PipelineUpdateConfigVO;
  20. import cn.seecoder.web.model.vo.tree.TreeNodeUpdateBasicVO;
  21. import com.fasterxml.jackson.databind.ObjectMapper;
  22. import com.nju.edu.gitlab.SeecoderGitlabApi;
  23. import com.nju.edu.gitlab.SeecoderGitlabException;
  24. import com.nju.edu.gitlab.vo.ProjectVO;
  25. import lombok.AllArgsConstructor;
  26. import lombok.NoArgsConstructor;
  27. import lombok.RequiredArgsConstructor;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.security.core.context.SecurityContextHolder;
  32. import org.springframework.stereotype.Component;
  33. import javax.servlet.http.HttpServletRequest;
  34. import java.io.IOException;
  35. import java.util.List;
  36. import java.util.stream.Collectors;
  37. @Component
  38. @Slf4j
  39. public class AuthTools {
  40. private final APITestMapper apiTestMapper;
  41. private final FuncTestCaseMapper funcTestCaseMapper;
  42. private final FuncTestStepMapper funcTestStepMapper;
  43. private final TreeNodeMapper treeNodeMapper;
  44. private final BugListMapper bugListMapper;
  45. private final PipelineMapper pipelineMapper;
  46. private final PipelineRecordMapper pipelineRecordMapper;
  47. @Autowired
  48. public AuthTools(APITestMapper apiTestMapper,
  49. FuncTestCaseMapper funcTestCaseMapper,
  50. FuncTestStepMapper funcTestStepMapper,
  51. TreeNodeMapper treeNodeMapper,
  52. BugListMapper bugListMapper,
  53. PipelineMapper pipelineMapper,
  54. PipelineRecordMapper pipelineRecordMapper) {
  55. this.apiTestMapper = apiTestMapper;
  56. this.funcTestCaseMapper = funcTestCaseMapper;
  57. this.funcTestStepMapper = funcTestStepMapper;
  58. this.treeNodeMapper = treeNodeMapper;
  59. this.bugListMapper = bugListMapper;
  60. this.pipelineMapper = pipelineMapper;
  61. this.pipelineRecordMapper = pipelineRecordMapper;
  62. }
  63. private UserPO getCurrentUser() {
  64. return (UserPO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  65. }
  66. public boolean checkProjOwnershipParam(HttpServletRequest request) {
  67. String projectIdStr = request.getParameter("projectId");
  68. log.info("checkProjOwnershipParam verifying projectId: " + projectIdStr);
  69. if (!StringUtils.isNumeric(projectIdStr)) {
  70. return false;
  71. }
  72. Integer projectId = Integer.parseInt(projectIdStr);
  73. return checkProjOwnership(projectId);
  74. }
  75. public boolean checkProjOwnership (Integer projectId) {
  76. UserPO userPO = getCurrentUser();
  77. SeecoderGitlabApi seecoderGitlabApi = SpringUtil.getBean(SeecoderGitlabApi.class);
  78. log.info("checkProjOwnership verifying projectId: " + projectId);
  79. try {
  80. List<ProjectVO> projects = seecoderGitlabApi.getAllProjectsByUserId(userPO.getId());
  81. if (projects.stream().noneMatch(project -> project.getProjectId().equals(projectId))){
  82. log.error("checkProjOwnership projectId none match " + projectId);
  83. return false;
  84. }
  85. } catch (SeecoderGitlabException e) {
  86. log.error("checkProjOwnership " + e.getMessage());
  87. e.printStackTrace();
  88. }
  89. log.info("Verified ProjectId " + projectId);
  90. return true;
  91. }
  92. public boolean checkTestOwnershipParam(HttpServletRequest request) {
  93. String testIdStr = request.getParameter("testId");
  94. log.info("checkTestOwnershipParam verifying testId: " + testIdStr);
  95. if (!StringUtils.isNumeric(testIdStr)) {
  96. return false;
  97. }
  98. Integer testId = Integer.parseInt(testIdStr);
  99. return checkTestOwnership(testId);
  100. }
  101. public boolean checkTestOwnership (Integer testId) {
  102. log.info("checkTestOwnership verifying testId: " + testId);
  103. TestInfo testInfo = apiTestMapper.selectByTestId(testId);
  104. if (testInfo == null) {
  105. return false;
  106. }
  107. Integer projectId = testInfo.getProjectId();
  108. return checkProjOwnership(projectId);
  109. }
  110. public boolean checkTestCaseOwnershipParam(HttpServletRequest request) {
  111. String testCaseIdStr = request.getParameter("testCaseId");
  112. log.info("checkTestCaseOwnershipParam verifying testCaseId: " + testCaseIdStr);
  113. if (!StringUtils.isNumeric(testCaseIdStr)) {
  114. return false;
  115. }
  116. Integer testCaseId = Integer.parseInt(testCaseIdStr);
  117. return checkTestCaseOwnership(testCaseId);
  118. }
  119. public boolean checkTestCaseOwnership (Integer testCaseId) {
  120. log.info("checkTestCaseOwnership verifying testCaseId: " + testCaseId);
  121. FuncTestCasePO funcTestCasePO = funcTestCaseMapper.selectById(testCaseId);
  122. Integer projectId = funcTestCasePO.getProjectId();
  123. return checkProjOwnership(projectId);
  124. }
  125. public boolean checkTestStepOwnershipParam (HttpServletRequest request) {
  126. String testStepIdStr = request.getParameter("testStepId");
  127. log.info("checkTestStepOwnershipParam verifying testStepId: " + testStepIdStr);
  128. if (!StringUtils.isNumeric(testStepIdStr)) {
  129. return false;
  130. }
  131. Integer testStepId = Integer.parseInt(testStepIdStr);
  132. return checkTestStepOwnership(testStepId);
  133. }
  134. public boolean checkTestStepOwnership (Integer testStepId) {
  135. log.info("checkTestStepOwnership verifying testStepId: " + testStepId);
  136. List<FuncTestStepPO> funcTestStepPOS = funcTestStepMapper.selectByTestCaseId(testStepId);
  137. if (funcTestStepPOS == null) {
  138. return false;
  139. }
  140. Integer testCaseId = funcTestStepPOS.get(0).getTestCaseId();
  141. return checkTestCaseOwnership(testCaseId);
  142. }
  143. public boolean checkTreeNodeOwnershipBody (HttpServletRequest request) throws IOException {
  144. ObjectMapper mapper = new ObjectMapper();
  145. String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
  146. TreeNodeUpdateBasicVO treeNodeUpdateBasicVO = mapper.readValue(requestBody, TreeNodeUpdateBasicVO.class);
  147. log.info("checkTreeNodeOwnershipBody verifying treeNodeId: " + treeNodeUpdateBasicVO.getId());
  148. return checkTreeNodeOwnership(treeNodeUpdateBasicVO.getId());
  149. }
  150. public boolean checkTreeNodeOwnershipParam (HttpServletRequest request) {
  151. String treeNodeIdStr = request.getParameter("treeId");
  152. log.info("checkTreeNodeOwnershipParam verifying treeNodeId: " + treeNodeIdStr);
  153. if (!StringUtils.isNumeric(treeNodeIdStr)) {
  154. return false;
  155. }
  156. Integer treeNodeId = Integer.parseInt(treeNodeIdStr);
  157. return checkTreeNodeOwnership(treeNodeId);
  158. }
  159. public boolean checkTreeNodeOwnership (Integer treeNodeId) {
  160. log.info("checkTreeNodeOwnership verifying treeNodeId: " + treeNodeId);
  161. TreeNodePO treeNodeById = treeNodeMapper.getTreeNodeById(treeNodeId);
  162. if (treeNodeById == null) {
  163. return false;
  164. }
  165. Integer projectId = treeNodeById.getProjectId();
  166. return checkProjOwnership(projectId);
  167. }
  168. public boolean checkBugOwnershipBody (HttpServletRequest request) throws IOException {
  169. ObjectMapper mapper = new ObjectMapper();
  170. String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
  171. BugListUpdateBasicVO bugListUpdateBasicVO = mapper.readValue(requestBody, BugListUpdateBasicVO.class);
  172. log.info("checkBugOwnershipBody verifying bugId: " + bugListUpdateBasicVO.getId());
  173. return checkBugOwnership(bugListUpdateBasicVO.getId());
  174. }
  175. public boolean checkBugOwnershipParam (HttpServletRequest request) {
  176. String bugIdStr = request.getParameter("bugId");
  177. log.info("checkBugOwnershipParam verifying bugId: " + bugIdStr);
  178. if (!StringUtils.isNumeric(bugIdStr)) {
  179. return false;
  180. }
  181. Integer bugId = Integer.parseInt(bugIdStr);
  182. return checkBugOwnership(bugId);
  183. }
  184. public boolean checkBugOwnership (Integer bugId) {
  185. BugListPO bugListPO = bugListMapper.selectById(bugId);
  186. log.info("checkBugOwnership verifying bugId: " + bugId);
  187. if (bugListPO == null) {
  188. return false;
  189. }
  190. Integer projectId = bugListPO.getProjectId();
  191. return checkProjOwnership(projectId);
  192. }
  193. public boolean checkProjPipelineBody (HttpServletRequest request) throws IOException {
  194. ObjectMapper mapper = new ObjectMapper();
  195. String requestBody = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
  196. PipelineUpdateConfigVO pipelineUpdateConfigVO = mapper.readValue(requestBody, PipelineUpdateConfigVO.class);
  197. log.info("checkProjPipelineBody verifying projectId: " + pipelineUpdateConfigVO.getProjectId() + " pipelineId: " + pipelineUpdateConfigVO.getPipelineId());
  198. return checkProjPipeline(pipelineUpdateConfigVO.getProjectId(), pipelineUpdateConfigVO.getPipelineId());
  199. }
  200. public boolean checkProjPipelineParam (HttpServletRequest request) {
  201. String projectIdStr = request.getParameter("projectId");
  202. String pipelineIdStr = request.getParameter("pipelineId");
  203. log.info("checkProjPipelineParam verifying projectId: " + projectIdStr + " pipelineId: " + pipelineIdStr);
  204. if (!StringUtils.isNumeric(projectIdStr) || !StringUtils.isNumeric(pipelineIdStr)) {
  205. return false;
  206. }
  207. Integer projectId = Integer.parseInt(projectIdStr);
  208. Integer pipelineId = Integer.parseInt(pipelineIdStr);
  209. return checkProjPipeline(projectId, pipelineId);
  210. }
  211. public boolean checkProjPipeline (Integer projectId, Integer pipelineId) {
  212. log.info("checkProjPipeline verifying projectId: " + projectId + " pipelineId: " + pipelineId);
  213. // check if pipelineId owned by projectId
  214. PipelinePO pipelinePO = pipelineMapper.selectById(pipelineId);
  215. Integer projectId1 = pipelinePO.getProjectId();
  216. if (!projectId1.equals(projectId)) {
  217. return false;
  218. }
  219. // check if own project
  220. return checkProjOwnership(projectId);
  221. }
  222. public boolean checkPipelineOwnership (Integer pipelineId) {
  223. log.info("checkPipelineOwnership verifying pipelineId: " + pipelineId);
  224. PipelinePO pipelinePO = pipelineMapper.selectById(pipelineId);
  225. if (pipelinePO == null) {
  226. return false;
  227. }
  228. Integer projectId = pipelinePO.getProjectId();
  229. return checkProjOwnership(projectId);
  230. }
  231. public boolean checkPipelineRecordOwnershipParam (HttpServletRequest request) {
  232. String pipelineRecordIdStr = request.getParameter("recordId");
  233. log.info("checkPipelineRecordOwnershipParam verifying pipelineRecordId: " + pipelineRecordIdStr);
  234. if (!StringUtils.isNumeric(pipelineRecordIdStr)) {
  235. return false;
  236. }
  237. Integer pipelineRecordId = Integer.parseInt(pipelineRecordIdStr);
  238. return checkPipelineRecordOwnership(pipelineRecordId);
  239. }
  240. public boolean checkPipelineRecordOwnership (Integer pipelineRecordId) {
  241. log.info("checkPipelineRecordOwnershipParam verifying pipelineRecordId: " + pipelineRecordId);
  242. PipelineRecordPO pipelineRecordPO = pipelineRecordMapper.selectById(pipelineRecordId);
  243. if (pipelineRecordPO == null) {
  244. return false;
  245. }
  246. Integer pipelineId = pipelineRecordPO.getPipelineId();
  247. return checkPipelineOwnership(pipelineId);
  248. }
  249. }