mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 00:54:06 -08:00
33 lines
902 B
TypeScript
33 lines
902 B
TypeScript
import type { Scope } from '@n8n/permissions';
|
|
import { RESOURCES } from '@n8n/permissions';
|
|
|
|
type ExtractScopePrefixSuffix<T> = T extends `${infer Prefix}:${infer Suffix}`
|
|
? [Prefix, Suffix]
|
|
: never;
|
|
type ActionBooleans<T extends readonly string[]> = {
|
|
[K in T[number]]?: boolean;
|
|
};
|
|
export type PermissionsRecord = {
|
|
[K in keyof typeof RESOURCES]: ActionBooleans<(typeof RESOURCES)[K]>;
|
|
};
|
|
|
|
export const getResourcePermissions = (resourceScopes: Scope[] = []): PermissionsRecord =>
|
|
Object.keys(RESOURCES).reduce(
|
|
(permissions, key) => ({
|
|
...permissions,
|
|
[key]: resourceScopes.reduce((resourcePermissions, scope) => {
|
|
const [prefix, suffix] = scope.split(':') as ExtractScopePrefixSuffix<Scope>;
|
|
|
|
if (prefix === key) {
|
|
return {
|
|
...resourcePermissions,
|
|
[suffix]: true,
|
|
};
|
|
}
|
|
|
|
return resourcePermissions;
|
|
}, {}),
|
|
}),
|
|
{} as PermissionsRecord,
|
|
);
|