import { Domain } from '@things-factory/shell' import { User } from '@things-factory/auth-base' /** * Label Studio Community Edition 권한 * Community Edition은 Django의 is_superuser 플래그만 사용 */ export interface LabelStudioPermissions { is_superuser: boolean // true: 관리자, false: 일반 사용자 is_staff: boolean // Django admin 페이지 접근 (일반적으로 is_superuser와 동일) is_active: boolean // 활성화 여부 } /** * Things-Factory Role → Label Studio Permissions 매핑 * Community Edition용 (2단계 권한) */ export class LabelStudioRoleMapper { /** * Things-Factory 사용자의 권한을 분석하여 Label Studio 권한 결정 * * 매핑 규칙: * 1. privilege category에 'label-studio'가 포함되어 있으면 → Staff 권한 * 2. user.admin === true 이면 → Admin 권한 * * @param user Things-Factory 사용자 * @returns Label Studio 권한 (is_superuser, is_staff, is_active) */ static async mapUserPermissions(domain: Domain, user: User): Promise { // 사용자의 권한 조회 // 1. Label Studio 관련 권한이 있는지 확인 (category에 'label-studio' 포함) const hasLabelStudioPrivilege = (await User.hasPrivilege('label-studio', 'query', domain, user)) || (await User.hasPrivilege('label-studio', 'mutation', domain, user)) // Label Studio 권한이 없으면 비활성화 if (!hasLabelStudioPrivilege) { return { is_superuser: false, is_staff: false, is_active: false } } // 2. user.owner 플래그 확인 if (user.owner === true) { return { is_superuser: true, is_staff: true, is_active: true } } // 3. Label Studio 권한은 있지만 owner가 아닌 경우 → Staff return { is_superuser: false, is_staff: false, is_active: true } } /** * Label Studio 권한 설명 반환 (디버깅/로깅용) * * @param permissions Label Studio 권한 * @returns 권한 설명 */ static getPermissionsDescription(permissions: LabelStudioPermissions): string { if (!permissions.is_active) { return 'Inactive (No Label Studio access)' } if (permissions.is_superuser) { return 'Admin (Full access)' } return 'Staff (Labeling only)' } }