Skip to content

Commit

Permalink
remove unused parameter from RequireGlobalScope
Browse files Browse the repository at this point in the history
  • Loading branch information
valya committed Nov 28, 2023
1 parent 9e93182 commit 7d491b2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
11 changes: 4 additions & 7 deletions packages/cli/src/decorators/Scopes.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import type { Scope, ScopeOptions } from '@n8n/permissions';
import type { Scope } from '@n8n/permissions';
import type { ScopeMetadata } from './types';
import { CONTROLLER_REQUIRED_SCOPES } from './constants';

export const RequireGlobalScope = (scope: Scope | Scope[], scopeOptions?: ScopeOptions) => {
export const RequireGlobalScope = (scope: Scope | Scope[]) => {
// eslint-disable-next-line @typescript-eslint/ban-types
return (target: Function | object, handlerName?: string) => {
const controllerClass = handlerName ? target.constructor : target;
const scopes = (Reflect.getMetadata(CONTROLLER_REQUIRED_SCOPES, controllerClass) ??
{}) as ScopeMetadata;
scopes[handlerName ?? '*'] = {
scopes: Array.isArray(scope) ? scope : [scope],
options: scopeOptions,
};
[]) as ScopeMetadata;
scopes[handlerName ?? '*'] = Array.isArray(scope) ? scope : [scope];
Reflect.defineMetadata(CONTROLLER_REQUIRED_SCOPES, scopes, controllerClass);
};
};
8 changes: 4 additions & 4 deletions packages/cli/src/decorators/registerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ import type {
MiddlewareMetadata,
RouteMetadata,
ScopeMetadata,
ScopeWithOptions,
} from './types';
import type { BooleanLicenseFeature } from '@/Interfaces';
import Container from 'typedi';
import { License } from '@/License';
import type { Scope } from '@n8n/permissions';

export const createAuthMiddleware =
(authRole: AuthRole): RequestHandler =>
Expand Down Expand Up @@ -59,15 +59,15 @@ export const createLicenseMiddleware =
};

export const createGlobalScopeMiddleware =
(scopes: ScopeWithOptions): RequestHandler =>
(scopes: Scope[]): RequestHandler =>
async ({ user }: AuthenticatedRequest, res, next) => {
if (scopes.scopes.length === 0) {
if (scopes.length === 0) {
return next();
}

if (!user) return res.status(401).json({ status: 'error', message: 'Unauthorized' });

const hasScopes = await user.hasGlobalScope(scopes.scopes, scopes.options);
const hasScopes = await user.hasGlobalScope(scopes);
if (!hasScopes) {
return res.status(403).json({ status: 'error', message: 'Unauthorized' });
}
Expand Down
8 changes: 2 additions & 6 deletions packages/cli/src/decorators/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Request, Response, RequestHandler } from 'express';
import type { RoleNames, RoleScopes } from '@db/entities/Role';
import type { BooleanLicenseFeature } from '@/Interfaces';
import type { ScopeOptions, Scope } from '@n8n/permissions';
import type { Scope } from '@n8n/permissions';

export type Method = 'get' | 'post' | 'put' | 'patch' | 'delete';

Expand All @@ -10,11 +10,7 @@ export type AuthRoleMetadata = Record<string, AuthRole>;

export type LicenseMetadata = Record<string, BooleanLicenseFeature[]>;

export interface ScopeWithOptions {
scopes: Scope[];
options?: ScopeOptions;
}
export type ScopeMetadata = Record<string, ScopeWithOptions>;
export type ScopeMetadata = Record<string, Scope[]>;

export interface MiddlewareMetadata {
handlerName: string;
Expand Down

0 comments on commit 7d491b2

Please sign in to comment.