feat(project): support adding existing team members and prioritize project-specific roles

This commit is contained in:
yiling
2026-06-12 10:00:17 +08:00
parent 8a0358fd98
commit fb76fbd970
15 changed files with 438 additions and 48 deletions
+106
View File
@@ -397,4 +397,110 @@ describe('AuthzService', () => {
projectId: project.id,
})
})
describe('New project-precedent authz rules', () => {
it('should prioritize project-specific role over team role', async () => {
const user = await prisma.user.create({
data: { name: 'Matt', email: 'matt@example.com', password: 'pass' },
})
const team = await prisma.team.create({ data: { name: 'Team Precedence' } })
const project = await prisma.project.create({
data: { name: 'Project Foo', teamId: team.id },
})
// Matt is a team editor (which would normally give Edit permissions)
const tm = await prisma.teamMember.create({
data: { teamId: team.id, userId: user.id, role: 'editor', scope: 'team' },
})
// But Matt is specifically added to Project Foo as a reviewer (Read-only)
await prisma.projectMember.create({
data: { projectId: project.id, teamMemberId: tm.id, role: 'reviewer' },
})
// Check that Matt has Read permission on Project Foo
await expect(
authzService.hasPermission({
type: ResourceType.Project,
id: project.id,
user,
permission: Permission.Read,
}),
).resolves.toBeUndefined()
// Check that Matt is DENIED Edit permission on Project Foo (even though they are team editor)
await expect(
authzService.hasPermission({
type: ResourceType.Project,
id: project.id,
user,
permission: Permission.Edit,
}),
).rejects.toThrow()
})
it('should fall back to team role if no project-specific role exists', async () => {
const user = await prisma.user.create({
data: { name: 'Jane', email: 'jane@example.com', password: 'pass' },
})
const team = await prisma.team.create({ data: { name: 'Team Fallback' } })
const project = await prisma.project.create({
data: { name: 'Project Bar', teamId: team.id },
})
// Jane is a team editor (scope: team)
await prisma.teamMember.create({
data: { teamId: team.id, userId: user.id, role: 'editor', scope: 'team' },
})
// Jane has no project-specific record on Project Bar, should fall back to team role (editor)
await expect(
authzService.hasPermission({
type: ResourceType.Project,
id: project.id,
user,
permission: Permission.Edit,
}),
).resolves.toBeUndefined()
})
it('should deny project-scoped user access to projects they are not member of', async () => {
const user = await prisma.user.create({
data: { name: 'Guest', email: 'guest@example.com', password: 'pass' },
})
const team = await prisma.team.create({ data: { name: 'Team Guest' } })
const projectA = await prisma.project.create({ data: { name: 'Project A', teamId: team.id } })
const projectB = await prisma.project.create({ data: { name: 'Project B', teamId: team.id } })
// Guest is project-scoped
const tm = await prisma.teamMember.create({
data: { teamId: team.id, userId: user.id, role: 'reviewer', scope: 'project' },
})
// Guest is only added to Project A (as editor)
await prisma.projectMember.create({
data: { projectId: projectA.id, teamMemberId: tm.id, role: 'editor' },
})
// Guest can access Project A
await expect(
authzService.hasPermission({
type: ResourceType.Project,
id: projectA.id,
user,
permission: Permission.Edit,
}),
).resolves.toBeUndefined()
// Guest CANNOT access Project B (denied)
await expect(
authzService.hasPermission({
type: ResourceType.Project,
id: projectB.id,
user,
permission: Permission.Read,
}),
).rejects.toThrow('User has only project scope')
})
})
})
+19 -21
View File
@@ -50,37 +50,35 @@ export class AuthzService {
if (!member) throw new HTTPException(403, { message: 'User is not a member of the team' })
// 2. Check Scope
// 2. Check Project Membership first if project context is available
if (projectId) {
const pm = await prisma.projectMember.findUnique({
where: {
projectIdTeamMemberId: {
projectId: projectId,
teamMemberId: member.id,
},
},
})
if (pm) {
return this.checkRole(pm.role, req.permission)
}
}
// 3. Fall back to Team scope checks
if (member.scope === 'team') {
return this.checkRole(member.role, req.permission)
}
// 3. Scope is Project
// 4. Scope is Project, and no project-level record exists (or no project context was provided)
if (!projectId) {
// User has project scope but no project context provided (e.g. Team-level resource)
// If it's a team-level resource, project-scoped users shouldn't have access except for Read maybe?
// But usually team resources like Skills/Providers are managed by Team Owners/Editors.
// For now, let's keep the logic: project-scoped users can't access team-level resources unless it's Read (like ListProjects).
// If it's a team-level resource, project-scoped users can access Read (like ListProjects).
if (req.permission === Permission.Read) {
return
}
throw new HTTPException(403, { message: 'User has only project scope' })
}
// Check Project Membership
const pm = await prisma.projectMember.findUnique({
where: {
projectIdTeamMemberId: {
projectId: projectId,
teamMemberId: member.id,
},
},
})
if (!pm)
throw new HTTPException(403, { message: `User is not a member of project ${projectId}` })
return this.checkRole(pm.role, req.permission)
throw new HTTPException(403, { message: 'User has only project scope' })
}
private async resolveContext(
+38
View File
@@ -342,6 +342,44 @@ export class ProjectService {
})
}
async getProjectMe(
projectId: string,
userId: string,
): Promise<{ id: string; name: string; role: string; image?: string }> {
const user = await prisma.user.findUnique({
where: { id: userId },
})
if (!user) throw new Error('User not found')
const project = await prisma.project.findUnique({
where: { id: projectId },
})
if (!project) throw new Error('Project not found')
const tm = await prisma.teamMember.findUnique({
where: {
teamIdUserId: { teamId: project.teamId, userId },
},
})
if (!tm) throw new Error('User is not a team member')
const pm = await prisma.projectMember.findFirst({
where: {
projectId,
teamMemberId: tm.id,
},
})
const role = pm ? pm.role : tm.role
return {
id: user.id,
name: user.name,
role: role,
image: await getAvatarUrl(user.image),
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private async toProjectInfo(p: any): Promise<ProjectInfo> {
const pi: ProjectInfo = {