From 31ba1baa3146d170cd939952aff6f599e3100be3 Mon Sep 17 00:00:00 2001 From: wangjiahao <1522128093@qq.com> Date: Tue, 31 May 2022 12:04:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BC=93=E5=AD=98=E6=B8=85?= =?UTF-8?q?=E7=90=86=E6=B3=A8=E8=A7=A3=E6=94=B9=E4=B8=BA=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E5=8A=A0=E5=BF=AB=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/aop/DeCleanerAnnotationHandler.java | 91 ++--------------- .../service/decatch/DeCatchProcess.java | 97 +++++++++++++++++++ 2 files changed, 105 insertions(+), 83 deletions(-) create mode 100644 backend/src/main/java/io/dataease/service/decatch/DeCatchProcess.java diff --git a/backend/src/main/java/io/dataease/auth/aop/DeCleanerAnnotationHandler.java b/backend/src/main/java/io/dataease/auth/aop/DeCleanerAnnotationHandler.java index 3584de725a..0132c5559e 100644 --- a/backend/src/main/java/io/dataease/auth/aop/DeCleanerAnnotationHandler.java +++ b/backend/src/main/java/io/dataease/auth/aop/DeCleanerAnnotationHandler.java @@ -1,18 +1,13 @@ package io.dataease.auth.aop; import io.dataease.auth.annotation.DeCleaner; -import io.dataease.auth.api.dto.CurrentUserDto; -import io.dataease.commons.constants.AuthConstants; import io.dataease.commons.constants.DePermissionType; -import io.dataease.commons.model.AuthURD; import io.dataease.commons.utils.AopUtils; -import io.dataease.commons.utils.AuthUtils; +import io.dataease.commons.utils.CommonBeanFactory; import io.dataease.commons.utils.LogUtil; -import io.dataease.listener.util.CacheUtils; -import org.apache.commons.collections4.CollectionUtils; +import io.dataease.service.decatch.DeCatchProcess; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ObjectUtils; -import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; @@ -20,9 +15,6 @@ import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Method; -import java.util.List; -import java.util.Optional; - @Aspect @Component public class DeCleanerAnnotationHandler { @@ -43,15 +35,16 @@ public class DeCleanerAnnotationHandler { paramValue = AopUtils.getParamValue(arg, key, 0); } + switch (type.name()) { case "DATASOURCE": - cleanDataSource(paramValue); + catchProcess().cleanDataSource(paramValue); break; case "DATASET": - cleanDataSet(paramValue); + catchProcess().cleanDataSet(paramValue); break; default: - cleanPanel(paramValue); + catchProcess().cleanPanel(paramValue); break; } } catch (Throwable e) { @@ -60,78 +53,10 @@ public class DeCleanerAnnotationHandler { } } - - - private void cleanCacheParent(String pid, String type) { - if (StringUtils.isBlank(pid) || StringUtils.isBlank(type)) { - return; - } - CurrentUserDto user = AuthUtils.getUser(); - List resourceIds = AuthUtils.parentResources(pid.toString(), type); - if (CollectionUtils.isEmpty(resourceIds))return; - resourceIds.forEach(resourceId -> { - AuthURD authURD = AuthUtils.authURDR(resourceId); - Optional.ofNullable(authURD.getUserIds()).ifPresent(ids -> { - ids.forEach(id -> { - CacheUtils.remove("user_"+type, "user" + id); - }); - }); - Optional.ofNullable(authURD.getRoleIds()).ifPresent(ids -> { - ids.forEach(id -> { - CacheUtils.remove("role_"+type, "role" + id); - }); - }); - Optional.ofNullable(authURD.getDeptIds()).ifPresent(ids -> { - ids.forEach(id -> { - List depts = AuthUtils.getAuthModels(id.toString(), "dept", user.getUserId(), user.getIsAdmin()); - depts.forEach(deptId -> { - CacheUtils.remove("dept_"+type, "dept" + deptId); - }); - }); - }); - }); + public DeCatchProcess catchProcess() { + return CommonBeanFactory.getBean(DeCatchProcess.class); } - public void cleanPanel(Object pid) { - CurrentUserDto user = AuthUtils.getUser(); - CacheUtils.remove(AuthConstants.USER_PANEL_NAME, "user" + user.getUserId()); - CacheUtils.remove(AuthConstants.DEPT_PANEL_NAME, "dept" + user.getDeptId()); - user.getRoles().forEach(role -> { - CacheUtils.remove(AuthConstants.ROLE_PANEL_NAME, "role" + role.getId()); - }); - - Optional.ofNullable(pid).ifPresent(resourceId -> { - cleanCacheParent(resourceId.toString(), "panel"); - }); - - - } - - public void cleanDataSet(Object pid) { - CurrentUserDto user = AuthUtils.getUser(); - CacheUtils.remove(AuthConstants.USER_DATASET_NAME, "user" + user.getUserId()); - CacheUtils.remove(AuthConstants.DEPT_DATASET_NAME, "dept" + user.getDeptId()); - user.getRoles().forEach(role -> { - CacheUtils.remove(AuthConstants.ROLE_DATASET_NAME, "role" + role.getId()); - }); - - Optional.ofNullable(pid).ifPresent(resourceId -> { - cleanCacheParent(resourceId.toString(), "dataset"); - }); - } - - public void cleanDataSource(Object pid) { - CurrentUserDto user = AuthUtils.getUser(); - CacheUtils.remove(AuthConstants.USER_LINK_NAME, "user" + user.getUserId()); - CacheUtils.remove(AuthConstants.DEPT_LINK_NAME, "dept" + user.getDeptId()); - user.getRoles().forEach(role -> { - CacheUtils.remove(AuthConstants.ROLE_LINK_NAME, "role" + role.getId()); - }); - - Optional.ofNullable(pid).ifPresent(resourceId -> { - cleanCacheParent(resourceId.toString(), "link"); - }); - } } diff --git a/backend/src/main/java/io/dataease/service/decatch/DeCatchProcess.java b/backend/src/main/java/io/dataease/service/decatch/DeCatchProcess.java new file mode 100644 index 0000000000..23010d6039 --- /dev/null +++ b/backend/src/main/java/io/dataease/service/decatch/DeCatchProcess.java @@ -0,0 +1,97 @@ +package io.dataease.service.decatch; + +import io.dataease.auth.api.dto.CurrentUserDto; +import io.dataease.commons.constants.AuthConstants; +import io.dataease.commons.model.AuthURD; +import io.dataease.commons.utils.AuthUtils; +import io.dataease.listener.util.CacheUtils; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +/** + * Author: wangjiahao + * Date: 2022/5/31 + * Description: + */ +@Service +public class DeCatchProcess { + + @Async("taskExecutor") + public void cleanPanel(Object pid) { + CurrentUserDto user = AuthUtils.getUser(); + CacheUtils.remove(AuthConstants.USER_PANEL_NAME, "user" + user.getUserId()); + CacheUtils.remove(AuthConstants.DEPT_PANEL_NAME, "dept" + user.getDeptId()); + user.getRoles().forEach(role -> { + CacheUtils.remove(AuthConstants.ROLE_PANEL_NAME, "role" + role.getId()); + }); + + Optional.ofNullable(pid).ifPresent(resourceId -> { + cleanCacheParent(resourceId.toString(), "panel"); + }); + + + } + + @Async("taskExecutor") + public void cleanDataSet(Object pid) { + CurrentUserDto user = AuthUtils.getUser(); + CacheUtils.remove(AuthConstants.USER_DATASET_NAME, "user" + user.getUserId()); + CacheUtils.remove(AuthConstants.DEPT_DATASET_NAME, "dept" + user.getDeptId()); + user.getRoles().forEach(role -> { + CacheUtils.remove(AuthConstants.ROLE_DATASET_NAME, "role" + role.getId()); + }); + + Optional.ofNullable(pid).ifPresent(resourceId -> { + cleanCacheParent(resourceId.toString(), "dataset"); + }); + } + + @Async("taskExecutor") + public void cleanDataSource(Object pid) { + CurrentUserDto user = AuthUtils.getUser(); + CacheUtils.remove(AuthConstants.USER_LINK_NAME, "user" + user.getUserId()); + CacheUtils.remove(AuthConstants.DEPT_LINK_NAME, "dept" + user.getDeptId()); + user.getRoles().forEach(role -> { + CacheUtils.remove(AuthConstants.ROLE_LINK_NAME, "role" + role.getId()); + }); + + Optional.ofNullable(pid).ifPresent(resourceId -> { + cleanCacheParent(resourceId.toString(), "link"); + }); + } + + private void cleanCacheParent(String pid, String type) { + if (StringUtils.isBlank(pid) || StringUtils.isBlank(type)) { + return; + } + CurrentUserDto user = AuthUtils.getUser(); + List resourceIds = AuthUtils.parentResources(pid.toString(), type); + if (CollectionUtils.isEmpty(resourceIds))return; + resourceIds.forEach(resourceId -> { + AuthURD authURD = AuthUtils.authURDR(resourceId); + Optional.ofNullable(authURD.getUserIds()).ifPresent(ids -> { + ids.forEach(id -> { + CacheUtils.remove("user_"+type, "user" + id); + }); + }); + Optional.ofNullable(authURD.getRoleIds()).ifPresent(ids -> { + ids.forEach(id -> { + CacheUtils.remove("role_"+type, "role" + id); + }); + }); + Optional.ofNullable(authURD.getDeptIds()).ifPresent(ids -> { + ids.forEach(id -> { + List depts = AuthUtils.getAuthModels(id.toString(), "dept", user.getUserId(), user.getIsAdmin()); + depts.forEach(deptId -> { + CacheUtils.remove("dept_"+type, "dept" + deptId); + }); + }); + }); + }); + } +}