fix(core): Assign credential ownership correctly in source control import (#8955)

This commit is contained in:
Iván Ovejero
2024-03-26 17:18:41 +01:00
committed by GitHub
parent 160dfd383d
commit 260bc07ca9
7 changed files with 202 additions and 22 deletions

View File

@@ -310,7 +310,10 @@ export class SourceControlImportService {
}>;
}
public async importCredentialsFromWorkFolder(candidates: SourceControlledFile[], userId: string) {
public async importCredentialsFromWorkFolder(
candidates: SourceControlledFile[],
importingUserId: string,
) {
const candidateIds = candidates.map((c) => c.id);
const existingCredentials = await Container.get(CredentialsRepository).find({
where: {
@@ -335,9 +338,6 @@ export class SourceControlImportService {
const existingCredential = existingCredentials.find(
(e) => e.id === credential.id && e.type === credential.type,
);
const sharedOwner = existingSharedCredentials.find(
(e) => e.credentialsId === credential.id,
);
const { name, type, data, id, nodesAccess } = credential;
const newCredentialObject = new Credentials({ id, name }, type, []);
@@ -351,10 +351,23 @@ export class SourceControlImportService {
this.logger.debug(`Updating credential id ${newCredentialObject.id as string}`);
await Container.get(CredentialsRepository).upsert(newCredentialObject, ['id']);
if (!sharedOwner) {
const isOwnedLocally = existingSharedCredentials.some(
(c) => c.credentialsId === credential.id,
);
if (!isOwnedLocally) {
const remoteOwnerId = credential.ownedBy
? await Container.get(UserRepository)
.findOne({
where: { email: credential.ownedBy },
select: { id: true },
})
.then((user) => user?.id)
: null;
const newSharedCredential = new SharedCredentials();
newSharedCredential.credentialsId = newCredentialObject.id as string;
newSharedCredential.userId = userId;
newSharedCredential.userId = remoteOwnerId ?? importingUserId;
newSharedCredential.role = 'credential:owner';
await Container.get(SharedCredentialsRepository).upsert({ ...newSharedCredential }, [