Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat: 修复IM问答无法读取session的问题

wangke.20022e540654f124
diff --git a/internal/application/service/session.go b/internal/application/service/session.go
index ea9bd3036..c38c7b01c 100644
--- a/internal/application/service/session.go
+++ b/internal/application/service/session.go
@@ -34,11 +34,13 @@ func loadSessionForRead(
ownerID, sessionID string,
) (*types.Session, error) {
isAdmin := types.TenantRoleFromContext(ctx).HasPermission(types.TenantRoleAdmin)
+ principal, hasPrincipal := types.PrincipalFromContext(ctx)
+ isIMRuntime := hasPrincipal && principal.Type == types.PrincipalIMUser
session, err := repo.Get(ctx, tenantID, ownerID, sessionID)
if err == nil {
imPlatform, _ := repo.GetIMPlatform(ctx, tenantID, sessionID)
- if types.SessionRequiresAdminConsoleRead(session, imPlatform) && !isAdmin {
+ if types.SessionRequiresAdminConsoleRead(session, imPlatform) && !isAdmin && !isIMRuntime {
return nil, apperrors.ErrSessionNotFound
}
if imPlatform != "" {
diff --git a/internal/application/service/session_user_scope_test.go b/internal/application/service/session_user_scope_test.go
index 343e3efa5..0f93203d9 100644
--- a/internal/application/service/session_user_scope_test.go
+++ b/internal/application/service/session_user_scope_test.go
@@ -292,6 +292,33 @@ func TestGetSessionDeniesViewerOnIMSession(t *testing.T) {
require.Equal(t, "feishu", got.IMPlatform)
}
+func TestGetSessionAllowsIMRuntimeToReadIMSession(t *testing.T) {
+ svc, db := newTestSessionService(t)
+ require.NoError(t, db.AutoMigrate(&testListSessionsIMChannelSession{}))
+
+ imSession := &types.Session{TenantID: 1, Title: "feishu chat"}
+ require.NoError(t, db.Create(imSession).Error)
+ require.NoError(t, db.Create(&testListSessionsIMChannelSession{
+ SessionID: imSession.ID,
+ Platform: "feishu",
+ }).Error)
+
+ ctx := context.WithValue(
+ testSessionScopeContext(1, "system-1"),
+ types.TenantRoleContextKey,
+ types.TenantRoleViewer,
+ )
+ ctx = types.WithPrincipal(ctx, types.Principal{
+ Type: types.PrincipalIMUser,
+ ID: "1:channel-1:feishu:open-id-1",
+ })
+
+ got, err := svc.GetSession(ctx, imSession.ID)
+ require.NoError(t, err)
+ require.Equal(t, imSession.ID, got.ID)
+ require.Equal(t, "feishu", got.IMPlatform)
+}
+
// testListSessionsIMChannelSession lets QueryPaged's LEFT JOIN resolve against a
// real table in the in-memory SQLite database.
type testListSessionsIMChannelSession struct {