From a5ca4e2dadbfc5dc4577af5c0ad8bfd7f15fd8b6 Mon Sep 17 00:00:00 2001
From: shao1121353141 <>
Date: Wed, 13 Sep 2023 14:47:09 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BA=94=E7=94=A8=E5=88=97=E8=A1=A8=E6=A0=B9?=
=?UTF-8?q?=E6=8D=AE=E5=88=86=E7=BB=84id=E6=9F=A5=E8=AF=A2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../common/entity/app/query}/AppQuery.java | 10 ++-
.../app/AppGroupAssociationRepository.java | 8 +++
.../app/AppRepositoryCustomized.java | 11 +++
.../app/impl/AppRepositoryCustomizedImpl.java | 51 +++++++++++++
.../console/controller/app/AppController.java | 2 +-
.../console/converter/app/AppConverter.java | 2 +-
.../pojo/update/app/AppUpdateParam.java | 7 ++
.../console/service/app/AppService.java | 2 +-
.../service/app/impl/AppServiceImpl.java | 71 +++++++++----------
9 files changed, 121 insertions(+), 43 deletions(-)
rename {eiam-console/src/main/java/cn/topiam/employee/console/pojo/query/app => eiam-common/src/main/java/cn/topiam/employee/common/entity/app/query}/AppQuery.java (87%)
diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/pojo/query/app/AppQuery.java b/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/query/AppQuery.java
similarity index 87%
rename from eiam-console/src/main/java/cn/topiam/employee/console/pojo/query/app/AppQuery.java
rename to eiam-common/src/main/java/cn/topiam/employee/common/entity/app/query/AppQuery.java
index 882ac49c..00366cf0 100644
--- a/eiam-console/src/main/java/cn/topiam/employee/console/pojo/query/app/AppQuery.java
+++ b/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/query/AppQuery.java
@@ -1,5 +1,5 @@
/*
- * eiam-console - Employee Identity and Access Management
+ * eiam-common - Employee Identity and Access Management
* Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
*
* This program is free software: you can redistribute it and/or modify
@@ -15,7 +15,7 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
-package cn.topiam.employee.console.pojo.query.app;
+package cn.topiam.employee.common.entity.app.query;
import java.io.Serializable;
@@ -50,4 +50,10 @@ public class AppQuery implements Serializable {
*/
@Parameter(description = "协议类型")
private AppProtocol protocol;
+
+ /**
+ * 应用组ID
+ */
+ @Parameter(description = "应用组ID")
+ private Long groupId;
}
diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppGroupAssociationRepository.java b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppGroupAssociationRepository.java
index 652acc26..2ef21deb 100644
--- a/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppGroupAssociationRepository.java
+++ b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppGroupAssociationRepository.java
@@ -51,4 +51,12 @@ public interface AppGroupAssociationRepository extends
@Query(value = "UPDATE app_group_association SET " + SOFT_DELETE_SET
+ " WHERE app_id = :appId and group_id = :groupId", nativeQuery = true)
void deleteByGroupIdAndAppId(@Param("groupId") Long groupId, @Param("appId") Long appId);
+
+
+ /**
+ * 根据应用ID删除关联信息
+ *
+ * @param appId {@link Long}
+ */
+ void deleteByAppId(Long appId);
}
diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppRepositoryCustomized.java b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppRepositoryCustomized.java
index 448152ea..bea2c11f 100644
--- a/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppRepositoryCustomized.java
+++ b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppRepositoryCustomized.java
@@ -23,6 +23,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import cn.topiam.employee.common.entity.app.AppEntity;
+import cn.topiam.employee.common.entity.app.query.AppQuery;
/**
* 应用 Repository Customized
@@ -42,4 +43,14 @@ public interface AppRepositoryCustomized {
* @return {@link List}
*/
Page getAppList(Long userId, String name, Long groupId, Pageable pageable);
+
+ /**
+ *
+ * 获取应用列表
+ *
+ * @param appQuery {@link AppQuery}
+ * @param pageable {@link Pageable}
+ * @return {@link List}
+ */
+ Page getAppList(AppQuery appQuery, Pageable pageable);
}
diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/impl/AppRepositoryCustomizedImpl.java b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/impl/AppRepositoryCustomizedImpl.java
index d356daf2..d52f075a 100644
--- a/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/impl/AppRepositoryCustomizedImpl.java
+++ b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/impl/AppRepositoryCustomizedImpl.java
@@ -20,11 +20,13 @@ package cn.topiam.employee.common.repository.app.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
+import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
@@ -33,6 +35,7 @@ import com.google.common.collect.Lists;
import cn.topiam.employee.common.entity.account.OrganizationMemberEntity;
import cn.topiam.employee.common.entity.account.UserGroupMemberEntity;
import cn.topiam.employee.common.entity.app.AppEntity;
+import cn.topiam.employee.common.entity.app.query.AppQuery;
import cn.topiam.employee.common.repository.account.OrganizationMemberRepository;
import cn.topiam.employee.common.repository.account.UserGroupMemberRepository;
import cn.topiam.employee.common.repository.app.AppRepositoryCustomized;
@@ -107,6 +110,54 @@ public class AppRepositoryCustomizedImpl implements AppRepositoryCustomized {
return new PageImpl<>(list, pageable, count);
}
+ /**
+ * 获取应用列表
+ *
+ * @param appQuery {@link AppQuery}
+ * @param pageable {@link Pageable}
+ * @return {@link List}
+ */
+ public Page getAppList(AppQuery appQuery, Pageable pageable) {
+ //@formatter:off
+ StringBuilder builder = new StringBuilder("""
+ SELECT DISTINCT
+ app.*
+ FROM
+ app
+ INNER JOIN app_group_association `group` ON app.id_ = `group`.app_id
+ """);
+
+ //应用名称
+ if (StringUtils.isNoneBlank(appQuery.getName())) {
+ builder.append(" AND app.name_ like '%").append(appQuery.getName()).append("%'");
+ }
+ //协议类型
+ if (Objects.nonNull(appQuery.getProtocol())) {
+ builder.append(" AND app.protocol_ = ").append(appQuery.getProtocol().getCode());
+ }
+ //应用组ID
+ if(Objects.nonNull(appQuery.getGroupId())){
+ builder.append(" AND group.group_id = ").append(appQuery.getGroupId());
+ }
+
+ //@formatter:on
+ String sql = builder.toString();
+ List list = jdbcTemplate.query(
+ builder.append(" LIMIT ").append(pageable.getPageNumber() * pageable.getPageSize())
+ .append(",").append(pageable.getPageSize()).toString(),
+ new AppEntityMapper());
+ //@formatter:off
+ String countSql = "SELECT count(*) FROM (" + sql + ") app_account_";
+ //@formatter:on
+ Integer count = jdbcTemplate.queryForObject(countSql, Integer.class);
+ return new PageImpl<>(list, pageable, count);
+ }
+
+ /**
+ * JdbcTemplate
+ */
+ private final JdbcTemplate jdbcTemplate;
+
/**
* NamedParameterJdbcTemplate
*/
diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/controller/app/AppController.java b/eiam-console/src/main/java/cn/topiam/employee/console/controller/app/AppController.java
index e4cdb35a..466c5b96 100644
--- a/eiam-console/src/main/java/cn/topiam/employee/console/controller/app/AppController.java
+++ b/eiam-console/src/main/java/cn/topiam/employee/console/controller/app/AppController.java
@@ -24,7 +24,7 @@ import org.springframework.web.bind.annotation.*;
import cn.topiam.employee.audit.annotation.Audit;
import cn.topiam.employee.audit.event.type.EventType;
-import cn.topiam.employee.console.pojo.query.app.AppQuery;
+import cn.topiam.employee.common.entity.app.query.AppQuery;
import cn.topiam.employee.console.pojo.result.app.AppCreateResult;
import cn.topiam.employee.console.pojo.result.app.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult;
diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/converter/app/AppConverter.java b/eiam-console/src/main/java/cn/topiam/employee/console/converter/app/AppConverter.java
index c37ffefa..fd00b4d2 100644
--- a/eiam-console/src/main/java/cn/topiam/employee/console/converter/app/AppConverter.java
+++ b/eiam-console/src/main/java/cn/topiam/employee/console/converter/app/AppConverter.java
@@ -32,7 +32,7 @@ import cn.topiam.employee.application.ApplicationService;
import cn.topiam.employee.application.ApplicationServiceLoader;
import cn.topiam.employee.common.entity.app.AppEntity;
import cn.topiam.employee.common.entity.app.QAppEntity;
-import cn.topiam.employee.console.pojo.query.app.AppQuery;
+import cn.topiam.employee.common.entity.app.query.AppQuery;
import cn.topiam.employee.console.pojo.result.app.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult;
import cn.topiam.employee.console.pojo.update.app.AppUpdateParam;
diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/pojo/update/app/AppUpdateParam.java b/eiam-console/src/main/java/cn/topiam/employee/console/pojo/update/app/AppUpdateParam.java
index 777667e4..6d581f77 100644
--- a/eiam-console/src/main/java/cn/topiam/employee/console/pojo/update/app/AppUpdateParam.java
+++ b/eiam-console/src/main/java/cn/topiam/employee/console/pojo/update/app/AppUpdateParam.java
@@ -18,6 +18,7 @@
package cn.topiam.employee.console.pojo.update.app;
import java.io.Serializable;
+import java.util.List;
import lombok.Data;
@@ -58,4 +59,10 @@ public class AppUpdateParam implements Serializable {
*/
@Schema(description = "备注")
private String remark;
+
+ /**
+ * 应用分组id
+ */
+ @Schema(description = "应用分组")
+ private List groupIds;
}
diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/service/app/AppService.java b/eiam-console/src/main/java/cn/topiam/employee/console/service/app/AppService.java
index d4f6e057..b24e4517 100644
--- a/eiam-console/src/main/java/cn/topiam/employee/console/service/app/AppService.java
+++ b/eiam-console/src/main/java/cn/topiam/employee/console/service/app/AppService.java
@@ -19,7 +19,7 @@ package cn.topiam.employee.console.service.app;
import java.util.Map;
-import cn.topiam.employee.console.pojo.query.app.AppQuery;
+import cn.topiam.employee.common.entity.app.query.AppQuery;
import cn.topiam.employee.console.pojo.result.app.AppCreateResult;
import cn.topiam.employee.console.pojo.result.app.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult;
diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/service/app/impl/AppServiceImpl.java b/eiam-console/src/main/java/cn/topiam/employee/console/service/app/impl/AppServiceImpl.java
index 5bf37d10..301f7280 100644
--- a/eiam-console/src/main/java/cn/topiam/employee/console/service/app/impl/AppServiceImpl.java
+++ b/eiam-console/src/main/java/cn/topiam/employee/console/service/app/impl/AppServiceImpl.java
@@ -17,17 +17,17 @@
*/
package cn.topiam.employee.console.service.app.impl;
-import java.time.LocalDateTime;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Map;
import java.util.Optional;
-import org.springframework.data.querydsl.QPageRequest;
+import cn.topiam.employee.common.entity.app.AppGroupAssociationEntity;
+import cn.topiam.employee.common.repository.app.AppGroupAssociationRepository;
+import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import com.querydsl.core.types.OrderSpecifier;
-import com.querydsl.core.types.Predicate;
-
import cn.topiam.employee.application.ApplicationService;
import cn.topiam.employee.application.ApplicationServiceLoader;
import cn.topiam.employee.application.exception.AppNotExistException;
@@ -35,10 +35,9 @@ import cn.topiam.employee.audit.context.AuditContext;
import cn.topiam.employee.audit.entity.Target;
import cn.topiam.employee.audit.enums.TargetType;
import cn.topiam.employee.common.entity.app.AppEntity;
-import cn.topiam.employee.common.entity.app.QAppEntity;
+import cn.topiam.employee.common.entity.app.query.AppQuery;
import cn.topiam.employee.common.repository.app.AppRepository;
import cn.topiam.employee.console.converter.app.AppConverter;
-import cn.topiam.employee.console.pojo.query.app.AppQuery;
import cn.topiam.employee.console.pojo.result.app.AppCreateResult;
import cn.topiam.employee.console.pojo.result.app.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult;
@@ -53,6 +52,7 @@ import cn.topiam.employee.support.util.BeanUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+
import static cn.topiam.employee.support.repository.domain.BaseEntity.LAST_MODIFIED_BY;
import static cn.topiam.employee.support.repository.domain.BaseEntity.LAST_MODIFIED_TIME;
@@ -76,15 +76,9 @@ public class AppServiceImpl implements AppService {
*/
@Override
public Page getAppList(PageModel pageModel, AppQuery query) {
- //查询条件
- Predicate predicate = appConverter.queryAppListParamConvertToPredicate(query);
- OrderSpecifier desc = QAppEntity.appEntity.updateTime.desc();
- //分页条件
- QPageRequest request = QPageRequest.of(pageModel.getCurrent(), pageModel.getPageSize(),
- desc);
//查询映射
- org.springframework.data.domain.Page list = appRepository.findAll(predicate,
- request);
+ org.springframework.data.domain.Page list = appRepository.getAppList(query,
+ PageRequest.of(pageModel.getCurrent(), pageModel.getPageSize()));
return appConverter.entityConvertToAppListResult(list);
}
@@ -98,9 +92,9 @@ public class AppServiceImpl implements AppService {
@Transactional(rollbackFor = Exception.class)
public AppCreateResult createApp(AppCreateParam param) {
ApplicationService applicationService = applicationServiceLoader
- .getApplicationService(param.getTemplate());
+ .getApplicationService(param.getTemplate());
String appId = applicationService.create(param.getName(), param.getIcon(),
- param.getRemark(), param.getGroupIds());
+ param.getRemark(), param.getGroupIds());
AuditContext.setTarget(Target.builder().id(appId).type(TargetType.APPLICATION).build());
return new AppCreateResult(appId);
}
@@ -112,13 +106,23 @@ public class AppServiceImpl implements AppService {
* @return {@link Boolean}
*/
@Override
+ @Transactional(rollbackFor = Exception.class)
public boolean updateApp(AppUpdateParam param) {
AppEntity app = appRequireNonNull(param.getId());
AppEntity entity = appConverter.appUpdateParamConverterToEntity(param);
BeanUtils.merge(entity, app, LAST_MODIFIED_TIME, LAST_MODIFIED_BY);
appRepository.save(app);
+ appGroupAssociationRepository.deleteByAppId(app.getId());
+ List list = new ArrayList<>();
+ for (String id : param.getGroupIds()) {
+ AppGroupAssociationEntity appGroupAssociationEntity = new AppGroupAssociationEntity();
+ appGroupAssociationEntity.setGroupId(Long.valueOf(id));
+ appGroupAssociationEntity.setAppId(app.getId());
+ list.add(appGroupAssociationEntity);
+ }
+ appGroupAssociationRepository.saveAll(list);
AuditContext.setTarget(
- Target.builder().id(param.getId().toString()).type(TargetType.APPLICATION).build());
+ Target.builder().id(param.getId().toString()).type(TargetType.APPLICATION).build());
return true;
}
@@ -134,7 +138,7 @@ public class AppServiceImpl implements AppService {
AppEntity app = appRequireNonNull(id);
applicationServiceLoader.getApplicationService(app.getTemplate()).delete(id.toString());
AuditContext
- .setTarget(Target.builder().id(id.toString()).type(TargetType.APPLICATION).build());
+ .setTarget(Target.builder().id(id.toString()).type(TargetType.APPLICATION).build());
return true;
}
@@ -192,10 +196,10 @@ public class AppServiceImpl implements AppService {
@Override
public Boolean saveAppConfig(AppSaveConfigParam param) {
ApplicationService applicationService = applicationServiceLoader
- .getApplicationService(param.getTemplate());
+ .getApplicationService(param.getTemplate());
applicationService.saveConfig(param.getId(), param.getConfig());
AuditContext
- .setTarget(Target.builder().id(param.getId()).type(TargetType.APPLICATION).build());
+ .setTarget(Target.builder().id(param.getId()).type(TargetType.APPLICATION).build());
return true;
}
@@ -210,7 +214,7 @@ public class AppServiceImpl implements AppService {
Optional optional = appRepository.findById(Long.valueOf(appId));
if (optional.isPresent()) {
ApplicationService applicationService = applicationServiceLoader
- .getApplicationService(optional.get().getTemplate());
+ .getApplicationService(optional.get().getTemplate());
return applicationService.getConfig(appId);
}
throw new AppNotExistException();
@@ -240,24 +244,15 @@ public class AppServiceImpl implements AppService {
/**
* ApplicationRepository
*/
- private final AppRepository appRepository;
-
- // /**
- // * 应用证书
- // */
- // private final AppCertRepository appCertRepository;
- //
- // /**
- // * 应用账户
- // */
- // private final AppAccountRepository appAccountRepository;
- // /**
- // * 应用策略
- // */
- // private final AppAccessPolicyRepository appAccessPolicyRepository;
+ private final AppRepository appRepository;
/**
* ApplicationConverter
*/
- private final AppConverter appConverter;
+ private final AppConverter appConverter;
+
+ /**
+ * AppGroupAssociationRepositorys
+ */
+ private final AppGroupAssociationRepository appGroupAssociationRepository;
}