feat(权限管理): 分页获取用户详情,新增组织信息 (#560)

This commit is contained in:
PengyuDeng 2024-09-02 09:33:12 +08:00 committed by GitHub
parent 594b2937d5
commit 412fbd6e2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 30 deletions

View File

@ -36,6 +36,9 @@ public class UserDetail {
@Schema(hidden = true)
private UserEntityType type;
@Schema(description = "用户类型ID")
private String typeId;
@Schema(description = "用户状态。1启用0禁用")
private Byte status;
@ -63,6 +66,9 @@ public class UserDetail {
@Schema(description = "所在机构(部门)信息")
private List<OrganizationInfo> orgList;
@Schema(description = "创建者ID")
private String creatorId;
private boolean tenantDisabled;
public static UserDetail of(UserEntity entity) {
@ -118,6 +124,11 @@ public class UserDetail {
return this;
}
public UserDetail withType() {
this.setType(UserEntityTypes.getType(this.getTypeId()));
return this;
}
public UserEntity toUserEntity() {
UserEntity userEntity = new UserEntity();
userEntity.setId(id);
@ -131,7 +142,7 @@ public class UserDetail {
return userEntity;
}
public UserDetailEntity toDetailEntity(){
public UserDetailEntity toDetailEntity() {
return FastBeanCopier.copy(this, new UserDetailEntity());
}

View File

@ -3,8 +3,10 @@ package org.jetlinks.community.auth.service;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.ReactiveAuthenticationHolder;
import org.hswebframework.web.authorization.ReactiveAuthenticationManager;
import org.hswebframework.web.bean.FastBeanCopier;
import org.hswebframework.web.crud.query.QueryHelper;
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
import org.hswebframework.web.i18n.LocaleUtils;
import org.hswebframework.web.system.authorization.api.entity.UserEntity;
@ -20,13 +22,14 @@ import org.jetlinks.community.auth.service.request.SaveUserRequest;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* 用户详情管理
@ -44,20 +47,25 @@ public class UserDetailService extends GenericReactiveCrudService<UserDetailEnti
private final ReactiveUserService userService;
private final RoleService roleService;
private final OrganizationService organizationService;
private final ReactiveAuthenticationManager authenticationManager;
private final QueryHelper queryHelper;
private final static UserDetailEntity emptyDetail = new UserDetailEntity();
public UserDetailService(ReactiveUserService userService,
RoleService roleService,
OrganizationService organizationService,
ReactiveAuthenticationManager authenticationManager) {
ReactiveAuthenticationManager authenticationManager,
QueryHelper queryHelper) {
this.userService = userService;
this.roleService = roleService;
this.organizationService = organizationService;
this.authenticationManager = authenticationManager;
this.queryHelper = queryHelper;
// 注册默认用户类型
UserEntityTypes.register(Arrays.asList(DefaultUserEntityType.values()));
@ -112,33 +120,41 @@ public class UserDetailService extends GenericReactiveCrudService<UserDetailEnti
}
public Mono<PagerResult<UserDetail>> queryUserDetail(QueryParamEntity query) {
return Mono
.zip(
userService.countUser(query),
userService.findUser(query).collectList())
.flatMap(tp2 -> {
List<UserEntity> userList = tp2.getT2();
return this.createQuery()
.in(UserDetailEntity::getId, userList
.stream()
.map(UserEntity::getId)
.collect(Collectors.toList()))
.fetch()
.collectMap(UserDetailEntity::getId)
.flatMap(userDetailMap -> {
List<UserDetail> userDetailList = userList.stream()
.map(user -> {
UserDetail userDetail = UserDetail.of(user);
UserDetailEntity entity = userDetailMap.get(user.getId());
if (entity != null) {
userDetail = userDetail.with(entity);
}
return userDetail;
})
.collect(Collectors.toList());
return Mono.just(PagerResult.of(tp2.getT1(), userDetailList, query));
});
});
return QueryHelper
.transformPageResult(
queryHelper
.select(UserDetail.class)
.all(UserDetailEntity.class)
.as(UserEntity::getId, UserDetail::setId)
.as(UserEntity::getName, UserDetail::setName)
.as(UserEntity::getUsername, UserDetail::setUsername)
// 兼容之前已有字段
.as(UserEntity::getType, UserDetail::setTypeId)
.as(UserEntity::getStatus, UserDetail::setStatus)
.as(UserEntity::getCreateTime, UserDetail::setCreateTime)
.as(UserEntity::getCreatorId, UserDetail::setCreatorId)
.from(UserEntity.class)
.leftJoin(UserDetailEntity.class, j -> j.is(UserDetailEntity::getId, UserEntity::getId))
.where(query)
.fetchPaged(),
list -> this.fillUserDetail(list).
collectList());
}
private Flux<UserDetail> fillUserDetail(List<UserDetail> users) {
if (CollectionUtils.isEmpty(users)) {
return Flux.empty();
}
return Flux
.fromIterable(users)
.flatMap(detail ->
//维度信息
ReactiveAuthenticationHolder
.get(detail.getId())
.map(Authentication::getDimensions)
.defaultIfEmpty(Collections.emptyList())
.map(dimensions -> detail.withDimension(dimensions).withType())
);
}
/**