Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(topic): let workspace admins move teammates' topics (#21776)

Lobehub Coder9db13e998183
diff --git a/apps/server/src/routers/lambda/_helpers/assertWorkspaceRowManageable.test.ts b/apps/server/src/routers/lambda/_helpers/assertWorkspaceRowManageable.test.ts
index 48203a0b142..b4da3109060 100644
--- a/apps/server/src/routers/lambda/_helpers/assertWorkspaceRowManageable.test.ts
+++ b/apps/server/src/routers/lambda/_helpers/assertWorkspaceRowManageable.test.ts
@@ -48,6 +48,38 @@ describe('assertWorkspaceRowManageable', () => {
}
});
+ it('rejects a workspace admin by default', () => {
+ expect(() =>
+ assertWorkspaceRowManageable(
+ { userId: 'me', workspaceId: 'ws1', workspaceRole: 'admin' },
+ 'someone-else',
+ 'topic',
+ ),
+ ).toThrow('Only the creator or a workspace owner can modify this topic');
+ });
+
+ it('allows a workspace admin when the call site opts in', () => {
+ expect(() =>
+ assertWorkspaceRowManageable(
+ { userId: 'me', workspaceId: 'ws1', workspaceRole: 'admin' },
+ 'someone-else',
+ 'topic',
+ { allowAdmin: true },
+ ),
+ ).not.toThrow();
+ });
+
+ it('still rejects a member when admins are allowed', () => {
+ expect(() =>
+ assertWorkspaceRowManageable(
+ { userId: 'me', workspaceId: 'ws1', workspaceRole: 'member' },
+ 'someone-else',
+ 'topic',
+ { allowAdmin: true },
+ ),
+ ).toThrow('Only the creator or a workspace owner or admin can modify this topic');
+ });
+
it('rejects when the row has no creator recorded', () => {
expect(() =>
assertWorkspaceRowManageable(
diff --git a/apps/server/src/routers/lambda/_helpers/assertWorkspaceRowManageable.ts b/apps/server/src/routers/lambda/_helpers/assertWorkspaceRowManageable.ts
index d1a3391ba7c..250c7fd81d1 100644
--- a/apps/server/src/routers/lambda/_helpers/assertWorkspaceRowManageable.ts
+++ b/apps/server/src/routers/lambda/_helpers/assertWorkspaceRowManageable.ts
@@ -8,6 +8,14 @@ interface WorkspaceRowCtx {
export type WorkspaceBulkDeleteScope = 'own' | 'workspace';
+export interface WorkspaceRowManageOptions {
+ /**
+ * Also let workspace admins manage rows other members created. Opt-in per
+ * call site so widening a resource's rule is a deliberate, local decision.
+ */
+ allowAdmin?: boolean;
+}
+
/**
* Row-level creator check for workspace-shared resources (connectors, skills,
* installed plugins). `buildWorkspaceWhere` makes these rows visible/writable
@@ -19,19 +27,24 @@ export type WorkspaceBulkDeleteScope = 'own' | 'workspace';
* - Personal mode (no `workspaceId`): pass through β€” the model's ownership
* filter already scopes rows to the caller.
* - Workspace owner: pass through β€” owners manage all workspace resources.
+ * - Workspace admin with `allowAdmin`: pass through for that call site.
* - Otherwise the row's creator must be the caller.
*/
export function assertWorkspaceRowManageable(
ctx: WorkspaceRowCtx,
rowUserId: string | null | undefined,
resource: string,
+ options: WorkspaceRowManageOptions = {},
): void {
if (!ctx.workspaceId) return;
if (ctx.workspaceRole === 'owner') return;
+ if (options.allowAdmin && ctx.workspaceRole === 'admin') return;
if (rowUserId !== ctx.userId) {
throw new TRPCError({
code: 'FORBIDDEN',
- message: `Only the creator or a workspace owner can modify this ${resource}`,
+ message: options.allowAdmin
+ ? `Only the creator or a workspace owner or admin can modify this ${resource}`
+ : `Only the creator or a workspace owner can modify this ${resource}`,
});
}
}
diff --git a/apps/server/src/routers/lambda/topic/update.batchMoveTopics.test.ts b/apps/server/src/routers/lambda/topic/update.batchMoveTopics.test.ts
new file mode 100644
index 00000000000..21224c13286
--- /dev/null
+++ b/apps/server/src/routers/lambda/topic/update.batchMoveTopics.test.ts
@@ -0,0 +1,95 @@
+// @vitest-environment node
+import { type LobeChatDatabase } from '@lobechat/database';
+import { agents, topics, workspaces } from '@lobechat/database/schemas';
+import { getTestDB } from '@lobechat/database/test-utils';
+import { eq } from 'drizzle-orm';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { cleanupTestUser, createTestUser } from '../__tests__/integration/setup';
+
+let testDB: LobeChatDatabase;
+vi.mock('@/database/core/db-adaptor', () => ({
+ getServerDB: vi.fn(function () {
+ return testDB;
+ }),
+}));
+
+// The General-access `use` guards have their own tests; this file covers the
+// creator/role rule layered on top of them.
+vi.mock('../_helpers/conversationResourceGuard', async (importOriginal) => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ assertCanUseConversationTargets: vi.fn(async () => {}),
+ assertCanUseTopicTargets: vi.fn(async () => []),
+}));
+
+const { topicRouter } = await import('./index');
+
+describe('topic.batchMoveTopics in a workspace', () => {
+ let creatorId: string;
+ let operatorId: string;
+ let workspaceId: string;
+
+ beforeEach(async () => {
+ testDB = await getTestDB();
+ creatorId = await createTestUser(testDB);
+ operatorId = await createTestUser(testDB);
+ const [workspace] = await testDB
+ .insert(workspaces)
+ .values({ name: 'Team', primaryOwnerId: creatorId, slug: `ws-move-${creatorId}` })
+ .returning();
+ workspaceId = workspace.id;
+ });
+
+ afterEach(async () => {
+ await cleanupTestUser(testDB, operatorId);
+ await cleanupTestUser(testDB, creatorId);
+ });
+
+ /** A teammate's topic on one workspace agent, plus a second agent to move it to. */
+ const seedTeammateTopic = async () => {
+ const [source, target] = await testDB
+ .insert(agents)
+ .values([
+ { slug: `src-${creatorId}`, userId: creatorId, workspaceId },
+ { slug: `dst-${creatorId}`, userId: creatorId, workspaceId },
+ ])
+ .returning();
+ const [topic] = await testDB
+ .insert(topics)
+ .values({ agentId: source.id, title: 'Teammate topic', userId: creatorId, workspaceId })
+ .returning();
+ return { targetAgentId: target.id, topicId: topic.id };
+ };
+
+ const readAgentId = async (topicId: string) => {
+ const [row] = await testDB.select().from(topics).where(eq(topics.id, topicId));
+ return row.agentId;
+ };
+
+ it("lets a workspace admin move a teammate's topic", async () => {
+ const { targetAgentId, topicId } = await seedTeammateTopic();
+ const caller = topicRouter.createCaller({
+ userId: operatorId,
+ workspaceId,
+ workspaceRole: 'admin',
+ } as any);
+
+ await caller.batchMoveTopics({ targetAgentId, topicIds: [topicId] });
+
+ expect(await readAgentId(topicId)).toBe(targetAgentId);
+ });
+
+ it("still refuses a plain member moving a teammate's topic", async () => {
+ const { targetAgentId, topicId } = await seedTeammateTopic();
+ const caller = topicRouter.createCaller({
+ userId: operatorId,
+ workspaceId,
+ workspaceRole: 'member',
+ } as any);
+
+ await expect(
+ caller.batchMoveTopics({ targetAgentId, topicIds: [topicId] }),
+ ).rejects.toMatchObject({ code: 'FORBIDDEN' });
+ expect(await readAgentId(topicId)).not.toBe(targetAgentId);
+ });
+});
diff --git a/apps/server/src/routers/lambda/topic/update.ts b/apps/server/src/routers/lambda/topic/update.ts
index 11e74081630..362c90e305f 100644
--- a/apps/server/src/routers/lambda/topic/update.ts
+++ b/apps/server/src/routers/lambda/topic/update.ts
@@ -129,8 +129,10 @@ export const updateRouter = router({
)
.mutation(async ({ input, ctx }) => {
const rows = await ctx.topicModel.findOwnersByIds(input.topicIds);
+ // Admins may re-home teammates' topics (e.g. consolidating a team's
+ // topics under one agent); everything else stays creator/owner-only.
for (const userId of new Set(rows.map((row) => row.userId))) {
- assertWorkspaceRowManageable(ctx, userId, 'topic');
+ assertWorkspaceRowManageable(ctx, userId, 'topic', { allowAdmin: true });
}
// Moving needs `use` on both the source conversations and the target agent.
await assertCanUseTopicTargets(guardCtx(ctx), input.topicIds);
@@ -153,7 +155,8 @@ export const updateRouter = router({
);
const goalHandoff = new GoalTopicHandoffService(tx, ctx.userId, workspaceId);
const goals = await goalHandoff.lockFollowing(input.topicIds, input.targetAgentId);
- for (const goal of goals) assertWorkspaceRowManageable(ctx, goal.userId, 'goal');
+ for (const goal of goals)
+ assertWorkspaceRowManageable(ctx, goal.userId, 'goal', { allowAdmin: true });
return { movedGoalIds: await goalHandoff.handOver(goals, input.targetAgentId) };
});
}),