60 lines
1.1 KiB
TypeScript
60 lines
1.1 KiB
TypeScript
import { prisma } from '@/lib/prisma'
|
|
|
|
export type MaterialShareActor = {
|
|
id: string
|
|
role: string
|
|
isSuperAdmin: boolean
|
|
}
|
|
|
|
export type MaterialShareTarget = {
|
|
userId: string
|
|
ownerType?: string | null
|
|
ownerId?: string | null
|
|
createdByUserId?: string | null
|
|
}
|
|
|
|
export async function getMaterialShareActor(session: unknown): Promise<MaterialShareActor | null> {
|
|
const userId = (session as any)?.user?.id
|
|
if (!userId) {
|
|
return null
|
|
}
|
|
|
|
const user = await prisma.user.findUnique({
|
|
where: { id: userId },
|
|
select: {
|
|
id: true,
|
|
role: true,
|
|
isSuperAdmin: true,
|
|
},
|
|
})
|
|
|
|
if (!user) {
|
|
return null
|
|
}
|
|
|
|
return {
|
|
id: user.id,
|
|
role: user.role,
|
|
isSuperAdmin: Boolean(user.isSuperAdmin),
|
|
}
|
|
}
|
|
|
|
export function canManageMaterialShare(
|
|
material: MaterialShareTarget,
|
|
actor: MaterialShareActor | null
|
|
) {
|
|
if (!actor) {
|
|
return false
|
|
}
|
|
|
|
if (actor.isSuperAdmin || actor.role === 'ADMIN') {
|
|
return true
|
|
}
|
|
|
|
return (
|
|
material.userId === actor.id ||
|
|
material.createdByUserId === actor.id ||
|
|
(material.ownerType === 'USER' && material.ownerId === actor.id)
|
|
)
|
|
}
|