refactor: 重构checkowner的逻辑

This commit is contained in:
Captain.B 2020-12-10 18:47:29 +08:00
parent 8c26c9b9f3
commit d20719c9ee
11 changed files with 150 additions and 80 deletions

View File

@ -6,6 +6,7 @@ import io.metersphere.base.domain.ApiTest;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Set;
public interface ExtApiTestMapper {
List<APITestResult> list(@Param("request") QueryAPITestRequest request);
@ -13,4 +14,7 @@ public interface ExtApiTestMapper {
List<ApiTest> getApiTestByProjectId(String projectId);
List<ApiTest> listByIds(@Param("ids") List<String> ids);
int checkApiTestOwner(@Param("testId") String testId, @Param("workspaceIds") Set<String> workspaceIds);
}

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="io.metersphere.base.mapper.ext.ExtApiTestMapper">
<resultMap id="BaseResultMap" type="io.metersphere.api.dto.APITestResult"
extends="io.metersphere.base.mapper.ApiTestMapper.BaseResultMap">
<result column="project_name" property="projectName"/>
@ -11,25 +10,25 @@
<sql id="condition">
<choose>
<when test='${object}.operator == "like"'>
like CONCAT('%', #{${object}.value},'%')
LIKE CONCAT('%', #{${object}.value},'%')
</when>
<when test='${object}.operator == "not like"'>
not like CONCAT('%', #{${object}.value},'%')
NOT LIKE CONCAT('%', #{${object}.value},'%')
</when>
<when test='${object}.operator == "in"'>
in
IN
<foreach collection="${object}.value" item="v" separator="," open="(" close=")">
#{v}
</foreach>
</when>
<when test='${object}.operator == "not in"'>
not in
NOT IN
<foreach collection="${object}.value" item="v" separator="," open="(" close=")">
#{v}
</foreach>
</when>
<when test='${object}.operator == "between"'>
between #{${object}.value[0]} and #{${object}.value[1]}
BETWEEN #{${object}.value[0]} AND #{${object}.value[1]}
</when>
<when test='${object}.operator == "gt"'>
&gt; #{${object}.value}
@ -54,37 +53,37 @@
<sql id="combine">
<if test='${condition}.name != null and (${name} == null or ${name} == "")'>
and api_test.name
AND api_test.name
<include refid="condition">
<property name="object" value="${condition}.name"/>
</include>
</if>
<if test="${condition}.updateTime != null">
and api_test.update_time
AND api_test.update_time
<include refid="condition">
<property name="object" value="${condition}.updateTime"/>
</include>
</if>
<if test="${condition}.projectName != null">
and project.name
AND project.name
<include refid="condition">
<property name="object" value="${condition}.projectName"/>
</include>
</if>
<if test="${condition}.createTime != null">
and api_test.create_time
AND api_test.create_time
<include refid="condition">
<property name="object" value="${condition}.createTime"/>
</include>
</if>
<if test="${condition}.status != null">
and api_test.status
AND api_test.status
<include refid="condition">
<property name="object" value="${condition}.status"/>
</include>
</if>
<if test="${condition}.creator != null">
and api_test.user_id
AND api_test.user_id
<include refid="condition">
<property name="object" value="${condition}.creator"/>
</include>
@ -92,11 +91,11 @@
</sql>
<select id="list" resultMap="BaseResultMap">
select api_test.id, api_test.project_id, api_test.name, api_test.description,
api_test.status, api_test.user_id, api_test.create_time, api_test.update_time, project.name as project_name, user.name as user_name
from api_test
left join project on api_test.project_id = project.id
left join user on api_test.user_id = user.id
SELECT api_test.id, api_test.project_id, api_test.name, api_test.description,
api_test.status, api_test.user_id, api_test.create_time, api_test.update_time, project.name AS project_name, user.name AS user_name
FROM api_test
LEFT JOIN project ON api_test.project_id = project.id
LEFT JOIN user ON api_test.user_id = user.id
<where>
<if test="request.combine != null">
<include refid="combine">
@ -109,7 +108,7 @@
and api_test.id != #{request.excludeId}
</if>
<if test="request.name != null">
and api_test.name like CONCAT('%', #{request.name},'%')
AND api_test.name LIKE CONCAT('%', #{request.name},'%')
</if>
<if test="request.workspaceId != null">
AND project.workspace_id = #{request.workspaceId}
@ -125,36 +124,34 @@
</if>
<if test="request.filters != null and request.filters.size() > 0">
<foreach collection="request.filters.entrySet()" index="key" item="values">
<if test="values != null and values.size() > 0">
and api_test.status in
AND api_test.status IN
<foreach collection="values" item="value" separator="," open="(" close=")">
#{value}
</foreach>
</if>
</foreach>
</if>
</where>
<if test="request.orders != null and request.orders.size() > 0">
order by
ORDER BY
<foreach collection="request.orders" separator="," item="order">
api_test.${order.name} ${order.type}
</foreach>
</if>
</select>
<select id="getApiTestByProjectId" resultType="io.metersphere.base.domain.ApiTest">
select id,name,status
from api_test
where project_id = #{projectId}
SELECT id,name,status
FROM api_test
WHERE project_id = #{projectId}
</select>
<select id="listByIds" resultType="io.metersphere.base.domain.ApiTest">
select *
from api_test
SELECT *
FROM api_test
<where>
<if test="ids != null and ids.size() > 0">
id in
id IN
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
@ -162,4 +159,20 @@
</where>
</select>
<select id="checkApiTestOwner" resultType="int">
SELECT COUNT(1)
FROM api_test
LEFT JOIN project ON api_test.project_id = project.id
<where>
<if test="testId != null">
and api_test.id = #{testId}
</if>
<if test="workspaceIds != null and workspaceIds.size() > 0">
AND workspace_id IN
<foreach collection="workspaceIds" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
</where>
</select>
</mapper>

View File

@ -1,14 +1,17 @@
package io.metersphere.base.mapper.ext;
import io.metersphere.base.domain.LoadTest;
import io.metersphere.track.request.testplan.QueryTestPlanRequest;
import io.metersphere.dto.LoadTestDTO;
import io.metersphere.track.request.testplan.QueryTestPlanRequest;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Set;
public interface ExtLoadTestMapper {
List<LoadTestDTO> list(@Param("request") QueryTestPlanRequest params);
List<LoadTest> getLoadTestByProjectId(String projectId);
int checkLoadTestOwner(@Param("testId") String testId, @Param("workspaceIds") Set<String> workspaceIds);
}

View File

@ -98,9 +98,25 @@
</if>
</select>
<select id="getLoadTestByProjectId" resultType="io.metersphere.base.domain.LoadTest">
select id,name
from load_test
where project_id = #{projectId}
SELECT id,name
FROM load_test
WHERE project_id = #{projectId}
</select>
<select id="checkLoadTestOwner" resultType="int">
SELECT COUNT(1)
FROM load_test
LEFT JOIN project ON api_test.project_id = project.id
<where>
<if test="testId != null">
and load_test.id = #{testId}
</if>
<if test="workspaceIds != null and workspaceIds.size() > 0">
AND workspace_id IN
<foreach collection="workspaceIds" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
</where>
</select>
</mapper>

View File

@ -1,12 +1,13 @@
package io.metersphere.base.mapper.ext;
import io.metersphere.base.domain.TestCase;
import io.metersphere.track.request.testcase.QueryTestCaseRequest;
import io.metersphere.track.dto.TestCaseDTO;
import io.metersphere.track.request.testcase.QueryTestCaseRequest;
import io.metersphere.track.request.testcase.TestCaseBatchRequest;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Set;
public interface ExtTestCaseMapper {
@ -22,6 +23,7 @@ public interface ExtTestCaseMapper {
/**
* 获取不在测试计划中的用例
*
* @param request
* @return
*/
@ -29,6 +31,7 @@ public interface ExtTestCaseMapper {
/**
* 获取不在评审范围中的用例
*
* @param request
* @return
*/
@ -38,9 +41,9 @@ public interface ExtTestCaseMapper {
* 检查某工作空间下是否有某用例
*
* @param caseId
* @param workspaceId
* @param workspaceIds
* @return TestCase ID
*/
List<String> checkIsHave(@Param("caseId") String caseId, @Param("workspaceId") String workspaceId);
int checkIsHave(@Param("caseId") String caseId, @Param("workspaceIds") Set<String> workspaceIds);
}

View File

@ -357,13 +357,19 @@
</select>
<select id="getMaxNumByProjectId" resultType="io.metersphere.base.domain.TestCase">
select * from test_case where test_case.project_id = #{projectId} order by num desc limit 1;
SELECT * FROM test_case WHERE test_case.project_id = #{projectId} ORDER BY num DESC LIMIT 1;
</select>
<select id="checkIsHave" resultType="java.lang.String">
select distinct test_case.id
from test_case, project
where test_case.project_id = project.id
and project.workspace_id = #{workspaceId}
and test_case.id = #{caseId}
<select id="checkIsHave" resultType="int">
SELECT COUNT(1)
FROM test_case, project
WHERE test_case.project_id = project.id
<if test="workspaceIds != null and workspaceIds.size() > 0">
AND project.workspace_id IN
<foreach collection="workspaceIds" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
AND test_case.id = #{caseId}
</select>
</mapper>

View File

@ -7,6 +7,7 @@ import io.metersphere.track.request.testreview.QueryTestReviewRequest;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Set;
public interface ExtTestCaseReviewMapper {
@ -20,8 +21,8 @@ public interface ExtTestCaseReviewMapper {
* 检查某工作空间下是否有某测试评审
*
* @param reviewId
* @param workspaceId
* @param workspaceIds
* @return Review ID
*/
List<String> checkIsHave(@Param("reviewId") String reviewId, @Param("workspaceId") String workspaceId);
int checkIsHave(@Param("reviewId") String reviewId, @Param("workspaceIds") Set<String> workspaceIds);
}

View File

@ -74,11 +74,16 @@
order by test_case_review.update_time desc
</select>
<select id="checkIsHave" resultType="java.lang.String">
select distinct review_id
from project, test_case_review_project
where project.id = test_case_review_project.project_id
and project.workspace_id = #{workspaceId}
and test_case_review_project.review_id = #{reviewId}
<select id="checkIsHave" resultType="int">
SELECT COUNT(1)
FROM project, test_case_review_project
WHERE project.id = test_case_review_project.project_id
<if test="workspaceIds != null and workspaceIds.size() > 0">
AND project.workspace_id IN
<foreach collection="workspaceIds" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
AND test_case_review_project.review_id = #{reviewId}
</select>
</mapper>

View File

@ -6,6 +6,7 @@ import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Set;
public interface ExtTestPlanMapper {
List<TestPlanDTOWithMetric> list(@Param("request") QueryTestPlanRequest params);
@ -20,4 +21,5 @@ public interface ExtTestPlanMapper {
List<TestPlanDTO> selectReference(@Param("request") QueryTestPlanRequest params);
int checkIsHave(@Param("planId") String planId, @Param("workspaceIds") Set<String> workspaceIds);
}

View File

@ -215,4 +215,16 @@
</if>
</where>
</select>
<select id="checkIsHave" resultType="int">
SELECT COUNT(1)
FROM test_plan_project, project
WHERE project_id = project.id AND test_plan_id = #{planId}
<if test="workspaceIds != null and workspaceIds.size() > 0">
AND project.workspace_id IN
<foreach collection="workspaceIds" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</if>
</select>
</mapper>

View File

@ -1,24 +1,18 @@
package io.metersphere.service;
import io.metersphere.api.dto.APITestResult;
import io.metersphere.api.dto.QueryAPITestRequest;
import io.metersphere.base.domain.Project;
import io.metersphere.base.domain.UserRole;
import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.base.mapper.ext.*;
import io.metersphere.commons.constants.RoleConstants;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.dto.LoadTestDTO;
import io.metersphere.i18n.Translator;
import io.metersphere.track.dto.TestPlanDTOWithMetric;
import io.metersphere.track.request.testplan.QueryTestPlanRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@ -44,6 +38,9 @@ public class CheckOwnerService {
if (project == null) {
return;
}
if (CollectionUtils.isEmpty(workspaceIds)) {
return;
}
if (!workspaceIds.contains(project.getWorkspaceId())) {
throw new UnauthorizedException(Translator.get("check_owner_project"));
}
@ -62,13 +59,14 @@ public class CheckOwnerService {
if (StringUtils.equals("other", testId)) {
return;
}
String workspaceId = SessionUtils.getCurrentWorkspaceId();
QueryAPITestRequest request = new QueryAPITestRequest();
request.setWorkspaceId(workspaceId);
request.setId(testId);
List<APITestResult> apiTestResults = extApiTestMapper.list(request);
Set<String> workspaceIds = getUserRelatedWorkspaceIds();
if (CollectionUtils.isEmpty(workspaceIds)) {
return;
}
if (CollectionUtils.size(apiTestResults) != 1) {
int result = extApiTestMapper.checkApiTestOwner(testId, workspaceIds);
if (result == 0) {
throw new UnauthorizedException(Translator.get("check_owner_test"));
}
}
@ -78,40 +76,47 @@ public class CheckOwnerService {
if (StringUtils.equals("other", testId)) {
return;
}
String workspaceId = SessionUtils.getCurrentWorkspaceId();
QueryTestPlanRequest request = new QueryTestPlanRequest();
request.setWorkspaceId(workspaceId);
request.setId(testId);
List<LoadTestDTO> loadTestDTOS = extLoadTestMapper.list(request);
Set<String> workspaceIds = getUserRelatedWorkspaceIds();
if (CollectionUtils.isEmpty(workspaceIds)) {
return;
}
int result = extLoadTestMapper.checkLoadTestOwner(testId, workspaceIds);
if (CollectionUtils.size(loadTestDTOS) != 1) {
if (result == 0) {
throw new UnauthorizedException(Translator.get("check_owner_test"));
}
}
public void checkTestCaseOwner(String caseId) {
String workspaceId = SessionUtils.getCurrentWorkspaceId();
List<String> list = extTestCaseMapper.checkIsHave(caseId, workspaceId);
if (CollectionUtils.size(list) != 1) {
Set<String> workspaceIds = getUserRelatedWorkspaceIds();
if (CollectionUtils.isEmpty(workspaceIds)) {
return;
}
int result = extTestCaseMapper.checkIsHave(caseId, workspaceIds);
if (result == 0) {
throw new UnauthorizedException(Translator.get("check_owner_case"));
}
}
public void checkTestPlanOwner(String planId) {
String workspaceId = SessionUtils.getCurrentWorkspaceId();
io.metersphere.track.request.testcase.QueryTestPlanRequest request = new io.metersphere.track.request.testcase.QueryTestPlanRequest();
request.setWorkspaceId(workspaceId);
request.setId(planId);
List<TestPlanDTOWithMetric> list = extTestPlanMapper.list(request);
if (CollectionUtils.size(list) != 1) {
Set<String> workspaceIds = getUserRelatedWorkspaceIds();
if (CollectionUtils.isEmpty(workspaceIds)) {
return;
}
int result = extTestPlanMapper.checkIsHave(planId, workspaceIds);
if (result == 0) {
throw new UnauthorizedException(Translator.get("check_owner_plan"));
}
}
public void checkTestReviewOwner(String reviewId) {
String workspaceId = SessionUtils.getCurrentWorkspaceId();
List<String> list = extTestCaseReviewMapper.checkIsHave(reviewId, workspaceId);
if (CollectionUtils.size(list) != 1) {
Set<String> workspaceIds = getUserRelatedWorkspaceIds();
if (CollectionUtils.isEmpty(workspaceIds)) {
return;
}
int result = extTestCaseReviewMapper.checkIsHave(reviewId, workspaceIds);
if (result == 0) {
throw new UnauthorizedException(Translator.get("check_owner_review"));
}
}