fix(core): Assign credential ownership correctly in source control import (#8955)
This commit is contained in:
@@ -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 }, [
|
||||
|
||||
Reference in New Issue
Block a user