feat: also send credentials when returning workflow via API (#4458) (no-changelog)

This commit is contained in:
Omar Ajoue
2022-10-31 11:08:25 -03:00
committed by GitHub
parent ec5ef0c50d
commit 7563d450f9
6 changed files with 184 additions and 10 deletions

View File

@@ -6,7 +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';
import type { WorkflowWithSharingsAndCredentials } from './workflows.types';
import { EECredentialsService as EECredentials } from '../credentials/credentials.service.ee';
export class EEWorkflowsService extends WorkflowsService {
@@ -74,10 +74,11 @@ export class EEWorkflowsService extends WorkflowsService {
}
static addOwnerAndSharings(
workflow: WorkflowEntity & WorkflowWithSharings,
): WorkflowEntity & WorkflowWithSharings {
workflow: WorkflowWithSharingsAndCredentials,
): WorkflowWithSharingsAndCredentials {
workflow.ownedBy = null;
workflow.sharedWith = [];
workflow.usedCredentials = [];
workflow.shared?.forEach(({ user, role }) => {
const { id, email, firstName, lastName } = user;
@@ -90,12 +91,49 @@ export class EEWorkflowsService extends WorkflowsService {
workflow.sharedWith?.push({ id, email, firstName, lastName });
});
// @ts-ignore
delete workflow.shared;
return workflow;
}
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,
currentUserHasAccess: userCredentialIds.includes(credentialId),
});
});
return workflow;
}
static validateCredentialPermissionsToUser(
workflow: WorkflowEntity,
allowedCredentials: ICredentialsDb[],