2022-10-11 05:55:05 -07:00
|
|
|
import { DeleteResult, EntityManager, In, Not } from 'typeorm';
|
2022-11-09 06:25:00 -08:00
|
|
|
import * as Db from '@/Db';
|
|
|
|
import * as ResponseHelper from '@/ResponseHelper';
|
|
|
|
import * as WorkflowHelpers from '@/WorkflowHelpers';
|
|
|
|
import { ICredentialsDb } from '@/Interfaces';
|
|
|
|
import { SharedWorkflow } from '@db/entities/SharedWorkflow';
|
|
|
|
import { User } from '@db/entities/User';
|
|
|
|
import { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
|
|
|
import { RoleService } from '@/role/role.service';
|
|
|
|
import { UserService } from '@/user/user.service';
|
2022-10-11 05:55:05 -07:00
|
|
|
import { WorkflowsService } from './workflows.services';
|
2022-10-31 07:08:25 -07:00
|
|
|
import type { WorkflowWithSharingsAndCredentials } from './workflows.types';
|
2022-11-09 06:25:00 -08:00
|
|
|
import { EECredentialsService as EECredentials } from '@/credentials/credentials.service.ee';
|
2022-10-11 05:55:05 -07:00
|
|
|
|
|
|
|
export class EEWorkflowsService extends WorkflowsService {
|
|
|
|
static async isOwned(
|
|
|
|
user: User,
|
|
|
|
workflowId: string,
|
|
|
|
): Promise<{ ownsWorkflow: boolean; workflow?: WorkflowEntity }> {
|
|
|
|
const sharing = await this.getSharing(user, workflowId, ['workflow', 'role'], {
|
|
|
|
allowGlobalOwner: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!sharing || sharing.role.name !== 'owner') return { ownsWorkflow: false };
|
|
|
|
|
|
|
|
const { workflow } = sharing;
|
|
|
|
|
|
|
|
return { ownsWorkflow: true, workflow };
|
|
|
|
}
|
|
|
|
|
|
|
|
static async getSharings(
|
|
|
|
transaction: EntityManager,
|
|
|
|
workflowId: string,
|
|
|
|
): Promise<SharedWorkflow[]> {
|
|
|
|
const workflow = await transaction.findOne(WorkflowEntity, workflowId, {
|
|
|
|
relations: ['shared'],
|
|
|
|
});
|
|
|
|
return workflow?.shared ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
static async pruneSharings(
|
|
|
|
transaction: EntityManager,
|
|
|
|
workflowId: string,
|
|
|
|
userIds: string[],
|
|
|
|
): Promise<DeleteResult> {
|
|
|
|
return transaction.delete(SharedWorkflow, {
|
|
|
|
workflow: { id: workflowId },
|
|
|
|
user: { id: Not(In(userIds)) },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static async share(
|
|
|
|
transaction: EntityManager,
|
|
|
|
workflow: WorkflowEntity,
|
|
|
|
shareWithIds: string[],
|
|
|
|
): Promise<SharedWorkflow[]> {
|
|
|
|
const [users, role] = await Promise.all([
|
|
|
|
UserService.getByIds(transaction, shareWithIds),
|
|
|
|
RoleService.trxGet(transaction, { scope: 'workflow', name: 'editor' }),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const newSharedWorkflows = users.reduce<SharedWorkflow[]>((acc, user) => {
|
|
|
|
if (user.isPending) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
acc.push(
|
|
|
|
Db.collections.SharedWorkflow.create({
|
|
|
|
workflow,
|
|
|
|
user,
|
|
|
|
role,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
return acc;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return transaction.save(newSharedWorkflows);
|
|
|
|
}
|
2022-10-11 07:40:39 -07:00
|
|
|
|
|
|
|
static addOwnerAndSharings(
|
2022-10-31 07:08:25 -07:00
|
|
|
workflow: WorkflowWithSharingsAndCredentials,
|
|
|
|
): WorkflowWithSharingsAndCredentials {
|
2022-10-11 07:40:39 -07:00
|
|
|
workflow.ownedBy = null;
|
|
|
|
workflow.sharedWith = [];
|
2022-10-31 07:08:25 -07:00
|
|
|
workflow.usedCredentials = [];
|
2022-10-11 07:40:39 -07:00
|
|
|
|
|
|
|
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 });
|
|
|
|
});
|
|
|
|
|
|
|
|
delete workflow.shared;
|
|
|
|
|
|
|
|
return workflow;
|
|
|
|
}
|
2022-10-13 02:55:58 -07:00
|
|
|
|
2022-10-31 07:08:25 -07:00
|
|
|
static async addCredentialsToWorkflow(
|
|
|
|
workflow: WorkflowWithSharingsAndCredentials,
|
|
|
|
currentUser: User,
|
|
|
|
): Promise<WorkflowWithSharingsAndCredentials> {
|
|
|
|
workflow.usedCredentials = [];
|
|
|
|
const userCredentials = await EECredentials.getAll(currentUser);
|
|
|
|
const credentialIdsUsedByWorkflow = new Set<number>();
|
|
|
|
workflow.nodes.forEach((node) => {
|
|
|
|
if (!node.credentials) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Object.keys(node.credentials).forEach((credentialType) => {
|
|
|
|
const credential = node.credentials?.[credentialType];
|
|
|
|
if (!credential?.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const credentialId = parseInt(credential.id, 10);
|
|
|
|
credentialIdsUsedByWorkflow.add(credentialId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
const workflowCredentials = await EECredentials.getMany({
|
|
|
|
where: {
|
|
|
|
id: In(Array.from(credentialIdsUsedByWorkflow)),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const userCredentialIds = userCredentials.map((credential) => credential.id.toString());
|
|
|
|
workflowCredentials.forEach((credential) => {
|
|
|
|
const credentialId = credential.id.toString();
|
|
|
|
workflow.usedCredentials?.push({
|
|
|
|
id: credential.id.toString(),
|
|
|
|
name: credential.name,
|
2022-11-08 08:52:42 -08:00
|
|
|
type: credential.type,
|
2022-10-31 07:08:25 -07:00
|
|
|
currentUserHasAccess: userCredentialIds.includes(credentialId),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return workflow;
|
|
|
|
}
|
|
|
|
|
2022-10-13 02:55:58 -07:00
|
|
|
static validateCredentialPermissionsToUser(
|
|
|
|
workflow: WorkflowEntity,
|
|
|
|
allowedCredentials: ICredentialsDb[],
|
|
|
|
) {
|
|
|
|
workflow.nodes.forEach((node) => {
|
|
|
|
if (!node.credentials) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Object.keys(node.credentials).forEach((credentialType) => {
|
|
|
|
const credentialId = parseInt(node.credentials?.[credentialType].id ?? '', 10);
|
|
|
|
const matchedCredential = allowedCredentials.find(
|
|
|
|
(credential) => credential.id === credentialId,
|
|
|
|
);
|
|
|
|
if (!matchedCredential) {
|
|
|
|
throw new Error('The workflow contains credentials that you do not have access to');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-10-26 06:49:43 -07:00
|
|
|
|
|
|
|
static async updateWorkflow(
|
|
|
|
user: User,
|
|
|
|
workflow: WorkflowEntity,
|
|
|
|
workflowId: string,
|
|
|
|
tags?: string[],
|
2022-10-31 02:35:24 -07:00
|
|
|
forceSave?: boolean,
|
2022-10-26 06:49:43 -07:00
|
|
|
): Promise<WorkflowEntity> {
|
|
|
|
const previousVersion = await EEWorkflowsService.get({ id: parseInt(workflowId, 10) });
|
|
|
|
if (!previousVersion) {
|
|
|
|
throw new ResponseHelper.ResponseError('Workflow not found', undefined, 404);
|
|
|
|
}
|
|
|
|
const allCredentials = await EECredentials.getAll(user);
|
|
|
|
try {
|
2022-10-31 02:35:24 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
|
2022-10-26 06:49:43 -07:00
|
|
|
workflow = WorkflowHelpers.validateWorkflowCredentialUsage(
|
|
|
|
workflow,
|
|
|
|
previousVersion,
|
|
|
|
allCredentials,
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
throw new ResponseHelper.ResponseError(
|
|
|
|
'Invalid workflow credentials - make sure you have access to all credentials and try again.',
|
|
|
|
undefined,
|
|
|
|
400,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-31 02:35:24 -07:00
|
|
|
return super.updateWorkflow(user, workflow, workflowId, tags, forceSave);
|
2022-10-26 06:49:43 -07:00
|
|
|
}
|
2022-10-11 05:55:05 -07:00
|
|
|
}
|