feat(chat): enforce team-scoped authorization and session creator check

- Update chat API routes and service methods to include and verify teamId context
- Validate that the requesting user is the creator of the chat session during continuation
- Update frontend chatbot store and components to pass teamId parameter
- Update backend API/core and frontend store tests to verify team scoping and creator checks
This commit is contained in:
yiling
2026-07-13 11:35:55 +08:00
parent 61c49b4d7f
commit e080167ac5
7 changed files with 158 additions and 91 deletions
+12 -3
View File
@@ -141,6 +141,7 @@ export class ChatService {
async startOrContinueChat(
user: User,
teamId: string,
req: ChatRequest,
): Promise<{ sessionId: string; taskId: string }> {
const {
@@ -254,13 +255,17 @@ export class ChatService {
if (!agent) {
throw new Error(`Agent with ID "${agentId}" not found`)
}
if (agent.teamId !== teamId) {
throw new Error(`Agent does not belong to the specified team`)
}
if (activeSessionId) {
const sessionExists = await tx.agentSession.findUnique({
where: { id: activeSessionId },
select: { id: true },
select: { id: true, userId: true },
})
if (!sessionExists) throw new Error('Session not found')
if (sessionExists.userId !== user.id) throw new Error('Unauthorized session access')
// Update the session's agentId to match the selected agent
await tx.agentSession.update({
@@ -313,11 +318,15 @@ export class ChatService {
async listSessions(
userId: string,
teamId: string,
params: { first?: number; after?: string },
): Promise<PaginatedData<ChatSessionInfo[]>> {
const where: Prisma.AgentSessionWhereInput = {
userId,
type: 'chat',
agent: {
teamId,
},
}
return await paginateQuery(
@@ -344,7 +353,7 @@ export class ChatService {
)
}
async listMessages(userId: string, sessionId: string): Promise<ChatMessage[]> {
async listMessages(userId: string, teamId: string, sessionId: string): Promise<ChatMessage[]> {
const session = await this.prismaClient.agentSession.findUnique({
where: { id: sessionId },
})
@@ -371,7 +380,7 @@ export class ChatService {
return buildSessionMessages(pathEntries)
}
async deleteSession(userId: string, sessionId: string): Promise<void> {
async deleteSession(userId: string, teamId: string, sessionId: string): Promise<void> {
const session = await this.prismaClient.agentSession.findUnique({
where: { id: sessionId },
})