diff --git a/eiam-application/eiam-application-core/src/main/java/cn/topiam/employee/application/AbstractApplicationService.java b/eiam-application/eiam-application-core/src/main/java/cn/topiam/employee/application/AbstractApplicationService.java index 785faefe..afdba1d3 100644 --- a/eiam-application/eiam-application-core/src/main/java/cn/topiam/employee/application/AbstractApplicationService.java +++ b/eiam-application/eiam-application-core/src/main/java/cn/topiam/employee/application/AbstractApplicationService.java @@ -60,7 +60,6 @@ public abstract class AbstractApplicationService implements ApplicationService { AppEntity appEntity = new AppEntity(); appEntity.setName(name); appEntity.setIcon(icon); - appEntity.setGroupId(null == groupId ? 0L : groupId); appEntity.setCode(RandomStringUtils.randomAlphanumeric(32).toLowerCase()); appEntity.setTemplate(getCode()); appEntity.setType(getType()); diff --git a/eiam-audit/src/main/java/cn/topiam/employee/audit/event/type/AppEventType.java b/eiam-audit/src/main/java/cn/topiam/employee/audit/event/type/AppEventType.java index 295580f9..e0c83e44 100644 --- a/eiam-audit/src/main/java/cn/topiam/employee/audit/event/type/AppEventType.java +++ b/eiam-audit/src/main/java/cn/topiam/employee/audit/event/type/AppEventType.java @@ -182,4 +182,18 @@ public class AppEventType { public static Type DELETE_APP_GROUP = new Type("eiam:event:app_group:delete", "删除应用分组", APP_GROUP_RESOURCE, List.of(UserType.ADMIN)); + /** + * 添加应用分组关联 + */ + public static Type ADD_APP_GROUP_ASSOCIATION = new Type( + "eiam:event:add_app_group_association", "添加应用分组关联", APP_GROUP_RESOURCE, + List.of(UserType.ADMIN)); + + /** + * 移除应用分组关联 + */ + public static Type REMOVE_APP_GROUP_ASSOCIATION = new Type( + "eiam:event:remove_app_group_association", "移除应用分组关联", APP_GROUP_RESOURCE, + List.of(UserType.ADMIN)); + } diff --git a/eiam-audit/src/main/java/cn/topiam/employee/audit/event/type/EventType.java b/eiam-audit/src/main/java/cn/topiam/employee/audit/event/type/EventType.java index 092da84d..a68bc15a 100644 --- a/eiam-audit/src/main/java/cn/topiam/employee/audit/event/type/EventType.java +++ b/eiam-audit/src/main/java/cn/topiam/employee/audit/event/type/EventType.java @@ -503,7 +503,15 @@ public enum EventType { /** * 删除应用分组 */ - DELETE_APP_GROUP(AppEventType.DELETE_APP_GROUP); + DELETE_APP_GROUP(AppEventType.DELETE_APP_GROUP), + /** + * 添加应用组关联 + */ + ADD_APP_GROUP_ASSOCIATION(AppEventType.ADD_APP_GROUP_ASSOCIATION), + /** + * 移除应用组关联 + */ + REMOVE_APP_GROUP_ASSOCIATION(AppEventType.REMOVE_APP_GROUP_ASSOCIATION); /** * code diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/AppEntity.java b/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/AppEntity.java index 3a3c44a7..0c943dd8 100644 --- a/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/AppEntity.java +++ b/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/AppEntity.java @@ -100,12 +100,6 @@ public class AppEntity extends LogicDeleteEntity { @Column(name = "icon_") private String icon; - /** - * 应用分组id - */ - @Column(name = "group_id") - private Long groupId; - /** * SSO 发起登录类型 */ diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/AppGroupAssociationEntity.java b/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/AppGroupAssociationEntity.java new file mode 100644 index 00000000..92592bb1 --- /dev/null +++ b/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/AppGroupAssociationEntity.java @@ -0,0 +1,63 @@ +/* + * 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 + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package cn.topiam.employee.common.entity.app; + +import org.hibernate.annotations.SQLDelete; +import org.hibernate.annotations.Where; + +import cn.topiam.employee.support.repository.domain.LogicDeleteEntity; + +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import lombok.experimental.Accessors; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; +import static cn.topiam.employee.support.repository.domain.LogicDeleteEntity.SOFT_DELETE_SET; +import static cn.topiam.employee.support.repository.domain.LogicDeleteEntity.SOFT_DELETE_WHERE; + +/** + * 应用组关联 + * + * @author TopIAM + * Created by support@topiam.cn on 2023年09月06日22:03:21 + */ +@Getter +@Setter +@ToString +@Accessors(chain = true) +@Entity +@Table(name = "app_group_association") +@SQLDelete(sql = "update app_group_association set " + SOFT_DELETE_SET + " where id_ = ?") +@Where(clause = SOFT_DELETE_WHERE) +public class AppGroupAssociationEntity extends LogicDeleteEntity { + + /** + * 应用组ID + */ + @Column(name = "group_id") + private Long groupId; + + /** + * 应用ID + */ + @Column(name = "app_id") + private Long appId; +} diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/query/AppGroupAssociationListQuery.java b/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/query/AppGroupAssociationListQuery.java new file mode 100644 index 00000000..708a9559 --- /dev/null +++ b/eiam-common/src/main/java/cn/topiam/employee/common/entity/app/query/AppGroupAssociationListQuery.java @@ -0,0 +1,55 @@ +/* + * 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 + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package cn.topiam.employee.common.entity.app.query; + +import java.io.Serial; +import java.io.Serializable; + +import org.springdoc.core.annotations.ParameterObject; + +import lombok.Data; + +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotEmpty; + +/** + * 查询应用列表入参 + * + * @author TopIAM + * Created by support@topiam.cn on 2020/8/11 23:08 + */ +@Data +@Schema(description = "查询应用组应用列表入参") +@ParameterObject +public class AppGroupAssociationListQuery implements Serializable { + @Serial + private static final long serialVersionUID = -7110595216804896858L; + /** + * 组ID + */ + @NotEmpty(message = "组ID不能为空") + @Parameter(description = "组ID") + private String id; + + /** + * 应用名称 + */ + @Parameter(description = "应用名称") + private String appName; +} 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 new file mode 100644 index 00000000..652acc26 --- /dev/null +++ b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppGroupAssociationRepository.java @@ -0,0 +1,54 @@ +/* + * 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 + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package cn.topiam.employee.common.repository.app; + +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.querydsl.QuerydslPredicateExecutor; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import cn.topiam.employee.common.entity.app.AppGroupAssociationEntity; +import cn.topiam.employee.support.repository.LogicDeleteRepository; +import static cn.topiam.employee.support.repository.domain.LogicDeleteEntity.SOFT_DELETE_SET; + +/** + * 应用组成员 + * + * @author TopIAM + * Created by support@topiam.cn on 2023年09月06日22:03:18 + */ +@Repository +public interface AppGroupAssociationRepository extends + LogicDeleteRepository, + QuerydslPredicateExecutor, + AppGroupAssociationRepositoryCustomized { + + /** + * 根据应用组ID和应用ID删除 + * + * @param groupId {@link String} + * @param appId {@link String} + */ + @Modifying + @Transactional(rollbackFor = Exception.class) + @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); +} diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppGroupAssociationRepositoryCustomized.java b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppGroupAssociationRepositoryCustomized.java new file mode 100644 index 00000000..79e16734 --- /dev/null +++ b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/AppGroupAssociationRepositoryCustomized.java @@ -0,0 +1,41 @@ +/* + * 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 + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package cn.topiam.employee.common.repository.app; + +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.AppGroupAssociationListQuery; + +/** + * @author TopIAM + * Created by support@topiam.cn on 2023/9/7 21:27 + */ +public interface AppGroupAssociationRepositoryCustomized { + + /** + * 获取应用组应用列表 + * + * @param query {@link AppGroupAssociationListQuery} + * @param pageable {@link Pageable} + * @return {@link Page} + */ + Page getAppGroupAssociationList(AppGroupAssociationListQuery query, + Pageable pageable); +} diff --git a/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/impl/AppGroupAssociationRepositoryCustomizedImpl.java b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/impl/AppGroupAssociationRepositoryCustomizedImpl.java new file mode 100644 index 00000000..dd6e76ee --- /dev/null +++ b/eiam-common/src/main/java/cn/topiam/employee/common/repository/app/impl/AppGroupAssociationRepositoryCustomizedImpl.java @@ -0,0 +1,84 @@ +/* + * 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 + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package cn.topiam.employee.common.repository.app.impl; + +import java.util.List; + +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.stereotype.Repository; + +import cn.topiam.employee.common.entity.account.query.UserGroupMemberListQuery; +import cn.topiam.employee.common.entity.app.AppEntity; +import cn.topiam.employee.common.entity.app.query.AppGroupAssociationListQuery; +import cn.topiam.employee.common.repository.app.AppGroupAssociationRepositoryCustomized; +import cn.topiam.employee.common.repository.app.impl.mapper.AppEntityMapper; + +import lombok.AllArgsConstructor; + +/** + * @author TopIAM + * Created by support@topiam.cn on 2023/9/7 21:27 + */ +@Repository +@AllArgsConstructor +public class AppGroupAssociationRepositoryCustomizedImpl implements + AppGroupAssociationRepositoryCustomized { + + /** + * 获取应用组应用列表 + * + * @param query {@link UserGroupMemberListQuery} + * @param pageable {@link Pageable} + * @return {@link Page} + */ + @SuppressWarnings("DuplicatedCode") + @Override + public Page getAppGroupAssociationList(AppGroupAssociationListQuery query, + Pageable pageable) { + //@formatter:off + StringBuilder builder = new StringBuilder(""" + SELECT + app.* + FROM + app app LEFT JOIN app_group_association ass ON app.id_ = ass.app_id AND app.is_deleted = 0 AND ass.is_deleted = 0 + WHERE ass.group_id = '%s' + """.formatted(query.getId())); + //应用名称 + if (StringUtils.isNoneBlank(query.getAppName())) { + builder.append(" AND app.name_ like '%").append(query.getAppName()).append("%'"); + } + builder.append(" ORDER BY `app`.create_time DESC"); + //@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_"; + //@formatter:on + Integer count = jdbcTemplate.queryForObject(countSql, Integer.class); + return new PageImpl<>(list, pageable, count); + } + + private final JdbcTemplate jdbcTemplate; +} 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 d9e18692..d356daf2 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 @@ -79,6 +79,7 @@ public class AppRepositoryCustomizedImpl implements AppRepositoryCustomized { FROM app LEFT JOIN app_access_policy app_acce ON app.id_ = app_acce.app_id AND app_acce.is_deleted = '0' + LEFT JOIN app_group_association ass ON app.id_ = ass.app_id AND ass.is_deleted = '0' WHERE app.is_enabled = 1 AND app.is_deleted = '0' @@ -90,7 +91,7 @@ public class AppRepositoryCustomizedImpl implements AppRepositoryCustomized { } //分组id if (null!=groupId) { - builder.append(" AND app.group_id = ").append(groupId); + builder.append(" AND ass.group_id = ").append(groupId); } //@formatter:on String sql = builder.toString(); diff --git a/eiam-common/src/main/resources/db/1.1.0-changelog.xml b/eiam-common/src/main/resources/db/1.1.0-changelog.xml index ceaf433a..3bf2e8ce 100644 --- a/eiam-common/src/main/resources/db/1.1.0-changelog.xml +++ b/eiam-common/src/main/resources/db/1.1.0-changelog.xml @@ -53,12 +53,33 @@ - - + + + + - + + + + + + + + + + + + + + + + + + + + @@ -196,5 +217,10 @@ + + + + + \ No newline at end of file diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/controller/app/AppGroupController.java b/eiam-console/src/main/java/cn/topiam/employee/console/controller/app/AppGroupController.java index 26793ec3..0f635ae4 100644 --- a/eiam-console/src/main/java/cn/topiam/employee/console/controller/app/AppGroupController.java +++ b/eiam-console/src/main/java/cn/topiam/employee/console/controller/app/AppGroupController.java @@ -22,11 +22,16 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import com.google.common.collect.Lists; + import cn.topiam.employee.audit.annotation.Audit; import cn.topiam.employee.audit.event.type.EventType; +import cn.topiam.employee.common.entity.account.query.UserGroupMemberListQuery; +import cn.topiam.employee.common.entity.app.query.AppGroupAssociationListQuery; import cn.topiam.employee.console.pojo.query.app.AppGroupQuery; import cn.topiam.employee.console.pojo.result.app.AppGroupGetResult; import cn.topiam.employee.console.pojo.result.app.AppGroupListResult; +import cn.topiam.employee.console.pojo.result.app.AppListResult; import cn.topiam.employee.console.pojo.save.app.AppGroupCreateParam; import cn.topiam.employee.console.pojo.update.app.AppGroupUpdateParam; import cn.topiam.employee.console.service.app.AppGroupService; @@ -39,7 +44,9 @@ import cn.topiam.employee.support.result.ApiRestResult; import lombok.AllArgsConstructor; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.constraints.NotEmpty; import static cn.topiam.employee.common.constant.AppConstants.APP_PATH; /** @@ -169,6 +176,58 @@ public class AppGroupController { return ApiRestResult. builder().result(result).build(); } + /** + * 添加分组用户 + * + * @param appIds {@link String} + * @return {@link Boolean} + */ + @Lock + @Preview + @Validated + @Operation(summary = "添加应用组关联") + @Audit(type = EventType.ADD_APP_GROUP_ASSOCIATION) + @PostMapping(value = "/add_association/{id}") + @PreAuthorize(value = "authenticated and @sae.hasAuthority(T(cn.topiam.employee.support.security.userdetails.UserType).ADMIN)") + public ApiRestResult addAssociation(@PathVariable(value = "id") String id, + @Parameter(description = "应用ID") String[] appIds) { + return ApiRestResult. builder().result(appGroupService.addAssociation(id, appIds)) + .build(); + } + + /** + * 移除分组用户 + * + * @param id {@link String} + * @return {@link Boolean} + */ + @Lock + @Preview + @Operation(summary = "移除应用组关联") + @Audit(type = EventType.REMOVE_APP_GROUP_ASSOCIATION) + @DeleteMapping(value = "/remove_association/{id}") + @PreAuthorize(value = "authenticated and @sae.hasAuthority(T(cn.topiam.employee.support.security.userdetails.UserType).ADMIN)") + public ApiRestResult removeAssociation(@PathVariable(value = "id") String id, + @NotEmpty(message = "应用ID不能为空") @Parameter(description = "应用ID集合") String[] appIds) { + return ApiRestResult. builder() + .result(appGroupService.batchRemoveAssociation(id, Lists.newArrayList(appIds))).build(); + } + + /** + * 获取应用组内应用 + * + * @param query {@link UserGroupMemberListQuery} 参数 + * @return {@link Boolean} + */ + @Operation(summary = "获取应用组内应用") + @GetMapping(value = "/{id}/app_list") + @PreAuthorize(value = "authenticated and @sae.hasAuthority(T(cn.topiam.employee.support.security.userdetails.UserType).ADMIN)") + public ApiRestResult> getAppGroupAssociationList(PageModel model, + AppGroupAssociationListQuery query) { + return ApiRestResult.> builder() + .result(appGroupService.getAppGroupAssociationList(model, query)).build(); + } + /** * AppGroupService */ diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/service/app/AppGroupService.java b/eiam-console/src/main/java/cn/topiam/employee/console/service/app/AppGroupService.java index 76ef4aca..27c268cb 100644 --- a/eiam-console/src/main/java/cn/topiam/employee/console/service/app/AppGroupService.java +++ b/eiam-console/src/main/java/cn/topiam/employee/console/service/app/AppGroupService.java @@ -17,9 +17,13 @@ */ package cn.topiam.employee.console.service.app; +import java.util.List; + +import cn.topiam.employee.common.entity.app.query.AppGroupAssociationListQuery; import cn.topiam.employee.console.pojo.query.app.AppGroupQuery; import cn.topiam.employee.console.pojo.result.app.AppGroupGetResult; import cn.topiam.employee.console.pojo.result.app.AppGroupListResult; +import cn.topiam.employee.console.pojo.result.app.AppListResult; import cn.topiam.employee.console.pojo.save.app.AppGroupCreateParam; import cn.topiam.employee.console.pojo.update.app.AppGroupUpdateParam; import cn.topiam.employee.support.repository.page.domain.Page; @@ -91,4 +95,32 @@ public interface AppGroupService { * @return {@link Boolean} */ Boolean disableAppGroup(String id); + + /** + * 添加应用 + * + * @param appIds {@link String} + * @param groupId {@link String} + * @return {@link Boolean} + */ + Boolean addAssociation(String groupId, String[] appIds); + + /** + * 批量移除应用 + * + * @param id {@link String} + * @param appIds {@link String} + * @return {@link Boolean} + */ + Boolean batchRemoveAssociation(String id, List appIds); + + /** + * 获取应用组内应用列表 + * + * @param query {@link AppGroupAssociationListQuery} + * @param page {@link PageModel} + * @return {@link AppListResult} + */ + Page getAppGroupAssociationList(PageModel page, + AppGroupAssociationListQuery query); } diff --git a/eiam-console/src/main/java/cn/topiam/employee/console/service/app/impl/AppGroupServiceImpl.java b/eiam-console/src/main/java/cn/topiam/employee/console/service/app/impl/AppGroupServiceImpl.java index d9d5f2dc..6c71d341 100644 --- a/eiam-console/src/main/java/cn/topiam/employee/console/service/app/impl/AppGroupServiceImpl.java +++ b/eiam-console/src/main/java/cn/topiam/employee/console/service/app/impl/AppGroupServiceImpl.java @@ -18,25 +18,36 @@ package cn.topiam.employee.console.service.app.impl; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Optional; +import org.springframework.data.domain.PageRequest; import org.springframework.data.querydsl.QPageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.google.common.collect.Lists; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Predicate; 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.AppGroupAssociationEntity; import cn.topiam.employee.common.entity.app.AppGroupEntity; import cn.topiam.employee.common.entity.app.QAppGroupEntity; +import cn.topiam.employee.common.entity.app.query.AppGroupAssociationListQuery; +import cn.topiam.employee.common.repository.app.AppGroupAssociationRepository; import cn.topiam.employee.common.repository.app.AppGroupRepository; +import cn.topiam.employee.console.converter.app.AppConverter; import cn.topiam.employee.console.converter.app.AppGroupConverter; import cn.topiam.employee.console.pojo.query.app.AppGroupQuery; import cn.topiam.employee.console.pojo.result.app.AppGroupGetResult; import cn.topiam.employee.console.pojo.result.app.AppGroupListResult; +import cn.topiam.employee.console.pojo.result.app.AppListResult; import cn.topiam.employee.console.pojo.save.app.AppGroupCreateParam; import cn.topiam.employee.console.pojo.update.app.AppGroupUpdateParam; import cn.topiam.employee.console.service.app.AppGroupService; @@ -93,7 +104,6 @@ public class AppGroupServiceImpl implements AppGroupService { public Boolean createAppGroup(AppGroupCreateParam param) { // TODO 创建后没有数据权限 AppGroupEntity entity = appGroupConverter.appGroupCreateParamConvertToEntity(param); - entity.setEnabled(true); appGroupRepository.save(entity); AuditContext.setTarget( Target.builder().id(String.valueOf(entity.getId())).type(TargetType.APP_GROUP).build()); @@ -194,13 +204,92 @@ public class AppGroupServiceImpl implements AppGroupService { return optional.get(); } + /** + * 添加应用 + * + * @param appIds {@link String} + * @param groupId {@link String} + * @return {@link Boolean} + */ + @Override + public Boolean addAssociation(String groupId, String[] appIds) { + Optional optional = appGroupRepository.findById(Long.valueOf(groupId)); + //用户组不存在 + if (optional.isEmpty()) { + AuditContext.setContent("操作失败,应用组不存在"); + log.warn(AuditContext.getContent()); + throw new TopIamException(AuditContext.getContent()); + } + List list = new ArrayList<>(); + Lists.newArrayList(appIds).forEach(id -> { + AppGroupAssociationEntity member = new AppGroupAssociationEntity(); + member.setGroupId(Long.valueOf(groupId)); + member.setAppId(Long.valueOf(id)); + list.add(member); + }); + //添加 + appGroupAssociationRepository.saveAll(list); + List targets = new ArrayList<>(Arrays.stream(appIds) + .map(i -> Target.builder().id(i).type(TargetType.APPLICATION).build()).toList()); + targets.add(Target.builder().id(groupId).type(TargetType.APP_GROUP).build()); + AuditContext.setTarget(targets); + return true; + } + + /** + * 批量移除应用 + * + * @param appIds {@link String} + * @param id {@link String} + * @return {@link Boolean} + */ + @Override + public Boolean batchRemoveAssociation(String id, List appIds) { + Optional optional = appGroupRepository.findById(Long.valueOf(id)); + //用户组不存在 + if (optional.isEmpty()) { + AuditContext.setContent("操作失败,应用组不存在"); + log.warn(AuditContext.getContent()); + throw new TopIamException(AuditContext.getContent()); + } + appIds.forEach(userId -> appGroupAssociationRepository + .deleteByGroupIdAndAppId(Long.valueOf(id), Long.valueOf(userId))); + List targets = new ArrayList<>(appIds.stream() + .map(i -> Target.builder().id(i).type(TargetType.APPLICATION).build()).toList()); + targets.add(Target.builder().id(id).type(TargetType.APP_GROUP).build()); + AuditContext.setTarget(targets); + return true; + } + + /** + * 获取应用组内应用列表 + * + * @param query {@link AppGroupAssociationListQuery} + * @return {@link AppListResult} + */ + @Override + public Page getAppGroupAssociationList(PageModel model, + AppGroupAssociationListQuery query) { + org.springframework.data.domain.Page page = appGroupAssociationRepository + .getAppGroupAssociationList(query, + PageRequest.of(model.getCurrent(), model.getPageSize())); + return appConverter.entityConvertToAppListResult(page); + } + /** * AppGroupRepository */ - private final AppGroupRepository appGroupRepository; + private final AppGroupRepository appGroupRepository; /** * AppGroupConverter */ - private final AppGroupConverter appGroupConverter; + private final AppGroupConverter appGroupConverter; + + private final AppConverter appConverter; + + /** + * AppGroupAssociationRepository + */ + private final AppGroupAssociationRepository appGroupAssociationRepository; } diff --git a/eiam-portal/src/main/java/cn/topiam/employee/portal/converter/AppGroupConverter.java b/eiam-portal/src/main/java/cn/topiam/employee/portal/converter/AppGroupConverter.java index af2795d8..ab6c80f0 100644 --- a/eiam-portal/src/main/java/cn/topiam/employee/portal/converter/AppGroupConverter.java +++ b/eiam-portal/src/main/java/cn/topiam/employee/portal/converter/AppGroupConverter.java @@ -25,8 +25,9 @@ import org.mapstruct.Mapper; import com.querydsl.core.types.ExpressionUtils; import com.querydsl.core.types.Predicate; -import cn.topiam.employee.common.entity.app.AppEntity; +import cn.topiam.employee.common.entity.app.AppGroupAssociationEntity; import cn.topiam.employee.common.entity.app.AppGroupEntity; +import cn.topiam.employee.common.entity.app.QAppGroupAssociationEntity; import cn.topiam.employee.common.entity.app.QAppGroupEntity; import cn.topiam.employee.portal.pojo.result.AppGroupListResult; @@ -53,20 +54,32 @@ public interface AppGroupConverter { return predicate; } + /** + * 应用组与应用关联 Predicate + * + * @return {@link Predicate} + */ + default Predicate queryAppGroupAssociationPredicate() { + QAppGroupAssociationEntity appGroupAssociation = QAppGroupAssociationEntity.appGroupAssociationEntity; + Predicate predicate = appGroupAssociation.deleted.eq(Boolean.FALSE); + //@formatter:on + return predicate; + } + /** * 实体转分组管理列表 * - * @param list {@link AppGroupEntity} - * @param appList {@link AppEntity} + * @param list {@link AppGroupEntity} + * @param appGroupAssociationList {@link AppGroupAssociationEntity} * @return {@link AppGroupListResult} */ default List entityConvertToAppGroupListResult(List list, - List appList) { + List appGroupAssociationList) { List results = new ArrayList<>(); for (AppGroupEntity entity : list) { AppGroupListResult result = appGroupEntityConverterToResult(entity); - Long count = appList.stream().filter(t -> t.getGroupId().equals(entity.getId())) - .count(); + Long count = appGroupAssociationList.stream() + .filter(t -> t.getGroupId().equals(entity.getId())).count(); result.setAppCount(Integer.valueOf(count.toString())); results.add(result); } diff --git a/eiam-portal/src/main/java/cn/topiam/employee/portal/service/impl/AppServiceImpl.java b/eiam-portal/src/main/java/cn/topiam/employee/portal/service/impl/AppServiceImpl.java index 97ab224e..ee166d24 100644 --- a/eiam-portal/src/main/java/cn/topiam/employee/portal/service/impl/AppServiceImpl.java +++ b/eiam-portal/src/main/java/cn/topiam/employee/portal/service/impl/AppServiceImpl.java @@ -25,7 +25,9 @@ import org.springframework.stereotype.Service; import com.querydsl.core.types.Predicate; import cn.topiam.employee.common.entity.app.AppEntity; +import cn.topiam.employee.common.entity.app.AppGroupAssociationEntity; import cn.topiam.employee.common.entity.app.AppGroupEntity; +import cn.topiam.employee.common.repository.app.AppGroupAssociationRepository; import cn.topiam.employee.common.repository.app.AppGroupRepository; import cn.topiam.employee.common.repository.app.AppRepository; import cn.topiam.employee.portal.converter.AppConverter; @@ -70,36 +72,46 @@ public class AppServiceImpl implements AppService { @Override public List getAppGroupList() { Predicate predicate = appGroupConverter.queryPredicate(); - List appList = appRepository.getAppListByGroup(); + Predicate appGroupAssociationPredicate = appGroupConverter + .queryAppGroupAssociationPredicate(); + List appGroupAssociationList = (List) appGroupAssociationRepository + .findAll(appGroupAssociationPredicate); //查询映射 List list = (List) appGroupRepository.findAll(predicate); - return appGroupConverter.entityConvertToAppGroupListResult(list, appList); + return appGroupConverter.entityConvertToAppGroupListResult(list, appGroupAssociationList); } /** * AppRepository */ - private final AppRepository appRepository; + private final AppRepository appRepository; /** * AppGroupRepository */ - private final AppGroupRepository appGroupRepository; + private final AppGroupRepository appGroupRepository; + + /** + * AppGroupAssociationRepository + */ + private final AppGroupAssociationRepository appGroupAssociationRepository; /** * AppConverter */ - private final AppConverter appConverter; + private final AppConverter appConverter; /** * AppGroupConverter */ - private final AppGroupConverter appGroupConverter; + private final AppGroupConverter appGroupConverter; public AppServiceImpl(AppRepository appRepository, AppGroupRepository appGroupRepository, + AppGroupAssociationRepository appGroupAssociationRepository, AppConverter appConverter, AppGroupConverter appGroupConverter) { this.appRepository = appRepository; this.appGroupRepository = appGroupRepository; + this.appGroupAssociationRepository = appGroupAssociationRepository; this.appConverter = appConverter; this.appGroupConverter = appGroupConverter; }