refactor: 租户重构
This commit is contained in:
@@ -8,6 +8,7 @@ import com.youlai.boot.system.service.TenantService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -71,6 +72,7 @@ public class TenantController {
|
||||
* 切换租户
|
||||
* <p>
|
||||
* 切换当前用户的租户上下文,需要验证用户是否有权限访问该租户
|
||||
* 并记录审计日志
|
||||
* </p>
|
||||
*
|
||||
* @param tenantId 目标租户ID
|
||||
@@ -79,32 +81,49 @@ public class TenantController {
|
||||
@Operation(summary = "切换租户")
|
||||
@PostMapping("/switch/{tenantId}")
|
||||
public Result<TenantVO> switchTenant(
|
||||
@Parameter(description = "租户ID") @PathVariable Long tenantId
|
||||
@Parameter(description = "租户ID") @PathVariable Long tenantId,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
log.info("用户 {} 请求切换租户到 {}", userId, tenantId);
|
||||
Long fromTenantId = TenantContextHolder.getTenantId();
|
||||
|
||||
log.info("用户 {} 请求切换租户:{} -> {}", userId, fromTenantId, tenantId);
|
||||
|
||||
// 验证用户是否有权限访问该租户
|
||||
boolean hasPermission = tenantService.hasTenantPermission(userId, tenantId);
|
||||
if (!hasPermission) {
|
||||
log.warn("用户 {} 无权限访问租户 {}", userId, tenantId);
|
||||
return Result.failed("无权限访问该租户");
|
||||
try {
|
||||
// 验证用户是否有权限访问该租户
|
||||
boolean hasPermission = tenantService.hasTenantPermission(userId, tenantId);
|
||||
if (!hasPermission) {
|
||||
log.warn("用户 {} 无权限访问租户 {}", userId, tenantId);
|
||||
// 记录失败日志
|
||||
tenantService.recordTenantSwitch(userId, fromTenantId, tenantId, false, "无权限访问该租户", request);
|
||||
return Result.failed("无权限访问该租户");
|
||||
}
|
||||
|
||||
// 验证租户是否存在且正常
|
||||
TenantVO tenant = tenantService.getTenantById(tenantId);
|
||||
if (tenant == null) {
|
||||
tenantService.recordTenantSwitch(userId, fromTenantId, tenantId, false, "租户不存在", request);
|
||||
return Result.failed("租户不存在");
|
||||
}
|
||||
if (tenant.getStatus() == null || tenant.getStatus() != 1) {
|
||||
tenantService.recordTenantSwitch(userId, fromTenantId, tenantId, false, "租户已禁用", request);
|
||||
return Result.failed("租户已禁用");
|
||||
}
|
||||
|
||||
// 设置新的租户上下文
|
||||
TenantContextHolder.setTenantId(tenantId);
|
||||
|
||||
// 记录成功日志
|
||||
tenantService.recordTenantSwitch(userId, fromTenantId, tenantId, true, null, request);
|
||||
|
||||
log.info("用户 {} 成功切换租户到 {}", userId, tenantId);
|
||||
|
||||
return Result.success(tenant);
|
||||
} catch (Exception e) {
|
||||
log.error("用户 {} 切换租户失败", userId, e);
|
||||
tenantService.recordTenantSwitch(userId, fromTenantId, tenantId, false, e.getMessage(), request);
|
||||
return Result.failed("切换租户失败:" + e.getMessage());
|
||||
}
|
||||
|
||||
// 验证租户是否存在且正常
|
||||
TenantVO tenant = tenantService.getTenantById(tenantId);
|
||||
if (tenant == null) {
|
||||
return Result.failed("租户不存在");
|
||||
}
|
||||
if (tenant.getStatus() == null || tenant.getStatus() != 1) {
|
||||
return Result.failed("租户已禁用");
|
||||
}
|
||||
|
||||
// 设置新的租户上下文
|
||||
TenantContextHolder.setTenantId(tenantId);
|
||||
log.info("用户 {} 成功切换租户到 {}", userId, tenantId);
|
||||
|
||||
return Result.success(tenant);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user