1
0
mirror of https://github.com/bitwarden/directory-connector synced 2026-01-05 18:13:23 +00:00

Ignore duplicate users with same externalId (#152)

* Ignore duplicate users with same externalId

* Add null check

* Exclude deleted users from disabled users (Azure)

* Fix order of null check

* Stringify and compare duplicate before discarding
This commit is contained in:
Thomas Rittson
2021-09-14 07:00:37 +10:00
committed by GitHub
parent 32a8e65fe8
commit 033c346042
2 changed files with 26 additions and 17 deletions

View File

@@ -144,7 +144,7 @@ export class AzureDirectoryService extends BaseDirectoryService implements IDire
continue;
}
const entry = this.buildUser(user);
if (!entry.disabled && !entry.deleted) {
if (!entry.deleted) {
continue;
}
if (await this.filterOutUserResult(setFilter, entry, false)) {

View File

@@ -57,13 +57,7 @@ export class SyncService {
this.flattenUsersToGroups(groups, groups);
}
const duplicateEmails = this.findDuplicateUserEmails(users);
if (duplicateEmails.length > 0) {
const emailsMessage = duplicateEmails.length < 4 ?
duplicateEmails.join('\n') :
duplicateEmails.slice(0, 3).join('\n') + '\n' + this.i18nService.t('andMore', `${duplicateEmails.length - 3}`);
throw new Error(this.i18nService.t('duplicateEmails') + '\n' + emailsMessage);
}
users = this.removeDuplicateUsers(users);
if (test || (!syncConfig.overwriteExisting &&
(groups == null || groups.length === 0) && (users == null || users.length === 0))) {
@@ -118,17 +112,32 @@ export class SyncService {
}
}
private findDuplicateUserEmails(users: UserEntry[]) {
const duplicatedEmails = new Array<string>();
users.reduce((agg, user) => {
if (agg.includes(user.email) && !duplicatedEmails.includes(user.email)) {
duplicatedEmails.push(user.email);
private removeDuplicateUsers(users: UserEntry[]) {
const uniqueUsers = new Array<UserEntry>();
const processedUsers = new Map<string, string>();
const duplicateEmails = new Array<string>();
// UserEntrys with the same email are ignored if their properties are the same
// UserEntrys with the same email but different properties will throw an error
users.forEach(u => {
if (processedUsers.has(u.email)) {
if (processedUsers.get(u.email) != JSON.stringify(u)) {
duplicateEmails.push(u.email);
}
} else {
agg.push(user.email);
uniqueUsers.push(u);
processedUsers.set(u.email, JSON.stringify(u));
}
return agg;
}, new Array<string>());
return duplicatedEmails;
});
if (duplicateEmails.length > 0) {
const emailsMessage = duplicateEmails.length < 4 ?
duplicateEmails.join('\n') :
duplicateEmails.slice(0, 3).join('\n') + '\n' + this.i18nService.t('andMore', `${duplicateEmails.length - 3}`);
throw new Error(this.i18nService.t('duplicateEmails') + '\n' + emailsMessage);
}
return uniqueUsers;
}
private filterUnsupportedUsers(users: UserEntry[]): UserEntry[] {