ci: 增加check owner注解

This commit is contained in:
CaptainB 2023-12-13 14:43:02 +08:00
parent d9f2193977
commit b334bdd72d
3 changed files with 46 additions and 39 deletions

View File

@ -2,6 +2,8 @@ package io.metersphere.base.mapper.ext;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ExtCheckOwnerMapper { 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 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"> <!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.ExtCheckOwnerMapper"> <mapper namespace="io.metersphere.base.mapper.ext.ExtCheckOwnerMapper">
<select id="checkoutOwner" resultType="boolean"> <select id="checkoutOwner" resultType="boolean">
SELECT 1 SELECT count(id) = #{ids.size()}
FROM ${table} FROM ${table}
WHERE id = #{id} WHERE project_id = #{projectId}
AND project_id = #{projectId} and id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select> </select>
</mapper> </mapper>

View File

@ -4,7 +4,6 @@ package io.metersphere.security;
import io.metersphere.base.mapper.ext.ExtCheckOwnerMapper; import io.metersphere.base.mapper.ext.ExtCheckOwnerMapper;
import io.metersphere.commons.constants.UserGroupConstants; import io.metersphere.commons.constants.UserGroupConstants;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -23,6 +22,8 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
@Aspect @Aspect
@ -41,7 +42,7 @@ public class CheckOwnerAspect {
@Before("pointcut()") @Before("pointcut()")
public void before(JoinPoint joinPoint) { public void before(JoinPoint joinPoint) {
try {
//从切面织入点处通过反射机制获取织入点处的方法 //从切面织入点处通过反射机制获取织入点处的方法
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入点所在的方法 //获取切入点所在的方法
@ -72,13 +73,14 @@ public class CheckOwnerAspect {
Expression titleExp = parser.parseExpression(resourceId); Expression titleExp = parser.parseExpression(resourceId);
Object v = titleExp.getValue(context, Object.class); Object v = titleExp.getValue(context, Object.class);
if (v instanceof String id) { if (v instanceof String id) {
if (!extCheckOwnerMapper.checkoutOwner(resourceType, SessionUtils.getCurrentProjectId(), id)) { if (!extCheckOwnerMapper.checkoutOwner(resourceType, SessionUtils.getCurrentProjectId(), List.of(id))) {
MSException.throwException(Translator.get("check_owner_case")); MSException.throwException(Translator.get("check_owner_case"));
} }
} }
} catch (Exception e) { if (v instanceof List ids) {
LogUtil.error(e.getMessage(), e); if (!extCheckOwnerMapper.checkoutOwner(resourceType, SessionUtils.getCurrentProjectId(), ids)) {
MSException.throwException(Translator.get("check_owner_case"));
}
} }
} }
} }