mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 20:47:44 +00:00
* feat(authz): enforce project-aware roles for agent/skill/mcp permissions Introduce a shared resolveEffectiveRole helper that mirrors AuthzService precedence rules (projectMember.role override -> team role for team-scoped members -> restricted), and route all agent/skill/mcp runtime permission checks through it. Previously these checks used the raw teamMember.role, which let project-only members execute team-level agents/skills/MCP tools and ignored per-project role overrides. Changes: - authz: add resolveEffectiveRole(teamId, projectId, userId, db) and refactor hasPermission to use it (behavior-preserving) - chat/asset/agent-activity: gate agent usage by the effective project role - read_skill: enforce skill permission via effective role (projectId threaded from the agent session) - mcp-service: checkPermission and buildAgentTools filter by effective role; un-permitted servers' tools are no longer advertised to the model - agent activity: only advertise skills the user's effective role permits - listProjectMembers: filter included agent bots by the requester's effective project role - new GET /projects/:projectId/chat-agents API returning chat agents scoped to the project and the user's effective role; WebUI chat selector now uses it (settings keeps the team-level API) - tests: effective-role resolver, chat/mention/MCP/skill project-role matrices, listProjectChatAgents, listProjectMembers filter, API route * fix(agent): forward projectId to getAgentChatContextActivity in agentChat workflow The agentChat workflow called getAgentChatContextActivity without passing projectId from the task payload, so project-scoped members (scope: 'project') resolved a null effective role in the activity and were denied with 'Insufficient role to use agent' even though the chat selector and chat.ts both authorized them. Forward payload.projectId so the activity enforces the user's effective project role, and assert the forwarding in the workflow unit test (fails without the fix).
205 lines
5.4 KiB
TypeScript
205 lines
5.4 KiB
TypeScript
import { Hono } from 'hono'
|
|
import { zValidator } from '@hono/zod-validator'
|
|
import { agentService } from '@shumai/core/src/agent/agent'
|
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
|
import {
|
|
createAgentRequestSchema,
|
|
updateAgentRequestSchema,
|
|
updateAgentPermissionRequestSchema,
|
|
paginationParamsSchema,
|
|
AgentInfo,
|
|
AuditAction,
|
|
} from '@shumai/dtos'
|
|
|
|
import type { Prisma } from '@shumai/db'
|
|
import { auditLogService } from '@shumai/core/src/auditLog/auditLog'
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
const route = new Hono<{ Variables: { user: User } }>()
|
|
.get('/teams/:teamId/agents', async (c) => {
|
|
const teamId = c.req.param('teamId')
|
|
const userReq = c.get('user')
|
|
|
|
await authzService.hasPermission({
|
|
user: userReq,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
const agents = await agentService.listAgents({ teamId, userId: userReq?.id })
|
|
|
|
const res: AgentInfo[] = await Promise.all(
|
|
agents.map((agent) => agentService.toAgentInfo(agent)),
|
|
)
|
|
|
|
return c.json(res, 200)
|
|
})
|
|
.get('/projects/:projectId/chat-agents', async (c) => {
|
|
const projectId = c.req.param('projectId')
|
|
const userReq = c.get('user')
|
|
|
|
await authzService.hasPermission({
|
|
user: userReq,
|
|
permission: Permission.Read,
|
|
type: ResourceType.Project,
|
|
id: projectId,
|
|
})
|
|
|
|
const agents = await agentService.listProjectChatAgents(projectId, userReq?.id)
|
|
return c.json(agents, 200)
|
|
})
|
|
.post('/teams/:teamId/agents', zValidator('json', createAgentRequestSchema), async (c) => {
|
|
const teamId = c.req.param('teamId')
|
|
const userReq = c.get('user')
|
|
const req = c.req.valid('json')
|
|
|
|
await authzService.hasPermission({
|
|
user: userReq,
|
|
permission: Permission.Admin,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
const agent = await agentService.createAgent({
|
|
teamId,
|
|
...req,
|
|
})
|
|
|
|
if (!agent) throw new Error('failed to create agent')
|
|
|
|
const info = await agentService.toAgentInfo(agent)
|
|
|
|
await auditLogService.logAction({
|
|
action: AuditAction.agent_create,
|
|
teamId,
|
|
userId: userReq?.id,
|
|
itemId: agent.id,
|
|
})
|
|
|
|
return c.json(info, 200)
|
|
})
|
|
.put('/agents/:agentId', zValidator('json', updateAgentRequestSchema), async (c) => {
|
|
const agentId = c.req.param('agentId')
|
|
const userReq = c.get('user')
|
|
const req = c.req.valid('json')
|
|
|
|
await authzService.hasPermission({
|
|
user: userReq,
|
|
permission: Permission.Admin,
|
|
type: ResourceType.Agent,
|
|
id: agentId,
|
|
})
|
|
|
|
const agent = await agentService.updateAgent({
|
|
agentId,
|
|
...req,
|
|
})
|
|
|
|
await auditLogService.logAction({
|
|
action: AuditAction.agent_update,
|
|
teamId: agent.teamId,
|
|
userId: userReq?.id,
|
|
itemId: agent.id,
|
|
})
|
|
|
|
const info = await agentService.toAgentInfo(agent)
|
|
|
|
return c.json(info, 200)
|
|
})
|
|
.patch(
|
|
'/agents/:agentId/permission',
|
|
zValidator('json', updateAgentPermissionRequestSchema),
|
|
async (c) => {
|
|
const agentId = c.req.param('agentId')
|
|
const userReq = c.get('user')
|
|
const req = c.req.valid('json')
|
|
|
|
await authzService.hasPermission({
|
|
user: userReq,
|
|
permission: Permission.Admin,
|
|
type: ResourceType.Agent,
|
|
id: agentId,
|
|
})
|
|
|
|
const agent = await agentService.updateAgentPermission(agentId, req.permission)
|
|
|
|
await auditLogService.logAction({
|
|
action: AuditAction.agent_update,
|
|
teamId: agent.teamId,
|
|
userId: userReq?.id,
|
|
itemId: agent.id,
|
|
})
|
|
|
|
const info = await agentService.toAgentInfo(agent)
|
|
|
|
return c.json(info, 200)
|
|
},
|
|
)
|
|
.delete('/agents/:agentId', async (c) => {
|
|
const agentId = c.req.param('agentId')
|
|
const userReq = c.get('user')
|
|
|
|
await authzService.hasPermission({
|
|
user: userReq,
|
|
permission: Permission.Admin,
|
|
type: ResourceType.Agent,
|
|
id: agentId,
|
|
})
|
|
|
|
const existingAgent = await agentService.getAgent({ agentId })
|
|
|
|
await agentService.deleteAgent({
|
|
agentId,
|
|
})
|
|
|
|
if (existingAgent) {
|
|
await auditLogService.logAction({
|
|
action: AuditAction.agent_delete,
|
|
teamId: existingAgent.teamId,
|
|
userId: userReq?.id,
|
|
itemId: agentId,
|
|
})
|
|
}
|
|
|
|
return new Response(null, { status: 204 })
|
|
})
|
|
.get('/agent-sessions/:sessionId/entries', async (c) => {
|
|
const sessionId = c.req.param('sessionId')
|
|
const userReq = c.get('user')
|
|
|
|
await authzService.hasPermission({
|
|
user: userReq,
|
|
permission: Permission.Admin,
|
|
type: ResourceType.AgentSession,
|
|
id: sessionId,
|
|
})
|
|
|
|
const entries = await agentService.getSessionEntries({ sessionId })
|
|
const infos = entries.map((entry) => ({
|
|
id: entry.id,
|
|
sessionId: entry.sessionId,
|
|
entry: entry.entry,
|
|
}))
|
|
|
|
return c.json(infos, 200)
|
|
})
|
|
.get('/teams/:teamId/agent-sessions', zValidator('query', paginationParamsSchema), async (c) => {
|
|
const teamId = c.req.param('teamId')
|
|
const userReq = c.get('user')
|
|
const query = c.req.valid('query')
|
|
|
|
await authzService.hasPermission({
|
|
user: userReq,
|
|
permission: Permission.Admin,
|
|
type: ResourceType.Team,
|
|
id: teamId,
|
|
})
|
|
|
|
const res = await agentService.listSessions(teamId, query)
|
|
return c.json(res, 200)
|
|
})
|
|
|
|
export default route
|