This commit is contained in:
2024-04-23 23:32:41 +08:00
parent cc7ac91409
commit 677e8e92fd
7 changed files with 112 additions and 37 deletions

View File

@@ -25,7 +25,9 @@ public class JpaUtils {
public static Function<String, String> underscoreToCamelCase =
(String name) -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name);
public static List<Map> convertTuplesToRawMap(List<Tuple> tuples) {
return convertTuplesToMap(tuples, Function.identity(), Function.identity()).stream().map(x -> (Map) x).collect(Collectors.toList());
}
public static List<Map<String, Object>> convertTuplesToMap(List<Tuple> tuples) {
return convertTuplesToMap(tuples, Function.identity(), Function.identity());
}

View File

@@ -9,6 +9,7 @@ import cn.lihongjie.coal.organization.dto.OrganizationDto;
import cn.lihongjie.coal.organization.dto.ResetAdminPasswordRequest;
import cn.lihongjie.coal.organization.dto.UpdateOrganizationDto;
import cn.lihongjie.coal.organization.service.OrganizationService;
import cn.lihongjie.coal.user.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
@@ -48,6 +49,15 @@ public class OrganizationController extends BaseController {
return this.service.list(dto);
}
@PostMapping("/userList")
public Page<UserDto> userList(@RequestBody CommonQuery dto) {
return this.service.userList(dto);
}
@PostMapping("/resetOrgAdminPassword")
public Boolean resetOrgAdminPassword(@RequestBody ResetAdminPasswordRequest dto) {
this.service.resetOrgAdminPassword(dto);

View File

@@ -1,12 +1,15 @@
package cn.lihongjie.coal.organization.dto;
import cn.lihongjie.coal.base.dto.CommonDto;
import cn.lihongjie.coal.base.dto.SimpleDto;
import cn.lihongjie.coal.permission.dto.PermissionSimpleDto;
import lombok.Data;
import org.hibernate.annotations.Comment;
import java.time.LocalDateTime;
import java.util.List;
@Data
public class OrganizationDto extends CommonDto {
@@ -17,7 +20,14 @@ public class OrganizationDto extends CommonDto {
private Integer maxUser;
private List<PermissionSimpleDto> permissions;
@Comment("过期时间")
private LocalDateTime expireTime;
private Integer userCount;
private SimpleDto adminUser;
}

View File

@@ -6,5 +6,6 @@ import lombok.Data;
public class ResetAdminPasswordRequest{
String id;
String password;
String newPassword;
String newPassword2;
}

View File

@@ -2,6 +2,7 @@ package cn.lihongjie.coal.organization.entity;
import cn.lihongjie.coal.base.entity.CommonEntity;
import cn.lihongjie.coal.permission.entity.PermissionEntity;
import cn.lihongjie.coal.user.entity.UserEntity;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@@ -45,5 +46,9 @@ public class OrganizationEntity extends CommonEntity {
private List<PermissionEntity> permissions;
@OneToOne
private UserEntity adminUser;
}

View File

@@ -4,6 +4,8 @@ import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.coalParameterDef.service.CoalParameterDefService;
import cn.lihongjie.coal.common.JpaUtils;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.organization.dto.CreateOrganizationDto;
import cn.lihongjie.coal.organization.dto.OrganizationDto;
import cn.lihongjie.coal.organization.dto.ResetAdminPasswordRequest;
@@ -13,11 +15,16 @@ import cn.lihongjie.coal.organization.mapper.OrganizationMapper;
import cn.lihongjie.coal.organization.repository.OrganizationRepository;
import cn.lihongjie.coal.rabbitmq.RabbitMQService;
import cn.lihongjie.coal.user.dto.CreateOrgAdminDto;
import cn.lihongjie.coal.user.dto.UserDto;
import cn.lihongjie.coal.user.entity.UserEntity;
import cn.lihongjie.coal.user.service.UserService;
import io.vavr.collection.Stream;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.Tuple;
import lombok.extern.slf4j.Slf4j;
@@ -58,21 +65,7 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
@Autowired RabbitTemplate rabbitTemplate;
public OrganizationDto create(CreateOrganizationDto request) {
OrganizationEntity entity = mapper.toEntity(request);
this.repository.save(entity);
CreateOrgAdminDto dto = new CreateOrgAdminDto();
dto.setOrganizationId(entity.getId());
dto.setUsername(request.getOrgAdminUserName());
dto.setPassword(request.getOrgAdminPassword());
userService.createOrgAdmin(dto);
rabbitMQService.sendToSysExchange(
"organization.create", entity.getId(), new HashMap<String, String>());
return getById(entity.getId());
}
@PersistenceContext EntityManager em;
public OrganizationDto update(UpdateOrganizationDto request) {
OrganizationEntity entity = this.repository.get(request.getId());
@@ -84,9 +77,27 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
return getById(entity.getId());
}
public void resetOrgAdminPassword(ResetAdminPasswordRequest resetAdminPasswordRequest) {
userService.resetOrgAdminPassword(
resetAdminPasswordRequest.getId(), resetAdminPasswordRequest.getPassword());
public OrganizationDto create(CreateOrganizationDto request) {
OrganizationEntity entity = mapper.toEntity(request);
this.repository.save(entity);
CreateOrgAdminDto dto = new CreateOrgAdminDto();
dto.setOrganizationId(entity.getId());
dto.setUsername(request.getOrgAdminUserName());
dto.setPassword(request.getOrgAdminPassword());
UserDto orgAdmin = userService.createOrgAdmin(dto);
entity.setAdminUser(em.getReference(UserEntity.class, orgAdmin.getId()));
this.repository.save(entity);
rabbitMQService.sendToSysExchange(
"organization.create", entity.getId(), new HashMap<String, String>());
return getById(entity.getId());
}
public void delete(IdRequest request) {
@@ -101,6 +112,16 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
return mapper.toDto(entity);
}
public void resetOrgAdminPassword(ResetAdminPasswordRequest request) {
if (!StringUtils.equals(request.getNewPassword(), request.getNewPassword2())){
throw new BizException("两次密码不一致");
}
userService.resetOrgAdminPassword(
request.getId(), request.getNewPassword());
}
public Page<OrganizationDto> list(CommonQuery query) {
Page<OrganizationEntity> page =
@@ -111,7 +132,21 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
query.getPageSize(),
Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
Page<OrganizationDto> map = page.map(this.mapper::toDto);
List<Tuple> counts = em.createQuery(
"select u.organizationId as id , count(1) as userCount from UserEntity u where u.organizationId in :ids group by u.organizationId",
Tuple.class)
.setParameter("ids", page.getContent().stream().map(OrganizationEntity::getId).toList())
.getResultList();
var maps = JpaUtils.convertTuplesToRawMap(counts);
JpaUtils.mergeMapToPojo(
map.getContent(), maps, conversionService);
return map;
}
public OrganizationEntity initOrGetAdminOrg() {
@@ -149,4 +184,9 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
return javaMap;
}
public Page<UserDto> userList(CommonQuery dto) {
return userService.list(dto);
}
}

View File

@@ -42,6 +42,7 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
@@ -149,7 +150,7 @@ public class UserService extends BaseService<UserEntity, UserRepository> {
private void checkDuplicateUserName(String username) {
if (this.repository.countByUsername(username) > 0) {
throw new BizException("用户名以及存在");
throw new BizException("用户名已经存在");
}
}
@@ -314,17 +315,20 @@ public class UserService extends BaseService<UserEntity, UserRepository> {
.map(x -> resourceMapper.toDto(x))
.collect(Collectors.toList());
}
OrganizationEntity organization = organizationService.get(user.getOrganizationId());
return Stream.ofAll(user.allRoles())
.flatMap(x -> x.getPermissions() == null ? Stream.empty() : x.getPermissions())
.flatMap(x -> x.getResources() == null ? Stream.empty() : x.getResources())
.appendAll(
Stream.ofAll(permissionService.getByTypes(new String[] {"0", "1"}))
.flatMap(PermissionEntity::getResources))
.appendAll(
Stream.ofAll(
BooleanUtils.isTrue(user.getOrgAdmin())
? permissionService.getByTypes(
new String[] {"0", "1", "2"})
: permissionService.getByTypes(
new String[] {"0", "1"}))
? (CollectionUtils.isEmpty(
organization.getPermissions()) ? permissionService.getByTypes(new String[] {"0", "1", "2"}) : organization.getPermissions())
: Collections.emptyList())
.flatMap(PermissionEntity::getResources))
.distinctBy(BaseEntity::getId)
.map(x -> resourceMapper.toDto(x))
@@ -344,13 +348,16 @@ public class UserService extends BaseService<UserEntity, UserRepository> {
.collect(Collectors.toList());
}
OrganizationEntity organization = organizationService.get(user.getOrganizationId());
return Stream.ofAll(user.allRoles())
.flatMap(x -> x.getPermissions() == null ? Stream.empty() : x.getPermissions())
.appendAll(permissionService.getByTypes(new String[] {"0", "1"}))
.appendAll(
Stream.ofAll(
BooleanUtils.isTrue(user.getOrgAdmin())
? permissionService.getByTypes(new String[] {"0", "1", "2"})
: permissionService.getByTypes(new String[] {"0", "1"})))
? ObjectUtils.defaultIfNull(
organization.getPermissions(), new ArrayList<>())
: Collections.emptyList()))
.distinctBy(BaseEntity::getId)
.map(x -> permissionMapper.toSimpleDto(x))
.collect(Collectors.toList());
@@ -421,7 +428,7 @@ public class UserService extends BaseService<UserEntity, UserRepository> {
return javaMap;
} catch (Exception e) {
log.error("获取用户名称失败 {}", StringUtils.join(names, " | "), e);
log.error("获取用户名称失败 {}", StringUtils.join(names, " | "), e);
return Map.of();
}
}
@@ -430,15 +437,15 @@ public class UserService extends BaseService<UserEntity, UserRepository> {
public void resetOrgAdminPassword(String organizationId, String password) {
em.createQuery("from UserEntity where organizationId = :organizationId and orgAdmin = true", UserEntity.class)
em.createQuery(
"from UserEntity where organizationId = :organizationId and orgAdmin = true",
UserEntity.class)
.setParameter("organizationId", organizationId)
.getResultList()
.forEach(x -> {
x.setPassword(passwordEncoder.encode(password));
repository.save(x);
});
.forEach(
x -> {
x.setPassword(passwordEncoder.encode(password));
repository.save(x);
});
}
}