Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
*/
public interface LoginUserContext {
/**
* 返回当前用户所诉的业务租户信息
* @return 租户ID
* 返回当前用户所在的业务租户id
* @return 租户Id
*/
String getTenantId();

/**
* 返回当前用户所在的业务租户信息
* @return 租户
*/
List<Tenant> getTenants();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,16 @@ public enum ExceptionEnum implements IBaseError {
* Cm 337 exception enum.
*/
CM337("CM337", "组织不存在"),

/**
* Cm 338 exception enum.
*/
CM338("CM338", "用户不存在,请重新注册");
CM338("CM338", "用户不存在,请重新注册"),

/**
* Cm 339 exception enum.
*/
CM339("CM339", "token检验失败,请重新登录");

/**
* 错误码
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.tinyengine.it.model.dto.AiToken;
import com.tinyengine.it.model.dto.ChatRequest;

import com.tinyengine.it.rag.entity.EmbeddingMatchDto;
import com.tinyengine.it.rag.entity.SearchRequest;
import com.tinyengine.it.rag.service.StorageService;
import com.tinyengine.it.service.app.v1.AiChatV1Service;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -37,6 +40,8 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.util.List;

/**
* The type Ai chat controller.
*
Expand All @@ -53,6 +58,9 @@ public class AiChatController {
@Autowired
private AiChatV1Service aiChatV1Service;

@Autowired
private StorageService vectorStorageService;

/**
* AI api
*
Expand Down Expand Up @@ -151,4 +159,25 @@ public Result<AiToken> getToken(@RequestBody ChatRequest request) throws Excepti
String token = aiChatV1Service.getToken(apiKey);
return Result.success(new AiToken(token));
}

/**
* search in collection
*
* @param searchDto the searchDto
* @return result
*/
@Operation(summary = "在指定集合中搜索", description = "在指定集合中搜索",
parameters = {
@Parameter(name = "searchDto", description = "搜索请求参数体"),
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "AI search in collection")
@PostMapping("/ai/search")
public Result<List<EmbeddingMatchDto>> searchInCollection(@RequestBody SearchRequest searchDto) {
List<EmbeddingMatchDto> results = vectorStorageService.search(searchDto);
return Result.success(results);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

package com.tinyengine.it.login.config;

import com.tinyengine.it.common.exception.ExceptionEnum;
import com.tinyengine.it.common.exception.ServiceException;
import com.tinyengine.it.login.utils.JwtUtil;
import com.tinyengine.it.login.config.context.DefaultLoginUserContext;
import com.tinyengine.it.login.model.UserInfo;
Expand All @@ -35,30 +37,26 @@ public class SSOInterceptor implements HandlerInterceptor {
@Autowired
private JwtUtil jwtUtil;

private static final String SSO_SERVER = System.getenv("SSO_SERVER");

@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {

String token = request.getHeader("Authorization");
String authorization = request.getHeader("Authorization");
// 如果没有token,重定向到登录页
if (authorization == null || authorization.isEmpty()) {
log.info("No token");
throw new ServiceException(ExceptionEnum.CM336.getResultCode(), ExceptionEnum.CM336.getResultMsg());
}
String token = jwtUtil.getTokenFromRequest(authorization);
String requestURI = request.getRequestURI();

log.info("Intercepting: {}, Token: {}", requestURI, token != null ? "present" : "null");

// 如果没有token,重定向到登录页
if (token == null || token.isEmpty()) {
log.info("No token, redirecting to: {}", SSO_SERVER);
response.sendRedirect(SSO_SERVER);
return false;
}

try {
// 验证token
if (!jwtUtil.validateToken(token)) {
log.warn("Token validation failed");
response.sendRedirect(SSO_SERVER);
return false;
throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg());
}

// 从token中获取用户信息
Expand All @@ -72,8 +70,7 @@ public boolean preHandle(HttpServletRequest request,
// 检查必需的用户信息
if (username == null || username.isEmpty() || userId == null) {
log.warn("User information is incomplete - username: {}, userId: {}", username, userId);
response.sendRedirect(SSO_SERVER);
return false;
throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg());
}

// 存储用户信息到LoginUserContext
Expand All @@ -90,9 +87,8 @@ public boolean preHandle(HttpServletRequest request,

} catch (Exception e) {
log.error("Token validation exception: {}", e.getMessage(), e);
response.sendRedirect(SSO_SERVER);
DefaultLoginUserContext.clear();
return false;
throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ public class DefaultLoginUserContext implements LoginUserContext {
private static final ThreadLocal<UserInfo> CURRENT_USER = new ThreadLocal<>();

private static final int DEFAULT_PLATFORM = 1;
private static final String DEFAULT_TENANT = "1";

/**
* 返回当前用户所在的业务租户id
*
* @return 租户Id
*/
@Override
public String getTenantId() {
UserInfo userInfo = CURRENT_USER.get();
List<Tenant> tenantList = userInfo != null ? userInfo.getTenants() : null;
if (tenantList == null || tenantList.isEmpty()) {
return DEFAULT_TENANT;
}
for (Tenant tenant : tenantList) {
if (tenant.getIsInUse()) {
return tenant.getId();
}
}

return DEFAULT_TENANT;
}

@Override
public List<Tenant> getTenants() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,29 @@ public Result<ValidationResult> validateToken(@RequestParam String token) {
@GetMapping("/user/tenant")
public Result<SSOTicket> setTenant(@RequestParam Integer tenantId) {
List<Tenant> tenants = loginUserContext.getTenants();
if (tenantId == null) {
return Result.failed(ExceptionEnum.CM320);
}
if (tenants == null || tenants.isEmpty()) {
return Result.failed(ExceptionEnum.CM337);
}
List<Tenant> currentTenant = new ArrayList<>();
List<Tenant> tenantList = new ArrayList<>();
boolean found = false;
for (Tenant tenant : tenants) {
if (tenant.getId().equals(tenantId.toString())) {
currentTenant.add(tenant);
tenant.setIsInUse(true);
found = true;
} else {
tenant.setIsInUse(false);
}

tenantList.add(tenant);
}
if (currentTenant.isEmpty()) {

if (!found) {
return Result.failed(ExceptionEnum.CM337);
}

// 通过 RequestContextHolder 获取请求
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();
Expand All @@ -259,7 +270,7 @@ public Result<SSOTicket> setTenant(@RequestParam Integer tenantId) {
if (headerToken == null || headerToken.isEmpty()) {
return Result.failed(ExceptionEnum.CM336);
}
String token = jwtUtil.generateTokenWithSelectedTenant(headerToken, currentTenant);
String token = jwtUtil.generateTokenWithSelectedTenant(headerToken, tenantList);
// 将原 token 加入黑名单
Claims claims = Jwts.parser()
.verifyWith(JwtUtil.getSecretKey())
Expand Down
24 changes: 12 additions & 12 deletions base/src/main/java/com/tinyengine/it/mapper/AppMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface AppMapper extends BaseMapper<App> {
*
* @return the list
*/
List<App> queryAllApp();
List<App> queryAllApp(String tenantId);

/**
* 分页查询应用所有信息
Expand All @@ -45,24 +45,24 @@ public interface AppMapper extends BaseMapper<App> {
* @param createdBy the createdBy
* @return the list
*/
List<App> queryAllAppByPage(Integer pageSize, Integer offset, String name,
Integer industryId, Integer sceneId, String framework, String orderBy, String createdBy);
List<App> queryAllAppByPage(Integer pageSize, Integer offset, String name, Integer industryId,
Integer sceneId, String framework, String orderBy, String createdBy, String tenantId);

/**
* 查询表t_app 应用总数
*
* @return the int
*/
@Select("SELECT COUNT(id) FROM t_app WHERE is_template IS NOT TRUE")
int queryAppTotal();
@Select("SELECT COUNT(id) FROM t_app WHERE tenant_id = #{tenantId} AND is_template IS NOT TRUE")
int queryAppTotal(String tenantId);

/**
* 查询表t_app 模版总数
*
* @return the int
*/
@Select("SELECT COUNT(id) FROM t_app WHERE is_template = TRUE")
int queryAppTemplateTotal();
@Select("SELECT COUNT(id) FROM t_app WHERE tenant_id = #{tenantId} AND is_template = TRUE")
int queryAppTemplateTotal(String tenantId);

/**
* 分页查询应用模版所有信息
Expand All @@ -76,24 +76,24 @@ List<App> queryAllAppByPage(Integer pageSize, Integer offset, String name,
* @param createdBy the createdBy
* @return the list
*/
List<App> queryAllAppTemplate(Integer pageSize, Integer offset, String name,
Integer industryId, Integer sceneId, String framework, String orderBy, String createdBy);
List<App> queryAllAppTemplate(Integer pageSize, Integer offset, String name, Integer industryId,
Integer sceneId, String framework, String orderBy, String createdBy, String tenantId);

/**
* 根据主键id查询应用模版数据
*
* @param id the id
* @return the app
*/
App queryAppTemplateById(Integer id);
App queryAppTemplateById(Integer id, String tenantId);

/**
* 根据主键id查询表t_app数据
*
* @param id the id
* @return the app
*/
App queryAppById(Integer id);
App queryAppById(Integer id, String tenantId);

/**
* 根据条件查询表t_app数据
Expand All @@ -109,7 +109,7 @@ List<App> queryAllAppTemplate(Integer pageSize, Integer offset, String name,
* @param id the id
* @return the integer
*/
Integer deleteAppById(@Param("id") Integer id);
Integer deleteAppById(@Param("id") Integer id, String tenantId);

/**
* 根据主键id更新表t_app数据
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.tinyengine.it.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tinyengine.it.model.entity.AuthUsersUnitsRoles;
import com.tinyengine.it.model.entity.Tenant;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface AuthUsersUnitsRolesMapper {
public interface AuthUsersUnitsRolesMapper extends BaseMapper<AuthUsersUnitsRoles> {
/**
* 查询表r_auth_users_units_roles所有信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Tenant {
@TableId(value = "id", type = IdType.AUTO)
private String id;

@Schema(name = "nameCn", description = "组织唯一代码")
@Schema(name = "orgCode", description = "组织唯一代码")
private String orgCode;

@Schema(name = "nameCn", description = "组织中文名")
Expand All @@ -54,6 +54,10 @@ public class Tenant {
@Schema(name = "description", description = "组织描述")
private String description;

@TableField(exist = false)
@Schema(name = "isInUse", description = "是否当前所在组织")
private Boolean isInUse;

@TableField(fill = FieldFill.INSERT)
@Schema(name = "createdTime", description = "创建时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
Expand Down
Loading
Loading