gongxiangp2/lib/material-authorization.ts

38 lines
778 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
isSuperAdmin,
isSystemAdmin,
} from '@/lib/department-permission'
export interface MaterialAuthorizationUser {
id: string
role?: string | null
isSuperAdmin?: boolean | null
}
export interface MaterialAuthorizationTarget {
userId: string
}
/**
* 素材写操作的最小统一授权:
* - 素材所有者
* - 系统管理员(兼容 ADMIN / SYSTEM_ADMIN)
* - 超级管理员
*
* 部门查看权限不能自动升级为素材写权限。
*/
export async function canManageMaterial(
user: MaterialAuthorizationUser,
material: MaterialAuthorizationTarget,
): Promise<boolean> {
if (
material.userId === user.id ||
isSystemAdmin(user.role) ||
user.isSuperAdmin === true
) {
return true
}
return isSuperAdmin(user.id)
}