mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 05:14:25 +00:00
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:
@@ -1,11 +1,11 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { ScrollArea } from '@/ui/components/ui/scroll-area'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/ui/components/ui/select'
|
||||
import { cn } from '@/ui/lib/utils'
|
||||
import { m } from '@/ui/paraglide/messages.js'
|
||||
@@ -15,16 +15,16 @@ import { useDroppable } from '@dnd-kit/react'
|
||||
import type { ChatMessage } from '@shumai/dtos'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import {
|
||||
ArrowLeft,
|
||||
ArrowUp,
|
||||
Bot,
|
||||
Brain,
|
||||
ChevronDown,
|
||||
History,
|
||||
Loader2,
|
||||
Plus,
|
||||
Trash2,
|
||||
Wrench,
|
||||
ArrowLeft,
|
||||
ArrowUp,
|
||||
Bot,
|
||||
Brain,
|
||||
ChevronDown,
|
||||
History,
|
||||
Loader2,
|
||||
Plus,
|
||||
Trash2,
|
||||
Wrench,
|
||||
} from 'lucide-react'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import Markdown from 'react-markdown'
|
||||
@@ -105,14 +105,14 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
|
||||
|
||||
// Fetch history when entering history mode
|
||||
useEffect(() => {
|
||||
if (isHistoryMode) {
|
||||
fetchHistorySessions()
|
||||
if (isHistoryMode && teamId) {
|
||||
fetchHistorySessions(teamId)
|
||||
}
|
||||
}, [isHistoryMode, fetchHistorySessions])
|
||||
}, [isHistoryMode, fetchHistorySessions, teamId])
|
||||
|
||||
const handleSend = () => {
|
||||
if (!inputText.trim() || isStreaming) return
|
||||
sendMessage(inputText, projectId, contextAssetId)
|
||||
if (!inputText.trim() || isStreaming || !teamId) return
|
||||
sendMessage(teamId, inputText, projectId, contextAssetId)
|
||||
setInputText('')
|
||||
}
|
||||
|
||||
@@ -441,7 +441,10 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
|
||||
isActive ? 'border-primary' : 'border-border hover:bg-accent/30',
|
||||
)}
|
||||
>
|
||||
<div onClick={() => loadSession(sess.id)} className="flex-1 min-w-0 pr-6">
|
||||
<div
|
||||
onClick={() => teamId && loadSession(teamId, sess.id)}
|
||||
className="flex-1 min-w-0 pr-6"
|
||||
>
|
||||
<div className="text-sm font-semibold text-foreground truncate">
|
||||
{sess.name || m.new_chat()}
|
||||
</div>
|
||||
@@ -452,8 +455,8 @@ export function ChatbotSidebar({ projectId, contextAssetId }: ChatbotSidebarProp
|
||||
<button
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
if (confirm(m.delete_session_confirm())) {
|
||||
deleteSession(sess.id)
|
||||
if (teamId && confirm(m.delete_session_confirm())) {
|
||||
deleteSession(teamId, sess.id)
|
||||
}
|
||||
}}
|
||||
className="absolute right-3 opacity-0 group-hover:opacity-100 focus:opacity-100 transition-opacity text-muted-foreground hover:text-destructive p-1 rounded hover:bg-muted"
|
||||
|
||||
@@ -4,17 +4,22 @@ import { client } from '@/ui/api/client'
|
||||
import type { ChatMessage } from '@shumai/dtos'
|
||||
|
||||
vi.mock('@/ui/api/client', () => {
|
||||
const chatMock = {
|
||||
$post: vi.fn(),
|
||||
sessions: {
|
||||
$get: vi.fn(() => Promise.resolve({ ok: true, json: async () => ({ data: [] }) } as any)),
|
||||
$delete: vi.fn(),
|
||||
messages: {
|
||||
$get: vi.fn(),
|
||||
},
|
||||
},
|
||||
}
|
||||
return {
|
||||
client: {
|
||||
api: {
|
||||
chat: {
|
||||
$post: vi.fn(),
|
||||
sessions: {
|
||||
$get: vi.fn(),
|
||||
$delete: vi.fn(),
|
||||
messages: {
|
||||
$get: vi.fn(),
|
||||
},
|
||||
teams: {
|
||||
':teamId': {
|
||||
chat: chatMock,
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -99,9 +104,9 @@ describe('useChatbotStore', () => {
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.mocked(client.api.chat.$post).mockResolvedValue(mockResponse as any)
|
||||
vi.mocked(client.api.teams[':teamId'].chat.$post).mockResolvedValue(mockResponse as any)
|
||||
|
||||
const sendPromise = useChatbotStore.getState().sendMessage('hello', 'proj-123')
|
||||
const sendPromise = useChatbotStore.getState().sendMessage('team-123', 'hello', 'proj-123')
|
||||
|
||||
// Optimistic message should be present immediately
|
||||
const stateWithOptimistic = useChatbotStore.getState()
|
||||
@@ -166,9 +171,11 @@ describe('useChatbotStore', () => {
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.mocked(client.api.chat.$post).mockResolvedValue(mockResponse as any)
|
||||
vi.mocked(client.api.teams[':teamId'].chat.$post).mockResolvedValue(mockResponse as any)
|
||||
|
||||
const sendPromise = useChatbotStore.getState().sendMessage('new message', 'proj-123')
|
||||
const sendPromise = useChatbotStore
|
||||
.getState()
|
||||
.sendMessage('team-123', 'new message', 'proj-123')
|
||||
|
||||
// Optimistic message should be appended immediately
|
||||
const stateWithOptimistic = useChatbotStore.getState()
|
||||
@@ -221,9 +228,9 @@ describe('useChatbotStore', () => {
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
vi.mocked(client.api.chat.$post).mockResolvedValue(mockResponse as any)
|
||||
vi.mocked(client.api.teams[':teamId'].chat.$post).mockResolvedValue(mockResponse as any)
|
||||
|
||||
const sendPromise = useChatbotStore.getState().sendMessage('hello', 'proj-123')
|
||||
const sendPromise = useChatbotStore.getState().sendMessage('team-123', 'hello', 'proj-123')
|
||||
|
||||
// 3. Verify that both the optimistic context message and user message are present immediately
|
||||
const stateWithOptimistic = useChatbotStore.getState()
|
||||
|
||||
@@ -29,11 +29,16 @@ interface ChatbotState {
|
||||
selectedAgentId: string | null
|
||||
setSelectedAgentId: (id: string | null) => void
|
||||
|
||||
fetchHistorySessions: () => Promise<void>
|
||||
loadSession: (sessionId: string) => Promise<void>
|
||||
deleteSession: (sessionId: string) => Promise<void>
|
||||
fetchHistorySessions: (teamId: string) => Promise<void>
|
||||
loadSession: (teamId: string, sessionId: string) => Promise<void>
|
||||
deleteSession: (teamId: string, sessionId: string) => Promise<void>
|
||||
startNewSession: () => void
|
||||
sendMessage: (text: string, projectId: string, contextAssetId?: string) => Promise<void>
|
||||
sendMessage: (
|
||||
teamId: string,
|
||||
text: string,
|
||||
projectId: string,
|
||||
contextAssetId?: string,
|
||||
) => Promise<void>
|
||||
}
|
||||
|
||||
export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
@@ -64,9 +69,10 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
selectedAgentId: null,
|
||||
setSelectedAgentId: (id) => set({ selectedAgentId: id }),
|
||||
|
||||
fetchHistorySessions: async () => {
|
||||
fetchHistorySessions: async (teamId) => {
|
||||
try {
|
||||
const res = await client.api.chat.sessions.$get({
|
||||
const res = await client.api.teams[':teamId'].chat.sessions.$get({
|
||||
param: { teamId },
|
||||
query: { first: '50' },
|
||||
})
|
||||
if (res.ok) {
|
||||
@@ -78,10 +84,10 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
}
|
||||
},
|
||||
|
||||
loadSession: async (sessionId) => {
|
||||
loadSession: async (teamId, sessionId) => {
|
||||
try {
|
||||
const res = await client.api.chat.sessions[':sessionId'].messages.$get({
|
||||
param: { sessionId },
|
||||
const res = await client.api.teams[':teamId'].chat.sessions[':sessionId'].messages.$get({
|
||||
param: { teamId, sessionId },
|
||||
})
|
||||
if (res.ok) {
|
||||
const messages = await res.json()
|
||||
@@ -104,10 +110,10 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
}
|
||||
},
|
||||
|
||||
deleteSession: async (sessionId) => {
|
||||
deleteSession: async (teamId, sessionId) => {
|
||||
try {
|
||||
const res = await client.api.chat.sessions[':sessionId'].$delete({
|
||||
param: { sessionId },
|
||||
const res = await client.api.teams[':teamId'].chat.sessions[':sessionId'].$delete({
|
||||
param: { teamId, sessionId },
|
||||
})
|
||||
if (res.ok) {
|
||||
toast.success('Session deleted successfully')
|
||||
@@ -133,7 +139,7 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
})
|
||||
},
|
||||
|
||||
sendMessage: async (text, projectId, contextAssetId) => {
|
||||
sendMessage: async (teamId, text, projectId, contextAssetId) => {
|
||||
const state = useChatbotStore.getState()
|
||||
if (state.isStreaming) return
|
||||
|
||||
@@ -180,7 +186,8 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await client.api.chat.$post({
|
||||
const res = await client.api.teams[':teamId'].chat.$post({
|
||||
param: { teamId },
|
||||
json: {
|
||||
agentId,
|
||||
textPrompt: text,
|
||||
@@ -272,7 +279,7 @@ export const useChatbotStore = create<ChatbotState>((set) => ({
|
||||
|
||||
// If we finished successfully, trigger a history list refresh
|
||||
const finalState = useChatbotStore.getState()
|
||||
finalState.fetchHistorySessions()
|
||||
finalState.fetchHistorySessions(teamId)
|
||||
} catch (err) {
|
||||
const errMsg = err instanceof Error ? err.message : 'Failed to send message'
|
||||
toast.error(errMsg)
|
||||
|
||||
Reference in New Issue
Block a user