refactor(core): Sort variables files under variables folder (#6051)

sort variables files under variables folder
This commit is contained in:
Michael Auerswald
2023-04-21 13:08:16 +02:00
committed by GitHub
parent 444ed1bf0e
commit da31925083
8 changed files with 3 additions and 21 deletions

View File

@@ -0,0 +1,26 @@
import { License } from '@/License';
import Container from 'typedi';
export function isVariablesEnabled(): boolean {
const license = Container.get(License);
return license.isVariablesEnabled();
}
export function canCreateNewVariable(variableCount: number): boolean {
if (!isVariablesEnabled()) {
return false;
}
const license = Container.get(License);
// This defaults to -1 which is what we want if we've enabled
// variables via the config
const limit = license.getVariablesLimit();
if (limit === -1) {
return true;
}
return limit > variableCount;
}
export function getVariablesLimit(): number {
const license = Container.get(License);
return license.getVariablesLimit();
}

View File

@@ -0,0 +1,79 @@
import express from 'express';
import { LoggerProxy } from 'n8n-workflow';
import * as ResponseHelper from '@/ResponseHelper';
import type { VariablesRequest } from '@/requests';
import {
VariablesLicenseError,
EEVariablesService,
VariablesValidationError,
} from './variables.service.ee';
import { isVariablesEnabled } from './enviromentHelpers';
// eslint-disable-next-line @typescript-eslint/naming-convention
export const EEVariablesController = express.Router();
/**
* Initialize Logger if needed
*/
EEVariablesController.use((req, res, next) => {
if (!isVariablesEnabled()) {
next('router');
return;
}
next();
});
EEVariablesController.post(
'/',
ResponseHelper.send(async (req: VariablesRequest.Create) => {
if (req.user.globalRole.name !== 'owner') {
LoggerProxy.info('Attempt to update a variable blocked due to lack of permissions', {
userId: req.user.id,
});
throw new ResponseHelper.AuthError('Unauthorized');
}
const variable = req.body;
delete variable.id;
try {
return await EEVariablesService.create(variable);
} catch (error) {
if (error instanceof VariablesLicenseError) {
throw new ResponseHelper.BadRequestError(error.message);
} else if (error instanceof VariablesValidationError) {
throw new ResponseHelper.BadRequestError(error.message);
}
throw error;
}
}),
);
EEVariablesController.patch(
'/:id(\\d+)',
ResponseHelper.send(async (req: VariablesRequest.Update) => {
const id = parseInt(req.params.id);
if (isNaN(id)) {
throw new ResponseHelper.BadRequestError('Invalid variable id ' + req.params.id);
}
if (req.user.globalRole.name !== 'owner') {
LoggerProxy.info('Attempt to update a variable blocked due to lack of permissions', {
id,
userId: req.user.id,
});
throw new ResponseHelper.AuthError('Unauthorized');
}
const variable = req.body;
delete variable.id;
try {
return await EEVariablesService.update(id, variable);
} catch (error) {
if (error instanceof VariablesLicenseError) {
throw new ResponseHelper.BadRequestError(error.message);
} else if (error instanceof VariablesValidationError) {
throw new ResponseHelper.BadRequestError(error.message);
}
throw error;
}
}),
);

View File

@@ -0,0 +1,82 @@
import express from 'express';
import { LoggerProxy } from 'n8n-workflow';
import { getLogger } from '@/Logger';
import * as ResponseHelper from '@/ResponseHelper';
import type { VariablesRequest } from '@/requests';
import { VariablesService } from './variables.service';
import { EEVariablesController } from './variables.controller.ee';
export const variablesController = express.Router();
variablesController.use('/', EEVariablesController);
/**
* Initialize Logger if needed
*/
variablesController.use((req, res, next) => {
try {
LoggerProxy.getInstance();
} catch (error) {
LoggerProxy.init(getLogger());
}
next();
});
variablesController.use(EEVariablesController);
variablesController.get(
'/',
ResponseHelper.send(async () => {
return VariablesService.getAll();
}),
);
variablesController.post(
'/',
ResponseHelper.send(async () => {
throw new ResponseHelper.BadRequestError('No variables license found');
}),
);
variablesController.get(
'/:id(\\d+)',
ResponseHelper.send(async (req: VariablesRequest.Get) => {
const id = parseInt(req.params.id);
if (isNaN(id)) {
throw new ResponseHelper.BadRequestError('Invalid variable id ' + req.params.id);
}
const variable = await VariablesService.get(id);
if (variable === null) {
throw new ResponseHelper.NotFoundError(`Variable with id ${req.params.id} not found`);
}
return variable;
}),
);
variablesController.patch(
'/:id(\\d+)',
ResponseHelper.send(async () => {
throw new ResponseHelper.BadRequestError('No variables license found');
}),
);
variablesController.delete(
'/:id(\\d+)',
ResponseHelper.send(async (req: VariablesRequest.Delete) => {
const id = parseInt(req.params.id);
if (isNaN(id)) {
throw new ResponseHelper.BadRequestError('Invalid variable id ' + req.params.id);
}
if (req.user.globalRole.name !== 'owner') {
LoggerProxy.info('Attempt to delete a variable blocked due to lack of permissions', {
id,
userId: req.user.id,
});
throw new ResponseHelper.AuthError('Unauthorized');
}
await VariablesService.delete(id);
return true;
}),
);

View File

@@ -0,0 +1,45 @@
import type { Variables } from '@/databases/entities/Variables';
import { collections } from '@/Db';
import { InternalHooks } from '@/InternalHooks';
import Container from 'typedi';
import { canCreateNewVariable } from './enviromentHelpers';
import { VariablesService } from './variables.service';
export class VariablesLicenseError extends Error {}
export class VariablesValidationError extends Error {}
export class EEVariablesService extends VariablesService {
static async getCount(): Promise<number> {
return collections.Variables.count();
}
static validateVariable(variable: Omit<Variables, 'id'>): void {
if (variable.key.length > 50) {
throw new VariablesValidationError('key cannot be longer than 50 characters');
}
if (variable.key.replace(/[A-Za-z0-9_]/g, '').length !== 0) {
throw new VariablesValidationError('key can only contain characters A-Za-z0-9_');
}
if (variable.value.length > 255) {
throw new VariablesValidationError('value cannot be longer than 255 characters');
}
}
static async create(variable: Omit<Variables, 'id'>): Promise<Variables> {
if (!canCreateNewVariable(await this.getCount())) {
throw new VariablesLicenseError('Variables limit reached');
}
this.validateVariable(variable);
void Container.get(InternalHooks).onVariableCreated({ variable_type: variable.type });
return collections.Variables.save(variable);
}
static async update(id: number, variable: Omit<Variables, 'id'>): Promise<Variables> {
this.validateVariable(variable);
await collections.Variables.update(id, variable);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return (await this.get(id))!;
}
}

View File

@@ -0,0 +1,20 @@
import type { Variables } from '@/databases/entities/Variables';
import { collections } from '@/Db';
export class VariablesService {
static async getAll(): Promise<Variables[]> {
return collections.Variables.find();
}
static async getCount(): Promise<number> {
return collections.Variables.count();
}
static async get(id: number): Promise<Variables | null> {
return collections.Variables.findOne({ where: { id } });
}
static async delete(id: number): Promise<void> {
await collections.Variables.delete(id);
}
}