Skip to content

Commit

Permalink
feat(权限管理): 分页获取用户详情,新增组织信息 (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
PengyuDeng authored Sep 2, 2024
1 parent 594b293 commit 412fbd6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -118,6 +124,11 @@ public UserDetail withDimension(List<Dimension> details) {
return this;
}

public UserDetail withType() {
this.setType(UserEntityTypes.getType(this.getTypeId()));
return this;
}

public UserEntity toUserEntity() {
UserEntity userEntity = new UserEntity();
userEntity.setId(id);
Expand All @@ -131,7 +142,7 @@ public UserEntity toUserEntity() {
return userEntity;
}

public UserDetailEntity toDetailEntity(){
public UserDetailEntity toDetailEntity() {
return FastBeanCopier.copy(this, new UserDetailEntity());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
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;
Expand All @@ -20,13 +22,14 @@
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;

/**
* 用户详情管理
Expand All @@ -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()));

Expand Down Expand Up @@ -112,33 +120,41 @@ public Mono<Void> saveUserDetail(String userId, SaveUserDetailRequest request) {
}

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())
);
}

/**
Expand Down

0 comments on commit 412fbd6

Please sign in to comment.