mirror of
https://github.com/n8n-io/n8n.git
synced 2026-09-19 01:45:48 +08:00
- Fix autofixable violations - Remove unused directives - Allow for PascalCased variables - needed for dynamically imported or assigned classes, decorators, routers, etc.
80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
import express from 'express';
|
|
import { Container } from 'typedi';
|
|
|
|
import * as ResponseHelper from '@/ResponseHelper';
|
|
import type { VariablesRequest } from '@/requests';
|
|
import {
|
|
VariablesLicenseError,
|
|
EEVariablesService,
|
|
VariablesValidationError,
|
|
} from './variables.service.ee';
|
|
import { isVariablesEnabled } from './enviromentHelpers';
|
|
import { Logger } from '@/Logger';
|
|
|
|
export const EEVariablesController = express.Router();
|
|
|
|
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') {
|
|
Container.get(Logger).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 Container.get(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(\\w+)',
|
|
ResponseHelper.send(async (req: VariablesRequest.Update) => {
|
|
const id = req.params.id;
|
|
if (req.user.globalRole.name !== 'owner') {
|
|
Container.get(Logger).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 Container.get(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;
|
|
}
|
|
}),
|
|
);
|