fix: clean up implementaiton to remove looping, by adding secondary entry into the map lists to search against directly

Signed-off-by: John Redwood <john.r.k.redwood@gmail.com>
This commit is contained in:
John Redwood
2024-11-23 08:09:59 +11:00
parent 732700ab82
commit ced964bf0d
@@ -367,24 +367,17 @@ function ensureItems(
}
/**
* Helper function to search a Map ignoring case of the key which is the DN where
* memberOf could potentially return in lowercase.
* @param map - The map of DN's user or group as the key generally multicased.
* Helper function which dual searches the user/group maps first for original value, then for lowercased value.
* @param map - The map of DN's user or group as the key usually multicased.
* @param searchValue - The DN/memberOf search criteria which could potentially not match the map DN by case.
* @returns The value/result of the search criteria forcing key/search lowercase.
* @returns The value/result of the search criteria dual searching.
*/
function getValueFromMapWithInsensitiveKey(
map: Map<string, any>,
searchValue: string,
) {
for (const [key, value] of map) {
if (
key.toLocaleLowerCase('en-US') === searchValue.toLocaleLowerCase('en-US')
) {
return value;
}
}
return undefined; // In case no match is found, return undefined
const result = map.get(searchValue);
return result ? result : map.get(searchValue.toLocaleLowerCase('en-US'));
}
/**
@@ -417,12 +410,24 @@ export function resolveRelations(
for (const user of users) {
userMap.set(stringifyEntityRef(user), user);
userMap.set(user.metadata.annotations![LDAP_DN_ANNOTATION], user);
userMap.set(
user.metadata.annotations![LDAP_DN_ANNOTATION]?.toLocaleLowerCase(
'en-US',
),
user,
);
userMap.set(user.metadata.annotations![LDAP_RDN_ANNOTATION], user);
userMap.set(user.metadata.annotations![LDAP_UUID_ANNOTATION], user);
}
for (const group of groups) {
groupMap.set(stringifyEntityRef(group), group);
groupMap.set(group.metadata.annotations![LDAP_DN_ANNOTATION], group);
groupMap.set(
group.metadata.annotations![LDAP_DN_ANNOTATION]?.toLocaleLowerCase(
'en-US',
),
group,
);
groupMap.set(group.metadata.annotations![LDAP_RDN_ANNOTATION], group);
groupMap.set(group.metadata.annotations![LDAP_UUID_ANNOTATION], group);
}