n8n/packages/cli/src/environments/variables/variables.controller.ee.ts
Csaba Tuncsik 596c472ecc
feat: RBAC (#8922)
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
Co-authored-by: Val <68596159+valya@users.noreply.github.com>
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
Co-authored-by: Valya Bullions <valya@n8n.io>
Co-authored-by: Danny Martini <danny@n8n.io>
Co-authored-by: Danny Martini <despair.blue@gmail.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: oleg <me@olegivaniv.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: Elias Meire <elias@meire.dev>
Co-authored-by: Giulio Andreini <andreini@netseven.it>
Co-authored-by: Giulio Andreini <g.andreini@gmail.com>
Co-authored-by: Ayato Hayashi <go12limchangyong@gmail.com>
2024-05-17 10:53:15 +02:00

76 lines
2.3 KiB
TypeScript

import { VariablesRequest } from '@/requests';
import { Delete, Get, GlobalScope, Licensed, Patch, Post, RestController } from '@/decorators';
import { VariablesService } from './variables.service.ee';
import { BadRequestError } from '@/errors/response-errors/bad-request.error';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { VariableValidationError } from '@/errors/variable-validation.error';
import { VariableCountLimitReachedError } from '@/errors/variable-count-limit-reached.error';
@RestController('/variables')
export class VariablesController {
constructor(private readonly variablesService: VariablesService) {}
@Get('/')
@GlobalScope('variable:list')
async getVariables() {
return await this.variablesService.getAllCached();
}
@Post('/')
@Licensed('feat:variables')
@GlobalScope('variable:create')
async createVariable(req: VariablesRequest.Create) {
const variable = req.body;
delete variable.id;
try {
return await this.variablesService.create(variable);
} catch (error) {
if (error instanceof VariableCountLimitReachedError) {
throw new BadRequestError(error.message);
} else if (error instanceof VariableValidationError) {
throw new BadRequestError(error.message);
}
throw error;
}
}
@Get('/:id')
@GlobalScope('variable:read')
async getVariable(req: VariablesRequest.Get) {
const id = req.params.id;
const variable = await this.variablesService.getCached(id);
if (variable === null) {
throw new NotFoundError(`Variable with id ${req.params.id} not found`);
}
return variable;
}
@Patch('/:id')
@Licensed('feat:variables')
@GlobalScope('variable:update')
async updateVariable(req: VariablesRequest.Update) {
const id = req.params.id;
const variable = req.body;
delete variable.id;
try {
return await this.variablesService.update(id, variable);
} catch (error) {
if (error instanceof VariableCountLimitReachedError) {
throw new BadRequestError(error.message);
} else if (error instanceof VariableValidationError) {
throw new BadRequestError(error.message);
}
throw error;
}
}
@Delete('/:id(\\w+)')
@GlobalScope('variable:delete')
async deleteVariable(req: VariablesRequest.Delete) {
const id = req.params.id;
await this.variablesService.delete(id);
return true;
}
}