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:
wizardchen
2026-07-20 18:23:35 +08:00
committed by lyingbug
parent eab91d2f7f
commit 76a4ffe525
18 changed files with 317 additions and 65 deletions
+4
View File
@@ -68,6 +68,10 @@ type SessionRepository interface {
// GetByID loads a session by tenant and id without user scoping. Callers
// must enforce access (e.g. embed channel + session signature).
GetByID(ctx context.Context, tenantID uint64, id string) (*types.Session, error)
// GetIMPlatform returns the IM platform bound to a session via
// im_channel_sessions, or "" when the session has no IM mapping. Used to
// classify a session's origin folder on read without a full list query.
GetIMPlatform(ctx context.Context, tenantID uint64, sessionID string) (string, error)
// GetByTenantID gets all sessions visible to the tenant/user scope.
GetByTenantID(ctx context.Context, tenantID uint64, userID string) ([]*types.Session, error)
// GetPagedByTenantID gets paged sessions visible to the tenant/user scope.
+6 -1
View File
@@ -20,6 +20,11 @@ const (
// across chat sessions within the same channel.
const EmbedVisitorHeader = "X-Embed-Visitor"
// SessionOwnerAPITenantKeyPrefix prefixes sessions.user_id for rows created by a
// tenant API key. The full owner id is "api_tenant_key:<tenantID>:<keyID>", so a
// LIKE '<prefix>%' selects every API-key session in the tenant.
const SessionOwnerAPITenantKeyPrefix = "api_tenant_key:"
// Principal represents the terminal caller for per-subject isolation features.
// It is intentionally separate from UserID: many principals, such as IM users
// or embed visitors, are not WeKnora accounts and must not imply RBAC rights.
@@ -164,7 +169,7 @@ func SessionOwnerIDFromContext(ctx context.Context) string {
case PrincipalAPITenant:
if scope, ok := TenantAPIKeyScopeFromContext(ctx); ok && scope.KeyID > 0 {
if tenantID, ok := TenantIDFromContext(ctx); ok && tenantID > 0 {
return fmt.Sprintf("api_tenant_key:%d:%d", tenantID, scope.KeyID)
return fmt.Sprintf("%s%d:%d", SessionOwnerAPITenantKeyPrefix, tenantID, scope.KeyID)
}
}
}
+14 -1
View File
@@ -118,6 +118,12 @@ type Session struct {
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"`
// IMPlatform is the originating IM platform (e.g. "feishu", "wecom") when
// this session is bound to an IM channel. It is not stored on the sessions
// table (it lives in im_channel_sessions) and is populated on read so the
// Web console can classify a session's origin folder without a list query.
IMPlatform string `json:"im_platform,omitempty" gorm:"-"`
// Association relationship, not stored in the database
Messages []Message `json:"-" gorm:"foreignKey:SessionID"`
}
@@ -127,11 +133,18 @@ func (s *Session) BeforeCreate(tx *gorm.DB) (err error) {
return nil
}
// SessionSourceAPI is the source filter that lists every session created via a
// tenant API key across the whole tenant. It is an admin-only view: the service
// layer requires Admin+ and drops the per-user owner scope so a tenant
// Owner/admin can observe API-key traffic that is otherwise isolated per key.
const SessionSourceAPI = "api"
// SessionListQuery bundles the parameters for listing sessions.
// UserID empty means "tenant-wide" (used by API-key callers / legacy rows).
// Keyword matches title ILIKE '%keyword%'.
// Source values: "web" (user chats, no IM/embed), "embed" / "embed:{channelID}",
// or an IM platform name (e.g. "feishu", "wechat").
// "api" (all API-key sessions, Admin+ only), or an IM platform name
// (e.g. "feishu", "wechat").
// AgentID currently only filters sessions that have an IM channel mapping.
type SessionListQuery struct {
TenantID uint64