feat(user): 添加用户当前登录信息获取功能并优化组织切换逻辑- 在 LoginController 中添加 currentUser 方法,用于获取当前登录用户信息- 在 LoginUserService 中实现清除活动组织的功能

- 在 OrganizationService 中增加检查组织循环引用的逻辑- 优化用户服务,为 UserDto 添加活动组织 ID 字段
This commit is contained in:
2025-03-17 22:51:23 +08:00
parent 0ef826800e
commit 8a6f2c5bcf
6 changed files with 72 additions and 12 deletions

View File

@@ -336,6 +336,7 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
if (entity == null) {
throw new BizException("sessionNotFound", "会话不存在");
}
entity.setActiveOrganizationId(request.getNewOrgId());
@@ -349,4 +350,23 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
this.clearCache(sessionId);
}
public void clearActiveOrg(String sessionId, SwitchActiveOrgRequest request) {
LoginUserEntity entity = this.get(sessionId);
if (entity == null) {
throw new BizException("sessionNotFound", "会话不存在");
}
entity.setActiveOrganizationId(null);
try {
this.repository.save(entity);
} catch (Exception e) {
log.warn("切换机构失败", e);
throw new BizException("切换机构失败");
}
this.clearCache(sessionId);
}
}

View File

@@ -26,6 +26,7 @@ import jakarta.persistence.Tuple;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
@@ -58,8 +59,7 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
@Autowired RabbitMQService rabbitMQService;
@Autowired
CacheManager cacheManager;
@Autowired CacheManager cacheManager;
public void init() {
@@ -99,12 +99,33 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
checkLoop(entity);
rabbitMQService.sendToSysExchange(
"organization.update", entity.getId(), new HashMap<String, String>());
reloadCache(request.getId());
return getById(entity.getId());
}
private void checkLoop(OrganizationEntity entity) {
if (entity.getParent() == null) {
return;
}
Set<String> visited = new HashSet<>();
while (entity.getParent() != null) {
if (visited.contains(entity.getId())) {
throw new BizException("机构不能有循环");
}
visited.add(entity.getId());
entity = entity.getParent();
}
}
public OrganizationDto create(CreateOrganizationDto request) {
OrganizationEntity entity = mapper.toEntity(request);
@@ -123,6 +144,8 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
this.repository.save(entity);
checkLoop(entity);
rabbitMQService.sendToSysExchange(
"organization.create", entity.getId(), new HashMap<String, String>());
return getById(entity.getId());
@@ -130,6 +153,16 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
public void delete(IdRequest request) {
for (String id : request.getIds()) {
OrganizationEntity organization = this.repository.get(id);
if (CollectionUtils.isNotEmpty(organization.getChildren())) {
throw new BizException("请先删除子机构");
}
}
this.repository.deleteAllById(request.getIds());
reloadCache();
@@ -223,22 +256,19 @@ public class OrganizationService extends BaseService<OrganizationEntity, Organiz
@Cacheable(cacheNames = Constants.CACHE_ORGANIZATION_PERMISSION_IDS, key = "#organizationId")
public Set<String> getDefaultPermissionIds(String organizationId) {
List<String> id = em.createQuery(
"select p.id from OrganizationEntity o join o.permissions p where o.id = :id",
String.class)
.setParameter("id", organizationId)
.getResultList();
return new HashSet<>(
id);
List<String> id =
em.createQuery(
"select p.id from OrganizationEntity o join o.permissions p where o.id = :id",
String.class)
.setParameter("id", organizationId)
.getResultList();
return new HashSet<>(id);
}
public OrganizationTreeDto orgTree() {
String s = Ctx.currentOrganizationId();
return this.mapper.toTreeDto(this.repository.get(s));
}
}

View File

@@ -50,6 +50,12 @@ public class LoginController extends BaseController {
}
@PostMapping("/currentUser")
public UserDto currentUser(){
return userService.getById(Ctx.getUserId());
}
@PostMapping("/trustDeviceBind")

View File

@@ -128,6 +128,7 @@ public class SessionService {
String organizationId = userEntity.getOrganizationId();
if (StringUtils.equals(request.getNewOrgId(), organizationId)){
loginUserService.clearActiveOrg(Ctx.getSessionId(), request);
return;
}

View File

@@ -35,6 +35,8 @@ public class UserDto extends OrgCommonDto {
private List<RoleDto> roles;
private List<RoleDto> otherRoles;
private String activeOrganizationId;
@Data

View File

@@ -244,6 +244,7 @@ public class UserService extends BaseService<UserEntity, UserRepository> {
UserDto dto = mapper.toDto(user);
if (Ctx.isLoggedIn()) {
dto.setSessionId(Ctx.getSessionId());
dto.setActiveOrganizationId(Ctx.activeOrganizationId());
}
return dto;