refactor: 检查资源和项目关系支持批量id

This commit is contained in:
CaptainB 2023-12-13 15:02:26 +08:00 committed by 刘瑞斌
parent e014f34d14
commit d5ec1cf140
3 changed files with 16 additions and 5 deletions

View File

@ -2,6 +2,8 @@ package io.metersphere.system.mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ExtCheckOwnerMapper {
boolean checkoutOwner(@Param("table") String resourceType, @Param("projectId") String projectId, @Param("id") String id);
boolean checkoutOwner(@Param("table") String resourceType, @Param("projectId") String projectId, @Param("ids") List<String> ids);
}

View File

@ -2,9 +2,12 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.system.mapper.ExtCheckOwnerMapper">
<select id="checkoutOwner" resultType="boolean">
SELECT 1
SELECT count(id) = ${ids.size()}
FROM ${table}
WHERE id = #{id}
AND project_id = #{projectId}
WHERE project_id = #{projectId}
and id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
</mapper>

View File

@ -22,6 +22,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.List;
@Aspect
@ -68,7 +69,12 @@ public class CheckOwnerAspect {
Expression titleExp = parser.parseExpression(resourceId);
Object v = titleExp.getValue(context, Object.class);
if (v instanceof String id) {
if (!extCheckOwnerMapper.checkoutOwner(resourceType, SessionUtils.getCurrentProjectId(), id)) {
if (!extCheckOwnerMapper.checkoutOwner(resourceType, SessionUtils.getCurrentProjectId(), List.of(id))) {
throw new MSException(Translator.get("check_owner_case"));
}
}
if (v instanceof List ids) {
if (!extCheckOwnerMapper.checkoutOwner(resourceType, SessionUtils.getCurrentProjectId(), ids)) {
throw new MSException(Translator.get("check_owner_case"));
}
}