forked from DataLinkDC/dinky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Improve] Improve the implementation logic for task operation permiss…
…ion check (DataLinkDC#3610) Co-authored-by: luoshangjie <[email protected]> Co-authored-by: 18216499322 <[email protected]>
- Loading branch information
1 parent
7cdc3bf
commit 3b4b431
Showing
16 changed files
with
278 additions
and
159 deletions.
There are no files selected for viewing
137 changes: 0 additions & 137 deletions
137
dinky-admin/src/main/java/org/dinky/aop/TaskOperationCheckAspect.java
This file was deleted.
Oops, something went wrong.
126 changes: 126 additions & 0 deletions
126
dinky-admin/src/main/java/org/dinky/aop/TaskOperationPermissionAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package org.dinky.aop; | ||
|
||
import org.dinky.data.annotations.CheckTaskOwner; | ||
import org.dinky.data.constant.BaseConstant; | ||
import org.dinky.data.enums.Status; | ||
import org.dinky.data.enums.TaskOwnerLockStrategyEnum; | ||
import org.dinky.data.exception.BusException; | ||
import org.dinky.data.model.SystemConfiguration; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.util.Objects; | ||
|
||
import javax.annotation.Resource; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.stereotype.Component; | ||
|
||
import cn.dev33.satoken.stp.StpUtil; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Aspect | ||
@Slf4j | ||
@Component | ||
public class TaskOperationPermissionAspect { | ||
|
||
@Resource | ||
private ApplicationContext applicationContext; | ||
|
||
/** | ||
* Check whether the user has the permission to perform the task. | ||
* | ||
* @param joinPoint | ||
* @param checkTaskOwner | ||
* @return | ||
* @throws Throwable | ||
*/ | ||
@Around(value = "@annotation(checkTaskOwner)") | ||
public Object processAround(ProceedingJoinPoint joinPoint, CheckTaskOwner checkTaskOwner) throws Throwable { | ||
if (!TaskOwnerLockStrategyEnum.ALL.equals( | ||
SystemConfiguration.getInstances().getTaskOwnerLockStrategy()) | ||
&& BaseConstant.ADMIN_ID != StpUtil.getLoginIdAsInt()) { | ||
Class checkParam = checkTaskOwner.checkParam(); | ||
Object param = getParam(joinPoint, checkParam); | ||
if (Objects.nonNull(param)) { | ||
Object bean = applicationContext.getBean(checkTaskOwner.checkInterface()); | ||
Class<?> clazz = bean.getClass(); | ||
Method method = clazz.getMethod(checkTaskOwner.checkMethod(), param.getClass()); | ||
Object invoke = method.invoke(bean, param); | ||
if (invoke != null && !(Boolean) invoke) { | ||
throw new BusException(Status.TASK_NOT_OPERATE_PERMISSION); | ||
} | ||
} | ||
} | ||
|
||
Object result; | ||
try { | ||
result = joinPoint.proceed(); | ||
} catch (Throwable e) { | ||
throw e; | ||
} | ||
return result; | ||
} | ||
|
||
private Object getParam(ProceedingJoinPoint joinPoint, Class paramAnno) throws IllegalAccessException { | ||
Object[] params = joinPoint.getArgs(); | ||
if (params.length == 0) { | ||
return null; | ||
} | ||
|
||
Object paramObj = null; | ||
// Get the method, here you can convert the signature strong to MethodSignature | ||
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); | ||
Method method = signature.getMethod(); | ||
|
||
Annotation[][] annotations = method.getParameterAnnotations(); | ||
for (int i = 0; i < annotations.length; i++) { | ||
Object param = params[i]; | ||
if (param == null) { | ||
continue; | ||
} | ||
Annotation[] paramAnn = annotations[i]; | ||
for (Annotation annotation : paramAnn) { | ||
if (annotation.annotationType() == paramAnno) { | ||
paramObj = param; | ||
break; | ||
} | ||
} | ||
if (paramObj == null) { | ||
Field[] fields = param.getClass().getDeclaredFields(); | ||
for (Field field : fields) { | ||
if (field.isAnnotationPresent(paramAnno)) { | ||
field.setAccessible(true); | ||
paramObj = field.get(param); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
return paramObj; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.