feat: return sharees when returning a workflow (#4312) (no-changelog)

This commit is contained in:
Omar Ajoue
2022-10-11 16:40:39 +02:00
committed by GitHub
parent 7a2e5bde90
commit ab0f776df1
6 changed files with 227 additions and 62 deletions

View File

@@ -1,5 +1,5 @@
import express from 'express';
import { Db } from '..';
import { Db, ResponseHelper } from '..';
import config from '../../config';
import type { WorkflowRequest } from '../requests';
import { isSharingEnabled, rightDiff } from '../UserManagement/UserManagementHelper';
@@ -58,3 +58,37 @@ EEWorkflowController.put('/:workflowId/share', async (req: WorkflowRequest.Share
return res.status(200).send();
});
EEWorkflowController.get(
'/:id',
(req: WorkflowRequest.Get, res, next) => (req.params.id === 'new' ? next('router') : next()), // skip ee router and use free one for naming
ResponseHelper.send(async (req: WorkflowRequest.Get) => {
const { id: workflowId } = req.params;
if (Number.isNaN(Number(workflowId))) {
throw new ResponseHelper.ResponseError(`Workflow ID must be a number.`, undefined, 400);
}
const workflow = await EEWorkflows.get(
{ id: parseInt(workflowId, 10) },
{ relations: ['shared', 'shared.user', 'shared.role'] },
);
if (!workflow) {
throw new ResponseHelper.ResponseError(
`Workflow with ID "${workflowId}" could not be found.`,
undefined,
404,
);
}
const userSharing = workflow.shared?.find((shared) => shared.user.id === req.user.id);
if (!userSharing && req.user.globalRole.name !== 'owner') {
throw new ResponseHelper.ResponseError(`Forbidden.`, undefined, 403);
}
// @TODO: also return the credentials used by the workflow
return EEWorkflows.addOwnerAndSharings(workflow);
}),
);

View File

@@ -6,6 +6,7 @@ import { WorkflowEntity } from '../databases/entities/WorkflowEntity';
import { RoleService } from '../role/role.service';
import { UserService } from '../user/user.service';
import { WorkflowsService } from './workflows.services';
import { WorkflowWithSharings } from './workflows.types';
export class EEWorkflowsService extends WorkflowsService {
static async isOwned(
@@ -70,4 +71,27 @@ export class EEWorkflowsService extends WorkflowsService {
return transaction.save(newSharedWorkflows);
}
static addOwnerAndSharings(
workflow: WorkflowEntity & WorkflowWithSharings,
): WorkflowEntity & WorkflowWithSharings {
workflow.ownedBy = null;
workflow.sharedWith = [];
workflow.shared?.forEach(({ user, role }) => {
const { id, email, firstName, lastName } = user;
if (role.name === 'owner') {
workflow.ownedBy = { id, email, firstName, lastName };
return;
}
workflow.sharedWith?.push({ id, email, firstName, lastName });
});
// @ts-ignore
delete workflow.shared;
return workflow;
}
}

View File

@@ -2,6 +2,7 @@ import { FindOneOptions, ObjectLiteral } from 'typeorm';
import { Db } from '..';
import { SharedWorkflow } from '../databases/entities/SharedWorkflow';
import { User } from '../databases/entities/User';
import { WorkflowEntity } from '../databases/entities/WorkflowEntity';
export class WorkflowsService {
static async getSharing(
@@ -29,4 +30,8 @@ export class WorkflowsService {
return Db.collections.SharedWorkflow.findOne(options);
}
static async get(workflow: Partial<WorkflowEntity>, options?: { relations: string[] }) {
return Db.collections.Workflow.findOne(workflow, options);
}
}

View File

@@ -0,0 +1,7 @@
import type { IUser } from 'n8n-workflow';
import { WorkflowEntity } from '../databases/entities/WorkflowEntity';
export interface WorkflowWithSharings extends WorkflowEntity {
ownedBy?: IUser | null;
sharedWith?: IUser[];
}