mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 05:50:32 +00:00
feat(members): allow owners to manage roles and remove members
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { prisma } from '@shumai/db'
|
||||
import type { Prisma } from '@shumai/db'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
|
||||
type User = Prisma.UserGetPayload<Record<string, never>>
|
||||
|
||||
export enum Permission {
|
||||
@@ -32,7 +34,7 @@ export interface AuthzRequest {
|
||||
|
||||
export class AuthzService {
|
||||
async hasPermission(req: AuthzRequest): Promise<void> {
|
||||
if (!req.user) throw new Error('User is required')
|
||||
if (!req.user) throw new HTTPException(401, { message: 'User is required' })
|
||||
|
||||
const { teamId, projectId } = await this.resolveContext(req.type, req.id)
|
||||
|
||||
@@ -46,7 +48,7 @@ export class AuthzService {
|
||||
},
|
||||
})
|
||||
|
||||
if (!member) throw new Error('User is not a member of the team')
|
||||
if (!member) throw new HTTPException(403, { message: 'User is not a member of the team' })
|
||||
|
||||
// 2. Check Scope
|
||||
if (member.scope === 'team') {
|
||||
@@ -62,7 +64,7 @@ export class AuthzService {
|
||||
if (req.permission === Permission.Read) {
|
||||
return
|
||||
}
|
||||
throw new Error('User has only project scope')
|
||||
throw new HTTPException(403, { message: 'User has only project scope' })
|
||||
}
|
||||
|
||||
// Check Project Membership
|
||||
@@ -75,7 +77,7 @@ export class AuthzService {
|
||||
},
|
||||
})
|
||||
|
||||
if (!pm) throw new Error(`User is not a member of project ${projectId}`)
|
||||
if (!pm) throw new HTTPException(403, { message: `User is not a member of project ${projectId}` })
|
||||
|
||||
return this.checkRole(pm.role, req.permission)
|
||||
}
|
||||
@@ -93,7 +95,7 @@ export class AuthzService {
|
||||
where: { id },
|
||||
select: { teamId: true },
|
||||
})
|
||||
if (!proj) throw new Error('Project not found')
|
||||
if (!proj) throw new HTTPException(404, { message: 'Project not found' })
|
||||
return { teamId: proj.teamId, projectId: id }
|
||||
}
|
||||
|
||||
@@ -106,7 +108,7 @@ export class AuthzService {
|
||||
project: { select: { id: true, teamId: true } },
|
||||
},
|
||||
})
|
||||
if (!asset) throw new Error('Asset not found')
|
||||
if (!asset) throw new HTTPException(404, { message: 'Asset not found' })
|
||||
if (asset.project) {
|
||||
return { teamId: asset.project.teamId, projectId: asset.project.id }
|
||||
}
|
||||
@@ -120,7 +122,7 @@ export class AuthzService {
|
||||
// For now, let's try to get context from parent.
|
||||
return this.resolveContext(ResourceType.Asset, asset.parentId)
|
||||
}
|
||||
throw new Error('Could not resolve context for asset')
|
||||
throw new HTTPException(403, { message: 'Could not resolve context for asset' })
|
||||
}
|
||||
|
||||
case ResourceType.Collection: {
|
||||
@@ -128,7 +130,7 @@ export class AuthzService {
|
||||
where: { id },
|
||||
include: { project: true },
|
||||
})
|
||||
if (!coll) throw new Error('Collection not found')
|
||||
if (!coll) throw new HTTPException(404, { message: 'Collection not found' })
|
||||
return { teamId: coll.project.teamId, projectId: coll.projectId }
|
||||
}
|
||||
|
||||
@@ -137,7 +139,7 @@ export class AuthzService {
|
||||
where: { id },
|
||||
select: { teamId: true },
|
||||
})
|
||||
if (!agent) throw new Error('Agent not found')
|
||||
if (!agent) throw new HTTPException(404, { message: 'Agent not found' })
|
||||
return { teamId: agent.teamId }
|
||||
}
|
||||
|
||||
@@ -146,7 +148,7 @@ export class AuthzService {
|
||||
where: { id },
|
||||
include: { agent: true },
|
||||
})
|
||||
if (!session) throw new Error('Agent session not found')
|
||||
if (!session) throw new HTTPException(404, { message: 'Agent session not found' })
|
||||
return { teamId: session.agent.teamId }
|
||||
}
|
||||
|
||||
@@ -155,7 +157,7 @@ export class AuthzService {
|
||||
where: { id },
|
||||
include: { project: true },
|
||||
})
|
||||
if (!share) throw new Error('Share not found')
|
||||
if (!share) throw new HTTPException(404, { message: 'Share not found' })
|
||||
return { teamId: share.project.teamId, projectId: share.projectId }
|
||||
}
|
||||
|
||||
@@ -164,7 +166,7 @@ export class AuthzService {
|
||||
where: { key: id },
|
||||
select: { teamId: true, projectId: true },
|
||||
})
|
||||
if (!field) throw new Error('Metadata field not found')
|
||||
if (!field) throw new HTTPException(404, { message: 'Metadata field not found' })
|
||||
if (field.projectId) {
|
||||
const proj = await prisma.project.findUnique({
|
||||
where: { id: field.projectId },
|
||||
@@ -172,7 +174,7 @@ export class AuthzService {
|
||||
})
|
||||
return { teamId: proj!.teamId, projectId: field.projectId }
|
||||
}
|
||||
if (!field.teamId) throw new Error('Metadata field has no context')
|
||||
if (!field.teamId) throw new HTTPException(403, { message: 'Metadata field has no context' })
|
||||
return { teamId: field.teamId }
|
||||
}
|
||||
|
||||
@@ -181,7 +183,7 @@ export class AuthzService {
|
||||
where: { id },
|
||||
select: { teamId: true },
|
||||
})
|
||||
if (!skill) throw new Error('Skill not found')
|
||||
if (!skill) throw new HTTPException(404, { message: 'Skill not found' })
|
||||
return { teamId: skill.teamId }
|
||||
}
|
||||
|
||||
@@ -190,7 +192,7 @@ export class AuthzService {
|
||||
where: { id },
|
||||
select: { teamId: true },
|
||||
})
|
||||
if (!provider) throw new Error('Provider not found')
|
||||
if (!provider) throw new HTTPException(404, { message: 'Provider not found' })
|
||||
return { teamId: provider.teamId }
|
||||
}
|
||||
|
||||
@@ -199,7 +201,7 @@ export class AuthzService {
|
||||
where: { id },
|
||||
select: { teamId: true, projectId: true },
|
||||
})
|
||||
if (!invite) throw new Error('Invite not found')
|
||||
if (!invite) throw new HTTPException(404, { message: 'Invite not found' })
|
||||
return { teamId: invite.teamId, projectId: invite.projectId ?? undefined }
|
||||
}
|
||||
|
||||
@@ -208,13 +210,13 @@ export class AuthzService {
|
||||
where: { id },
|
||||
include: { asset: { include: { project: true } } },
|
||||
})
|
||||
if (!comment) throw new Error('Comment not found')
|
||||
if (!comment.asset.project) throw new Error('Comment asset has no project')
|
||||
if (!comment) throw new HTTPException(404, { message: 'Comment not found' })
|
||||
if (!comment.asset.project) throw new HTTPException(403, { message: 'Comment asset has no project' })
|
||||
return { teamId: comment.asset.project.teamId, projectId: comment.asset.projectId! }
|
||||
}
|
||||
|
||||
default:
|
||||
throw new Error(`Unsupported resource type: ${type}`)
|
||||
throw new HTTPException(403, { message: `Unsupported resource type: ${type}` })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,7 +231,7 @@ export class AuthzService {
|
||||
if (role === 'owner') return
|
||||
break
|
||||
}
|
||||
throw new Error('Permission denied')
|
||||
throw new HTTPException(403, { message: 'Permission denied' })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
ServiceListProjectsRequest,
|
||||
ServiceAddProjectMemberRequest,
|
||||
ServiceListProjectMembersRequest,
|
||||
ServiceUpdateProjectMemberRoleRequest,
|
||||
ProjectInfo,
|
||||
ProjectUserInfo,
|
||||
} from '@shumai/dtos'
|
||||
@@ -303,6 +304,43 @@ export class ProjectService {
|
||||
return project.teamId
|
||||
}
|
||||
|
||||
async updateMemberRole(req: ServiceUpdateProjectMemberRoleRequest): Promise<void> {
|
||||
const member = await prisma.projectMember.findFirst({
|
||||
where: {
|
||||
projectId: req.projectId,
|
||||
teamMember: {
|
||||
userId: req.userId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!member) throw new Error('Member not found')
|
||||
if (member.role === 'owner') throw new Error('Cannot change the role of an owner')
|
||||
|
||||
await prisma.projectMember.update({
|
||||
where: { id: member.id },
|
||||
data: { role: req.role },
|
||||
})
|
||||
}
|
||||
|
||||
async removeMember(projectId: string, userId: string): Promise<void> {
|
||||
const member = await prisma.projectMember.findFirst({
|
||||
where: {
|
||||
projectId: projectId,
|
||||
teamMember: {
|
||||
userId: userId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!member) throw new Error('Member not found')
|
||||
if (member.role === 'owner') throw new Error('Cannot remove an owner')
|
||||
|
||||
await prisma.projectMember.delete({
|
||||
where: { id: member.id },
|
||||
})
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
private async toProjectInfo(p: any): Promise<ProjectInfo> {
|
||||
const pi: ProjectInfo = {
|
||||
|
||||
@@ -17,9 +17,7 @@ export class SearchService {
|
||||
|
||||
async search(folderId: string, req: SearchRequest): Promise<PaginatedData<AssetInfo[]>> {
|
||||
const targetFolderIds =
|
||||
req.recursively === false
|
||||
? [folderId]
|
||||
: await this.assetSvc.getDescendantFolderIds(folderId)
|
||||
req.recursively === false ? [folderId] : await this.assetSvc.getDescendantFolderIds(folderId)
|
||||
|
||||
const targetTypes =
|
||||
req.assetType === 'folder' ? [AssetType.folder] : [AssetType.file, AssetType.version_stack]
|
||||
|
||||
@@ -4,12 +4,14 @@ import { PaginatedData, paginateQuery } from '@shumai/core/src/pagination'
|
||||
import { ProviderService, providerService } from '@shumai/core/src/provider/provider'
|
||||
import { notificationService } from '@shumai/core/src/notification/notification'
|
||||
import { getAvatarUrl } from '@shumai/core/src/user/avatar'
|
||||
import { HTTPException } from 'hono/http-exception'
|
||||
import {
|
||||
ServiceCreateTeamRequest,
|
||||
ServiceGetUserTeamsRequest,
|
||||
ServiceJoinTeamRequest,
|
||||
ServiceGetMeRequest,
|
||||
ServiceGetTeamMembersRequest,
|
||||
ServiceUpdateTeamMemberRoleRequest,
|
||||
TeamInfo,
|
||||
GetMeResponse,
|
||||
UserInfo,
|
||||
@@ -140,7 +142,7 @@ export class TeamService {
|
||||
include: { user: true },
|
||||
})
|
||||
|
||||
if (!member) throw new Error('user is not a team member')
|
||||
if (!member) throw new HTTPException(403, { message: 'user is not a team member' })
|
||||
|
||||
const unreadNotificationCount = await notificationService.getUnreadCount(
|
||||
req.teamId,
|
||||
@@ -207,12 +209,50 @@ export class TeamService {
|
||||
)
|
||||
}
|
||||
|
||||
async updateMemberRole(req: ServiceUpdateTeamMemberRoleRequest): Promise<void> {
|
||||
const member = await prisma.teamMember.findUnique({
|
||||
where: {
|
||||
teamIdUserId: {
|
||||
teamId: req.teamId,
|
||||
userId: req.userId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!member) throw new HTTPException(404, { message: 'Member not found' })
|
||||
if (member.role === 'owner')
|
||||
throw new HTTPException(403, { message: 'Cannot change the role of an owner' })
|
||||
|
||||
await prisma.teamMember.update({
|
||||
where: { id: member.id },
|
||||
data: { role: req.role },
|
||||
})
|
||||
}
|
||||
|
||||
async removeMember(teamId: string, userId: string): Promise<void> {
|
||||
const member = await prisma.teamMember.findUnique({
|
||||
where: {
|
||||
teamIdUserId: {
|
||||
teamId: teamId,
|
||||
userId: userId,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!member) throw new HTTPException(404, { message: 'Member not found' })
|
||||
if (member.role === 'owner') throw new HTTPException(403, { message: 'Cannot remove an owner' })
|
||||
|
||||
await prisma.teamMember.delete({
|
||||
where: { id: member.id },
|
||||
})
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
async getSettings(teamId: string): Promise<any> {
|
||||
const team = await prisma.team.findUnique({
|
||||
where: { id: teamId },
|
||||
})
|
||||
if (!team) throw new Error('team not found')
|
||||
if (!team) throw new HTTPException(404, { message: 'team not found' })
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const settings = (team.settings || {}) as any
|
||||
|
||||
@@ -227,7 +267,7 @@ export class TeamService {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
async updateSettings(teamId: string, key: string, value: any): Promise<any> {
|
||||
const team = await prisma.team.findUnique({ where: { id: teamId } })
|
||||
if (!team) throw new Error('team not found')
|
||||
if (!team) throw new HTTPException(404, { message: 'team not found' })
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const settings = (team.settings || {}) as any
|
||||
|
||||
Reference in New Issue
Block a user