AuthTools.java 12 KB

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