import type { IUser, ICredentialsResponse, IWorkflowDb } from '@/Interface'; import type { CredentialScope, ProjectScope, Scope, WorkflowScope, VariableScope, } from '@n8n/permissions'; import type { Project } from './features/projects/projects.types'; type ExtractAfterColon = T extends `${infer _Prefix}:${infer Suffix}` ? Suffix : never; export type PermissionsMap = { [K in ExtractAfterColon]: boolean; }; const mapScopesToPermissions = (scopes: Scope[], scopeSet: Set): PermissionsMap => scopes.reduce( (permissions: PermissionsMap, scope: Scope) => ({ ...permissions, [scope.split(':')[1]]: scopeSet.has(scope), }), {} as PermissionsMap, ); export const getCredentialPermissions = ( credential: ICredentialsResponse, ): PermissionsMap => mapScopesToPermissions( [ 'credential:create', 'credential:read', 'credential:update', 'credential:delete', 'credential:list', 'credential:share', ], new Set(credential?.scopes ?? []), ); export const getWorkflowPermissions = (workflow: IWorkflowDb): PermissionsMap => mapScopesToPermissions( [ 'workflow:create', 'workflow:read', 'workflow:update', 'workflow:delete', 'workflow:list', 'workflow:share', 'workflow:execute', ], new Set(workflow?.scopes ?? []), ); export const getProjectPermissions = (project: Project | null): PermissionsMap => mapScopesToPermissions( ['project:create', 'project:read', 'project:update', 'project:delete', 'project:list'], new Set(project?.scopes ?? []), ); export const getVariablesPermissions = (user: IUser | null): PermissionsMap => mapScopesToPermissions( ['variable:create', 'variable:read', 'variable:update', 'variable:delete', 'variable:list'], new Set(user?.globalScopes ?? []), );