feat(session): add admin API session bucket and stabilize sidebar filter
Let tenant admins list and open API-key sessions from the Web console while keeping per-key isolation for everyone else. Pin the session source filter so it no longer jumps when switching buckets, and resolve session origin on read so deep links stay in sync with the active folder after refresh.
This commit is contained in:
@@ -72,6 +72,25 @@ func (r *sessionRepository) GetByID(ctx context.Context, tenantID uint64, id str
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
// GetIMPlatform returns the IM platform bound to a session, or "" when none.
|
||||
// It intentionally ignores soft-deleted mappings' visibility rules used by
|
||||
// QueryPaged: any mapping (active or cleared) marks the session as IM-origin.
|
||||
func (r *sessionRepository) GetIMPlatform(
|
||||
ctx context.Context, tenantID uint64, sessionID string,
|
||||
) (string, error) {
|
||||
var platform string
|
||||
err := r.db.WithContext(ctx).
|
||||
Table("im_channel_sessions AS ics").
|
||||
Joins("JOIN sessions AS s ON s.id = ics.session_id").
|
||||
Where("ics.session_id = ? AND s.tenant_id = ?", sessionID, tenantID).
|
||||
Limit(1).
|
||||
Pluck("ics.platform", &platform).Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return platform, nil
|
||||
}
|
||||
|
||||
// GetByTenantID retrieves all sessions for a tenant
|
||||
func (r *sessionRepository) GetByTenantID(ctx context.Context, tenantID uint64, userID string) ([]*types.Session, error) {
|
||||
var sessions []*types.Session
|
||||
@@ -169,6 +188,12 @@ func (r *sessionRepository) QueryPaged(
|
||||
switch lower {
|
||||
case "":
|
||||
return db
|
||||
case types.SessionSourceAPI:
|
||||
// Tenant-wide view of API-key sessions. Their owner id is
|
||||
// "api_tenant_key:<tenantID>:<keyID>", so a prefix match selects
|
||||
// every key's sessions. The service layer already enforced Admin+
|
||||
// and cleared the per-user scope for this source.
|
||||
return db.Where("s.user_id LIKE ?", types.SessionOwnerAPITenantKeyPrefix+"%")
|
||||
case "web":
|
||||
// User web chats only — exclude embed-widget sessions (same IM-null row).
|
||||
return db.Where(
|
||||
|
||||
@@ -245,3 +245,49 @@ func TestSessionRepositoryQueryPagedSplitsWebAndEmbedSessions(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []string{embed.ID}, listItemIDsForTest(embedItems))
|
||||
}
|
||||
|
||||
func TestSessionRepositoryGetIMPlatform(t *testing.T) {
|
||||
repo, db := newSessionRepositoryForTest(t)
|
||||
require.NoError(t, db.AutoMigrate(&testIMChannelSession{}))
|
||||
ctx := context.Background()
|
||||
|
||||
imSession := createSessionForTest(t, db, 1, "alice")
|
||||
require.NoError(t, db.Create(&testIMChannelSession{
|
||||
ID: "ics-1", SessionID: imSession.ID, Platform: "feishu",
|
||||
}).Error)
|
||||
webSession := createSessionForTest(t, db, 1, "alice")
|
||||
|
||||
platform, err := repo.GetIMPlatform(ctx, 1, imSession.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "feishu", platform)
|
||||
|
||||
platform, err = repo.GetIMPlatform(ctx, 1, webSession.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "", platform)
|
||||
|
||||
// Cross-tenant lookup must not leak the mapping.
|
||||
platform, err = repo.GetIMPlatform(ctx, 2, imSession.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "", platform)
|
||||
}
|
||||
|
||||
func TestSessionRepositoryQueryPagedAPISourceReturnsAllTenantAPIKeySessions(t *testing.T) {
|
||||
repo, db := newSessionRepositoryForTest(t)
|
||||
require.NoError(t, db.AutoMigrate(&testIMChannelSession{}))
|
||||
ctx := context.Background()
|
||||
|
||||
// Two different API keys plus a web user and a cross-tenant API session.
|
||||
key1 := createSessionForTest(t, db, 1, types.SessionOwnerAPITenantKeyPrefix+"1:10")
|
||||
key2 := createSessionForTest(t, db, 1, types.SessionOwnerAPITenantKeyPrefix+"1:20")
|
||||
_ = createSessionForTest(t, db, 1, "alice")
|
||||
_ = createSessionForTest(t, db, 2, types.SessionOwnerAPITenantKeyPrefix+"2:30")
|
||||
|
||||
// The admin view clears UserID, so every API-key session in the tenant is
|
||||
// returned regardless of which key created it.
|
||||
items, total, err := repo.QueryPaged(ctx, &types.SessionListQuery{
|
||||
TenantID: 1, UserID: "", Source: types.SessionSourceAPI, Page: 1, PageSize: 50,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.EqualValues(t, 2, total)
|
||||
require.ElementsMatch(t, []string{key1.ID, key2.ID}, listItemIDsForTest(items))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user