Files
Automata/packages/@n8n/permissions/src/hasScope.ts
Val 0468ded0db feat: Add permissions package (no-changelog) (#7650)
Github issue / Community forum post (link here to close automatically):
2023-11-08 15:42:40 +00:00

44 lines
1.0 KiB
TypeScript

import type { Scope, ScopeLevels } from './types';
export type HasScopeMode = 'oneOf' | 'allOf';
export interface HasScopeOptions {
mode: HasScopeMode;
}
export function hasScope(
scope: Scope | Scope[],
userScopes: ScopeLevels,
options?: HasScopeOptions,
): boolean;
export function hasScope(
scope: Scope | Scope[],
userScopes: Pick<ScopeLevels, 'global'>,
options?: HasScopeOptions,
): boolean;
export function hasScope(
scope: Scope | Scope[],
userScopes: Omit<ScopeLevels, 'resource'>,
options?: HasScopeOptions,
): boolean;
export function hasScope(
scope: Scope | Scope[],
userScopes: Pick<ScopeLevels, 'global'> & Partial<ScopeLevels>,
options: HasScopeOptions = { mode: 'oneOf' },
): boolean {
if (!Array.isArray(scope)) {
scope = [scope];
}
const userScopeSet = new Set([
...userScopes.global,
...(userScopes.project ?? []),
...(userScopes.resource ?? []),
]);
if (options.mode === 'allOf') {
return scope.every((s) => userScopeSet.has(s));
}
return scope.some((s) => userScopeSet.has(s));
}