应用组相关功能优化

pull/48/head
Friday 2023-09-13 08:19:29 +00:00 committed by smallbun
parent 7873c24dca
commit d0cf4d3fb3
10 changed files with 124 additions and 45 deletions

View File

@ -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) * Copyright © 2022-Present Jinan Yuanchuang Network Technology Co., Ltd. (support@topiam.cn)
* *
* This program is free software: you can redistribute it and/or modify * 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 * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package cn.topiam.employee.console.pojo.query.app; package cn.topiam.employee.common.entity.app.query;
import java.io.Serializable; import java.io.Serializable;
@ -50,4 +50,10 @@ public class AppQuery implements Serializable {
*/ */
@Parameter(description = "协议类型") @Parameter(description = "协议类型")
private AppProtocol protocol; private AppProtocol protocol;
/**
* ID
*/
@Parameter(description = "应用组ID")
private Long groupId;
} }

View File

@ -51,4 +51,12 @@ public interface AppGroupAssociationRepository extends
@Query(value = "UPDATE app_group_association SET " + SOFT_DELETE_SET @Query(value = "UPDATE app_group_association SET " + SOFT_DELETE_SET
+ " WHERE app_id = :appId and group_id = :groupId", nativeQuery = true) + " WHERE app_id = :appId and group_id = :groupId", nativeQuery = true)
void deleteByGroupIdAndAppId(@Param("groupId") Long groupId, @Param("appId") Long appId); void deleteByGroupIdAndAppId(@Param("groupId") Long groupId, @Param("appId") Long appId);
/**
* ID
*
* @param appId {@link Long}
*/
void deleteByAppId(Long appId);
} }

View File

@ -23,6 +23,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import cn.topiam.employee.common.entity.app.AppEntity; import cn.topiam.employee.common.entity.app.AppEntity;
import cn.topiam.employee.common.entity.app.query.AppQuery;
/** /**
* Repository Customized * Repository Customized
@ -42,4 +43,14 @@ public interface AppRepositoryCustomized {
* @return {@link List} * @return {@link List}
*/ */
Page<AppEntity> getAppList(Long userId, String name, Long groupId, Pageable pageable); Page<AppEntity> getAppList(Long userId, String name, Long groupId, Pageable pageable);
/**
*
*
*
* @param appQuery {@link AppQuery}
* @param pageable {@link Pageable}
* @return {@link List}
*/
Page<AppEntity> getAppList(AppQuery appQuery, Pageable pageable);
} }

View File

@ -20,11 +20,13 @@ package cn.topiam.employee.common.repository.app.impl;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository; 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.OrganizationMemberEntity;
import cn.topiam.employee.common.entity.account.UserGroupMemberEntity; import cn.topiam.employee.common.entity.account.UserGroupMemberEntity;
import cn.topiam.employee.common.entity.app.AppEntity; 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.OrganizationMemberRepository;
import cn.topiam.employee.common.repository.account.UserGroupMemberRepository; import cn.topiam.employee.common.repository.account.UserGroupMemberRepository;
import cn.topiam.employee.common.repository.app.AppRepositoryCustomized; import cn.topiam.employee.common.repository.app.AppRepositoryCustomized;
@ -107,6 +110,54 @@ public class AppRepositoryCustomizedImpl implements AppRepositoryCustomized {
return new PageImpl<>(list, pageable, count); return new PageImpl<>(list, pageable, count);
} }
/**
*
*
* @param appQuery {@link AppQuery}
* @param pageable {@link Pageable}
* @return {@link List}
*/
public Page<AppEntity> 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<AppEntity> 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 * NamedParameterJdbcTemplate
*/ */

View File

@ -24,7 +24,7 @@ import org.springframework.web.bind.annotation.*;
import cn.topiam.employee.audit.annotation.Audit; import cn.topiam.employee.audit.annotation.Audit;
import cn.topiam.employee.audit.event.type.EventType; 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.AppCreateResult;
import cn.topiam.employee.console.pojo.result.app.AppGetResult; import cn.topiam.employee.console.pojo.result.app.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult; import cn.topiam.employee.console.pojo.result.app.AppListResult;

View File

@ -32,7 +32,7 @@ import cn.topiam.employee.application.ApplicationService;
import cn.topiam.employee.application.ApplicationServiceLoader; import cn.topiam.employee.application.ApplicationServiceLoader;
import cn.topiam.employee.common.entity.app.AppEntity; import cn.topiam.employee.common.entity.app.AppEntity;
import cn.topiam.employee.common.entity.app.QAppEntity; 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.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult; import cn.topiam.employee.console.pojo.result.app.AppListResult;
import cn.topiam.employee.console.pojo.update.app.AppUpdateParam; import cn.topiam.employee.console.pojo.update.app.AppUpdateParam;

View File

@ -18,6 +18,7 @@
package cn.topiam.employee.console.pojo.update.app; package cn.topiam.employee.console.pojo.update.app;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
import lombok.Data; import lombok.Data;
@ -58,4 +59,10 @@ public class AppUpdateParam implements Serializable {
*/ */
@Schema(description = "备注") @Schema(description = "备注")
private String remark; private String remark;
/**
* id
*/
@Schema(description = "应用分组")
private List<String> groupIds;
} }

View File

@ -19,7 +19,7 @@ package cn.topiam.employee.console.service.app;
import java.util.Map; 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.AppCreateResult;
import cn.topiam.employee.console.pojo.result.app.AppGetResult; import cn.topiam.employee.console.pojo.result.app.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult; import cn.topiam.employee.console.pojo.result.app.AppListResult;

View File

@ -17,17 +17,17 @@
*/ */
package cn.topiam.employee.console.service.app.impl; 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.Map;
import java.util.Optional; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; 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.ApplicationService;
import cn.topiam.employee.application.ApplicationServiceLoader; import cn.topiam.employee.application.ApplicationServiceLoader;
import cn.topiam.employee.application.exception.AppNotExistException; 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.entity.Target;
import cn.topiam.employee.audit.enums.TargetType; import cn.topiam.employee.audit.enums.TargetType;
import cn.topiam.employee.common.entity.app.AppEntity; 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.common.repository.app.AppRepository;
import cn.topiam.employee.console.converter.app.AppConverter; 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.AppCreateResult;
import cn.topiam.employee.console.pojo.result.app.AppGetResult; import cn.topiam.employee.console.pojo.result.app.AppGetResult;
import cn.topiam.employee.console.pojo.result.app.AppListResult; 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.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; 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_BY;
import static cn.topiam.employee.support.repository.domain.BaseEntity.LAST_MODIFIED_TIME; import static cn.topiam.employee.support.repository.domain.BaseEntity.LAST_MODIFIED_TIME;
@ -76,15 +76,9 @@ public class AppServiceImpl implements AppService {
*/ */
@Override @Override
public Page<AppListResult> getAppList(PageModel pageModel, AppQuery query) { public Page<AppListResult> getAppList(PageModel pageModel, AppQuery query) {
//查询条件
Predicate predicate = appConverter.queryAppListParamConvertToPredicate(query);
OrderSpecifier<LocalDateTime> desc = QAppEntity.appEntity.updateTime.desc();
//分页条件
QPageRequest request = QPageRequest.of(pageModel.getCurrent(), pageModel.getPageSize(),
desc);
//查询映射 //查询映射
org.springframework.data.domain.Page<AppEntity> list = appRepository.findAll(predicate, org.springframework.data.domain.Page<AppEntity> list = appRepository.getAppList(query,
request); PageRequest.of(pageModel.getCurrent(), pageModel.getPageSize()));
return appConverter.entityConvertToAppListResult(list); return appConverter.entityConvertToAppListResult(list);
} }
@ -98,9 +92,9 @@ public class AppServiceImpl implements AppService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public AppCreateResult createApp(AppCreateParam param) { public AppCreateResult createApp(AppCreateParam param) {
ApplicationService applicationService = applicationServiceLoader ApplicationService applicationService = applicationServiceLoader
.getApplicationService(param.getTemplate()); .getApplicationService(param.getTemplate());
String appId = applicationService.create(param.getName(), param.getIcon(), 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()); AuditContext.setTarget(Target.builder().id(appId).type(TargetType.APPLICATION).build());
return new AppCreateResult(appId); return new AppCreateResult(appId);
} }
@ -112,13 +106,23 @@ public class AppServiceImpl implements AppService {
* @return {@link Boolean} * @return {@link Boolean}
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class)
public boolean updateApp(AppUpdateParam param) { public boolean updateApp(AppUpdateParam param) {
AppEntity app = appRequireNonNull(param.getId()); AppEntity app = appRequireNonNull(param.getId());
AppEntity entity = appConverter.appUpdateParamConverterToEntity(param); AppEntity entity = appConverter.appUpdateParamConverterToEntity(param);
BeanUtils.merge(entity, app, LAST_MODIFIED_TIME, LAST_MODIFIED_BY); BeanUtils.merge(entity, app, LAST_MODIFIED_TIME, LAST_MODIFIED_BY);
appRepository.save(app); appRepository.save(app);
appGroupAssociationRepository.deleteByAppId(app.getId());
List<AppGroupAssociationEntity> 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( AuditContext.setTarget(
Target.builder().id(param.getId().toString()).type(TargetType.APPLICATION).build()); Target.builder().id(param.getId().toString()).type(TargetType.APPLICATION).build());
return true; return true;
} }
@ -134,7 +138,7 @@ public class AppServiceImpl implements AppService {
AppEntity app = appRequireNonNull(id); AppEntity app = appRequireNonNull(id);
applicationServiceLoader.getApplicationService(app.getTemplate()).delete(id.toString()); applicationServiceLoader.getApplicationService(app.getTemplate()).delete(id.toString());
AuditContext AuditContext
.setTarget(Target.builder().id(id.toString()).type(TargetType.APPLICATION).build()); .setTarget(Target.builder().id(id.toString()).type(TargetType.APPLICATION).build());
return true; return true;
} }
@ -192,10 +196,10 @@ public class AppServiceImpl implements AppService {
@Override @Override
public Boolean saveAppConfig(AppSaveConfigParam param) { public Boolean saveAppConfig(AppSaveConfigParam param) {
ApplicationService applicationService = applicationServiceLoader ApplicationService applicationService = applicationServiceLoader
.getApplicationService(param.getTemplate()); .getApplicationService(param.getTemplate());
applicationService.saveConfig(param.getId(), param.getConfig()); applicationService.saveConfig(param.getId(), param.getConfig());
AuditContext AuditContext
.setTarget(Target.builder().id(param.getId()).type(TargetType.APPLICATION).build()); .setTarget(Target.builder().id(param.getId()).type(TargetType.APPLICATION).build());
return true; return true;
} }
@ -210,7 +214,7 @@ public class AppServiceImpl implements AppService {
Optional<AppEntity> optional = appRepository.findById(Long.valueOf(appId)); Optional<AppEntity> optional = appRepository.findById(Long.valueOf(appId));
if (optional.isPresent()) { if (optional.isPresent()) {
ApplicationService applicationService = applicationServiceLoader ApplicationService applicationService = applicationServiceLoader
.getApplicationService(optional.get().getTemplate()); .getApplicationService(optional.get().getTemplate());
return applicationService.getConfig(appId); return applicationService.getConfig(appId);
} }
throw new AppNotExistException(); throw new AppNotExistException();
@ -240,24 +244,15 @@ public class AppServiceImpl implements AppService {
/** /**
* ApplicationRepository * ApplicationRepository
*/ */
private final AppRepository appRepository; private final AppRepository appRepository;
// /**
// * 应用证书
// */
// private final AppCertRepository appCertRepository;
//
// /**
// * 应用账户
// */
// private final AppAccountRepository appAccountRepository;
// /**
// * 应用策略
// */
// private final AppAccessPolicyRepository appAccessPolicyRepository;
/** /**
* ApplicationConverter * ApplicationConverter
*/ */
private final AppConverter appConverter; private final AppConverter appConverter;
/**
* AppGroupAssociationRepositorys
*/
private final AppGroupAssociationRepository appGroupAssociationRepository;
} }

View File

@ -20,6 +20,7 @@ package cn.topiam.employee.portal.converter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.querydsl.core.types.ExpressionUtils;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
@ -47,7 +48,7 @@ public interface AppGroupConverter {
*/ */
default Predicate queryPredicate() { default Predicate queryPredicate() {
QAppGroupEntity appGroup = QAppGroupEntity.appGroupEntity; QAppGroupEntity appGroup = QAppGroupEntity.appGroupEntity;
Predicate predicate = appGroup.deleted.eq(Boolean.FALSE); Predicate predicate = ExpressionUtils.and(appGroup.isNotNull(), appGroup.deleted.eq(Boolean.FALSE));
//@formatter:on //@formatter:on
return predicate; return predicate;
} }
@ -59,7 +60,7 @@ public interface AppGroupConverter {
*/ */
default Predicate queryAppGroupAssociationPredicate() { default Predicate queryAppGroupAssociationPredicate() {
QAppGroupAssociationEntity appGroupAssociation = QAppGroupAssociationEntity.appGroupAssociationEntity; QAppGroupAssociationEntity appGroupAssociation = QAppGroupAssociationEntity.appGroupAssociationEntity;
return appGroupAssociation.deleted.eq(Boolean.FALSE); return ExpressionUtils.and(appGroupAssociation.isNotNull(), appGroupAssociation.deleted.eq(Boolean.FALSE));
} }
/** /**