refactor: 通过注解来拦截资源和项目的归属关系

This commit is contained in:
CaptainB 2023-12-13 14:44:16 +08:00
parent edf466ad35
commit 0adb7ab821
1 changed files with 29 additions and 34 deletions

View File

@ -3,7 +3,6 @@ package io.metersphere.system.security;
import io.metersphere.sdk.constants.InternalUserRole; import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.mapper.ExtCheckOwnerMapper; import io.metersphere.system.mapper.ExtCheckOwnerMapper;
import io.metersphere.system.utils.SessionUtils; import io.metersphere.system.utils.SessionUtils;
@ -41,41 +40,37 @@ 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(); //获取切入点所在的方法
//获取切入点所在的方法 Method method = signature.getMethod();
Method method = signature.getMethod(); //获取参数对象数组
//获取参数对象数组 Object[] args = joinPoint.getArgs();
Object[] args = joinPoint.getArgs(); CheckOwner checkOwner = method.getAnnotation(CheckOwner.class);
CheckOwner checkOwner = method.getAnnotation(CheckOwner.class); long count = SessionUtils.getUser().getUserRoles()
long count = SessionUtils.getUser().getUserRoles() .stream()
.stream() .filter(g -> StringUtils.equalsIgnoreCase(g.getId(), InternalUserRole.ADMIN.getValue()))
.filter(g -> StringUtils.equalsIgnoreCase(g.getId(), InternalUserRole.ADMIN.getValue())) .count();
.count(); if (count > 0) {
if (count > 0) { return;
return; }
} // 操作内容
// 操作内容 //获取方法参数名
//获取方法参数名 String[] params = discoverer.getParameterNames(method);
String[] params = discoverer.getParameterNames(method); //将参数纳入Spring管理
//将参数纳入Spring管理 EvaluationContext context = new StandardEvaluationContext();
EvaluationContext context = new StandardEvaluationContext(); for (int len = 0; len < params.length; len++) {
for (int len = 0; len < params.length; len++) { context.setVariable(params[len], args[len]);
context.setVariable(params[len], args[len]); }
}
String resourceId = checkOwner.resourceId(); String resourceId = checkOwner.resourceId();
String resourceType = checkOwner.resourceType(); String resourceType = checkOwner.resourceType();
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(), id)) {
throw new MSException(Translator.get("check_owner_case")); throw new MSException(Translator.get("check_owner_case"));
}
} }
} catch (Exception e) {
LogUtils.error(e.getMessage(), e);
} }
} }