mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 16:44:07 -08:00
wip
This commit is contained in:
parent
909508000f
commit
7fef507da1
|
@ -24,14 +24,12 @@ export class VariablesController {
|
||||||
constructor(private readonly variablesService: VariablesService) {}
|
constructor(private readonly variablesService: VariablesService) {}
|
||||||
|
|
||||||
@Get('/')
|
@Get('/')
|
||||||
@GlobalScope('variable:list')
|
async getVariables(req: AuthenticatedRequest) {
|
||||||
async getVariables() {
|
return await this.variablesService.getAllForUser(req.user);
|
||||||
return await this.variablesService.getAllCached();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('/')
|
@Post('/')
|
||||||
@Licensed('feat:variables')
|
@Licensed('feat:variables')
|
||||||
@GlobalScope('variable:create')
|
|
||||||
async createVariable(
|
async createVariable(
|
||||||
req: AuthenticatedRequest,
|
req: AuthenticatedRequest,
|
||||||
_res: Response,
|
_res: Response,
|
||||||
|
@ -56,7 +54,7 @@ export class VariablesController {
|
||||||
_res: Response,
|
_res: Response,
|
||||||
@Param('variableId') variableId: string,
|
@Param('variableId') variableId: string,
|
||||||
) {
|
) {
|
||||||
const variable = await this.variablesService.getCached(variableId);
|
const variable = await this.variablesService.getForUser(variableId, req.user);
|
||||||
if (variable === null) {
|
if (variable === null) {
|
||||||
throw new NotFoundError(`Variable with id ${variableId} not found`);
|
throw new NotFoundError(`Variable with id ${variableId} not found`);
|
||||||
}
|
}
|
||||||
|
@ -65,7 +63,6 @@ export class VariablesController {
|
||||||
|
|
||||||
@Patch('/:variableId')
|
@Patch('/:variableId')
|
||||||
@Licensed('feat:variables')
|
@Licensed('feat:variables')
|
||||||
@GlobalScope('variable:update')
|
|
||||||
async updateVariable(
|
async updateVariable(
|
||||||
req: AuthenticatedRequest,
|
req: AuthenticatedRequest,
|
||||||
_res: Response,
|
_res: Response,
|
||||||
|
@ -88,7 +85,7 @@ export class VariablesController {
|
||||||
@GlobalScope('variable:delete')
|
@GlobalScope('variable:delete')
|
||||||
async deleteVariable(req: VariablesRequest.Delete) {
|
async deleteVariable(req: VariablesRequest.Delete) {
|
||||||
const id = req.params.id;
|
const id = req.params.id;
|
||||||
await this.variablesService.delete(id);
|
await this.variablesService.delete(id, req.user);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ export class VariablesService {
|
||||||
return (variables as Array<Partial<Variables>>).map((v) => this.variablesRepository.create(v));
|
return (variables as Array<Partial<Variables>>).map((v) => this.variablesRepository.create(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllForUser(id: string, user: User): Promise<Variables[]> {
|
async getAllForUser(user: User): Promise<Variables[]> {
|
||||||
const projects = await this.projectService.getAccessibleProjects(user);
|
const projects = await this.projectService.getAccessibleProjects(user);
|
||||||
const projectIds = projects.map((p) => p.id);
|
const projectIds = projects.map((p) => p.id);
|
||||||
|
|
||||||
|
@ -41,9 +41,7 @@ export class VariablesService {
|
||||||
const canReadGlobalVariables = user.hasGlobalScope('globalVariable:read');
|
const canReadGlobalVariables = user.hasGlobalScope('globalVariable:read');
|
||||||
|
|
||||||
const foundVariables = unfilteredVariables.filter((variable) => {
|
const foundVariables = unfilteredVariables.filter((variable) => {
|
||||||
if (variable.id !== id) {
|
if (!variable.projectId && canReadGlobalVariables) {
|
||||||
return false;
|
|
||||||
} else if (!variable.projectId && canReadGlobalVariables) {
|
|
||||||
return true;
|
return true;
|
||||||
} else if (variable.projectId && projectIds.includes(variable.projectId)) {
|
} else if (variable.projectId && projectIds.includes(variable.projectId)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -93,7 +91,28 @@ export class VariablesService {
|
||||||
return this.variablesRepository.create(foundVariable as Partial<Variables>);
|
return this.variablesRepository.create(foundVariable as Partial<Variables>);
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(id: string): Promise<void> {
|
async delete(id: string, user: User): Promise<void> {
|
||||||
|
const originalVariable = await this.variablesRepository.findOneOrFail({
|
||||||
|
where: {
|
||||||
|
id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Updating a global variable
|
||||||
|
if (!originalVariable.projectId && !user.hasGlobalScope('globalVariable:delete')) {
|
||||||
|
throw new MissingScopeError();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Updating a project variable
|
||||||
|
if (
|
||||||
|
originalVariable.projectId &&
|
||||||
|
!(await this.projectService.getProjectWithScope(user, originalVariable.projectId, [
|
||||||
|
'variable:delete',
|
||||||
|
]))
|
||||||
|
) {
|
||||||
|
throw new MissingScopeError();
|
||||||
|
}
|
||||||
|
|
||||||
await this.variablesRepository.delete(id);
|
await this.variablesRepository.delete(id);
|
||||||
await this.updateCache();
|
await this.updateCache();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue