diff --git a/app/plugins/CoreServer/src/Lib/Enum/LdapMatchingRuleEnum.php b/app/plugins/CoreServer/src/Lib/Enum/LdapMatchingRuleEnum.php new file mode 100644 index 000000000..d94bb6011 --- /dev/null +++ b/app/plugins/CoreServer/src/Lib/Enum/LdapMatchingRuleEnum.php @@ -0,0 +1,196 @@ + + */ + const array ATTRIBUTE_MATCHING_RULES = [ + // RFC 4512 / RFC 4519 (core) + 'name' => self::CASE_IGNORE, + 'cn' => self::CASE_IGNORE, // SUP name + 'sn' => self::CASE_IGNORE, // SUP name + 'givenname' => self::CASE_IGNORE, // SUP name + 'title' => self::CASE_IGNORE, // SUP name + 'o' => self::CASE_IGNORE, // SUP name + 'ou' => self::CASE_IGNORE, // SUP name + 'l' => self::CASE_IGNORE, // SUP name + 'st' => self::CASE_IGNORE, // SUP name + 'street' => self::CASE_IGNORE, + 'postalcode' => self::CASE_IGNORE, + 'description' => self::CASE_IGNORE, + 'uid' => self::CASE_IGNORE, + 'objectclass' => self::CASE_IGNORE, // objectIdentifierMatch, descriptors compare case-insensitively + 'telephonenumber' => self::TELEPHONE_NUMBER, + 'facsimiletelephonenumber' => self::OCTET_STRING, // no EQUALITY rule (Facsimile Telephone Number syntax) + 'userpassword' => self::OCTET_STRING, + 'distinguishedname' => self::DN, + 'member' => self::DN, // SUP distinguishedName + 'owner' => self::DN, // SUP distinguishedName + 'roleoccupant' => self::DN, // SUP distinguishedName + 'seealso' => self::DN, // SUP distinguishedName + + // cosine (RFC 4524) + 'mail' => self::CASE_IGNORE_IA5, + 'roomnumber' => self::CASE_IGNORE, + 'manager' => self::DN, + 'mobile' => self::TELEPHONE_NUMBER, + 'homephone' => self::TELEPHONE_NUMBER, + 'pager' => self::TELEPHONE_NUMBER, + + // inetOrgPerson (RFC 2798) and labeledURI (RFC 2079) + 'displayname' => self::CASE_IGNORE, + 'employeenumber' => self::CASE_IGNORE, + 'employeetype' => self::CASE_IGNORE, + 'labeleduri' => self::CASE_EXACT, + + // nis (RFC 2307) + 'uidnumber' => self::INTEGER, + 'gidnumber' => self::INTEGER, + 'homedirectory' => self::CASE_EXACT_IA5, + 'loginshell' => self::CASE_EXACT_IA5, + + // Password Policy (draft-behera-ldap-password-policy) + 'pwdaccountlockedtime' => self::GENERALIZED_TIME, + + // openssh-lpk + 'sshpublickey' => self::OCTET_STRING, + + // eduMember + 'ismemberof' => self::CASE_EXACT, + 'hasmember' => self::CASE_EXACT, + + // eduPerson + 'edupersonaffiliation' => self::CASE_IGNORE, + 'edupersonassurance' => self::CASE_IGNORE, + 'edupersonanalyticstag' => self::CASE_EXACT, + 'edupersondisplaypronouns' => self::OCTET_STRING, // no EQUALITY rule + 'edupersonentitlement' => self::CASE_EXACT, + 'edupersonnickname' => self::CASE_IGNORE, + 'edupersonorcid' => self::CASE_IGNORE, + 'edupersonorgdn' => self::DN, + 'edupersonorgunitdn' => self::DN, + 'edupersonprimaryaffiliation' => self::CASE_IGNORE, + 'edupersonprimaryorgunitdn' => self::DN, + 'edupersonprincipalname' => self::CASE_IGNORE, + 'edupersonprincipalnameprior' => self::CASE_IGNORE, + 'edupersonscopedaffiliation' => self::CASE_IGNORE, + 'edupersontargetedid' => self::CASE_IGNORE, + 'edupersonuniqueid' => self::CASE_IGNORE, + + // voPerson + 'vopersonaffiliation' => self::CASE_IGNORE, + 'vopersonapplicationpassword' => self::OCTET_STRING, + 'vopersonapplicationuid' => self::CASE_IGNORE, + 'vopersonauthorname' => self::CASE_IGNORE, + 'vopersoncertificatedn' => self::DN, + 'vopersoncertificateissuerdn' => self::DN, + 'vopersonexternalaffiliation' => self::CASE_IGNORE, + 'vopersonexternalid' => self::CASE_IGNORE, + 'vopersonid' => self::CASE_IGNORE, + 'vopersonpolicyagreement' => self::CASE_IGNORE, + 'vopersonscopedaffiliation' => self::CASE_IGNORE, + 'vopersonsorid' => self::CASE_IGNORE, + 'vopersonstatus' => self::CASE_IGNORE, + 'vopersontoken' => self::CASE_EXACT, + 'vopersonverifiedemail' => self::CASE_IGNORE, + ]; +} diff --git a/app/plugins/CoreServer/src/Lib/Traits/LdapMatchingRulesTrait.php b/app/plugins/CoreServer/src/Lib/Traits/LdapMatchingRulesTrait.php new file mode 100644 index 000000000..eca40b980 --- /dev/null +++ b/app/plugins/CoreServer/src/Lib/Traits/LdapMatchingRulesTrait.php @@ -0,0 +1,484 @@ + matching rule map. + * + * Classes using this trait may override this method to add or replace entries + * (keys must be lowercased base attribute names). + * + * @return array + * @since COmanage Registry v5.4.0 + */ + protected function attributeMatchingRules(): array { + return LdapMatchingRuleEnum::ATTRIBUTE_MATCHING_RULES; + } + + /** + * Determine the equality matching rule for an attribute. + * + * Attribute options (eg ";app-foo", ";lang-es") don't change the matching rule, + * so they are removed before the lookup. Attribute names are case-insensitive. + * + * @param string $attr Attribute name, possibly with options + * @return string Matching rule name (one of the LdapMatchingRuleEnum constants) + * @since COmanage Registry v5.4.0 + */ + protected function attributeMatchingRule(string $attr): string { + $base = strtolower(trim(explode(';', $attr, 2)[0])); + + return $this->attributeMatchingRules()[$base] ?? LdapMatchingRuleEnum::CASE_IGNORE; + } + + /** + * Comparison key for caseIgnoreMatch. + * + * RFC 4517 section 4.2.11: two Directory Strings match if, after RFC 4518 string + * preparation (transcode, map, Unicode NFKC normalization, prohibit, check bidi) + * with case folding and insignificant space handling (leading and trailing spaces + * removed, inner runs of spaces collapsed to one), they are the same. + * + * Approximation: NFKC (when intl is available), whitespace collapse, trim and + * Unicode case folding. The RFC 4518 mapping and prohibited character tables are + * not applied. + * + * @param string $v Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function caseIgnoreMatchKey(string $v): string { + if (!mb_check_encoding($v, 'UTF-8')) { + // Not a valid Directory String, compare case-insensitively on ASCII only + return strtolower(trim((string)preg_replace('/\s+/', ' ', $v))); + } + + return mb_convert_case($this->prepareDirectoryString($v), MB_CASE_FOLD, 'UTF-8'); + } + + /** + * Comparison key for caseIgnoreIA5Match. + * + * RFC 4517 section 4.2.13: like caseIgnoreMatch, but for IA5 (ASCII) strings. + * Case is ignored and insignificant spaces are handled as for caseIgnoreMatch. + * + * Approximation: the caseIgnoreMatch key. For ASCII values this is the same as + * lowercasing, and it stays safe for values that contain non-ASCII characters. + * + * @param string $v Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function caseIgnoreIA5MatchKey(string $v): string { + return $this->caseIgnoreMatchKey($v); + } + + /** + * Comparison key for caseExactMatch. + * + * RFC 4517 section 4.2.4: like caseIgnoreMatch, but without case folding. Two + * Directory Strings match if they are the same after RFC 4518 string preparation + * and insignificant space handling. Case is significant. + * + * Approximation: NFKC (when intl is available), whitespace collapse and trim. + * + * @param string $v Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function caseExactMatchKey(string $v): string { + if (!mb_check_encoding($v, 'UTF-8')) { + return $this->octetStringMatchKey($v); + } + + return $this->prepareDirectoryString($v); + } + + /** + * Comparison key for caseExactIA5Match. + * + * RFC 4517 section 4.2.3: like caseExactMatch, but for IA5 (ASCII) strings. + * Case is significant; insignificant spaces are handled as for caseExactMatch. + * + * Approximation: the caseExactMatch key. + * + * @param string $v Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function caseExactIA5MatchKey(string $v): string { + return $this->caseExactMatchKey($v); + } + + /** + * Comparison key for octetStringMatch. + * + * RFC 4517 section 4.2.27: two values match only if they have the same length + * and are identical octet by octet. No string preparation is applied. + * + * This method intentionally returns the value unchanged: octet strings are + * compared exactly, so there is nothing to normalize (no case folding, no + * space handling, no Unicode normalization). It exists so that attributes + * mapped to octetStringMatch (e.g. userPassword, sshPublicKey) are compared + * exactly instead of falling back to caseIgnoreMatch, which would merge + * distinct values that differ only in case. It is also the safe fallback + * key for values that are not valid UTF-8. + * + * @param string $v Attribute value + * @return string Comparison key (the value itself) + * @since COmanage Registry v5.4.0 + */ + protected function octetStringMatchKey(string $v): string { + return $v; + } + + /** + * Comparison key for distinguishedNameMatch. + * + * RFC 4517 section 4.2.15: two DNs match if they have the same number of RDNs + * and each pair of RDNs in the same position matches. Two RDNs match if they + * have the same set of attribute types (compared case-insensitively, with OIDs + * and short names being equivalent) and each value is equal to the + * corresponding value under that attribute type's own equality rule. + * + * Approximation: the DN is split with ldap_explode_dn(), escapes are decoded, + * attribute types are lowercased, every value is compared with the + * caseIgnoreMatch key, and the parts of a multi-valued RDN are sorted. OID and + * short name equivalence and per-type rules are not handled. If the value can't + * be parsed as a DN, the caseIgnoreMatch key of the whole value is used. + * + * @param string $v Attribute value (a DN) + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function distinguishedNameMatchKey(string $v): string { + if (trim($v) === '') { + return ''; + } + + $rdns = function_exists('ldap_explode_dn') ? @ldap_explode_dn($v, 0) : false; + + if (!is_array($rdns)) { + return $this->caseIgnoreMatchKey($v); + } + + unset($rdns['count']); + + $keys = []; + + foreach ($rdns as $rdn) { + $parts = []; + + // Split a multi-valued RDN on unescaped "+" + foreach (preg_split('/(?caseIgnoreMatchKey($v); + } + + $type = strtolower(trim(substr($ava, 0, $pos))); + $value = $this->unescapeDnValue(trim(substr($ava, $pos + 1))); + + $parts[] = $type . '=' . rawurlencode($this->caseIgnoreMatchKey($value)); + } + + sort($parts, SORT_STRING); + $keys[] = implode('+', $parts); + } + + return implode(',', $keys); + } + + /** + * Comparison key for integerMatch. + * + * RFC 4517 section 4.2.19: two values match if they denote the same integer + * (for example "007" and "7"). + * + * Approximation: the sign and leading zeros are normalized as strings, so large + * numbers work without bcmath. Values that aren't integers are compared as given + * (after trimming). + * + * @param string $v Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function integerMatchKey(string $v): string { + $v = trim($v); + + if (!preg_match('/^([+-]?)0*(\d+)$/', $v, $m)) { + return $v; + } + + $digits = $m[2]; + + return ($m[1] === '-' && $digits !== '0') ? '-' . $digits : $digits; + } + + /** + * Comparison key for numericStringMatch. + * + * RFC 4517 section 4.2.22: two Numeric Strings match if they are the same after + * all space characters have been removed. + * + * @param string $v Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function numericStringMatchKey(string $v): string { + return str_replace(' ', '', $v); + } + + /** + * Comparison key for telephoneNumberMatch. + * + * RFC 4517 section 4.2.29: like caseIgnoreMatch, but all spaces and hyphen + * characters (U+002D, U+2010-U+2015, U+2212, U+FE58, U+FE63, U+FF0D) are + * removed before the values are compared (RFC 4518 section 2.6.3). + * + * @param string $v Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function telephoneNumberMatchKey(string $v): string { + if (mb_check_encoding($v, 'UTF-8')) { + $stripped = preg_replace('/[\s\x{002D}\x{2010}-\x{2015}\x{2212}\x{FE58}\x{FE63}\x{FF0D}]+/u', '', $v); + } else { + $stripped = preg_replace('/[\s\-]+/', '', $v); + } + + return $this->caseIgnoreMatchKey($stripped ?? $v); + } + + /** + * Comparison key for generalizedTimeMatch. + * + * RFC 4517 section 4.2.16: two values match if they denote the same point in + * time. Values without a time zone are local times and only match other local + * times. Missing minutes, seconds or fractions are treated as zero, and a + * fraction applies to the last unit given (hour, minute or second). + * + * Approximation: the value (YYYYMMDDHH[MM[SS]][(.|,)fraction][Z|+-HH[MM]]) is + * parsed, converted to UTC when it carries a time zone, and formatted as + * "YmdHis.u" plus "Z" (UTC) or nothing (local time). Values that can't be parsed + * are compared as given (after trimming). + * + * @param string $v Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function generalizedTimeMatchKey(string $v): string { + $v = trim($v); + + if (!preg_match('/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})?(\d{2})?(?:[.,](\d+))?(Z|[+-]\d{2}(?:\d{2})?)?$/i', $v, $m)) { + return $v; + } + + $year = (int)$m[1]; + $month = (int)$m[2]; + $day = (int)$m[3]; + $hour = (int)$m[4]; + $minute = (int)($m[5] ?? '' ?: 0); + $second = (int)($m[6] ?? '' ?: 0); + $zone = strtoupper($m[8] ?? ''); + + if (!checkdate($month, $day, $year) || $hour > 23 || $minute > 59 || $second > 60) { + return $v; + } + + try { + if ($zone === '' || $zone === 'Z') { + $tz = new \DateTimeZone('UTC'); + } else { + $tz = new \DateTimeZone(substr($zone, 0, 3) . ':' . (substr($zone, 3, 2) ?: '00')); + } + + $dt = (new \DateTimeImmutable('@0')) + ->setTimezone($tz) + ->setDate($year, $month, $day) + ->setTime($hour, $minute, $second); + + // The fraction applies to the last unit given (hour, minute or second) + if (($m[7] ?? '') !== '') { + $unitSeconds = ($m[6] ?? '') !== '' ? 1 : ((($m[5] ?? '') !== '') ? 60 : 3600); + $micro = (int)round((float)('0.' . $m[7]) * $unitSeconds * 1000000); + + $dt = $dt->modify('+' . intdiv($micro, 1000000) . ' seconds'); + $dt = $dt->setTime( + (int)$dt->format('H'), + (int)$dt->format('i'), + (int)$dt->format('s'), + $micro % 1000000 + ); + } + + if ($zone === '') { + return $dt->format('YmdHis.u'); + } + + return $dt->setTimezone(new \DateTimeZone('UTC'))->format('YmdHis.u') . 'Z'; + } catch (\Throwable $e) { + return $v; + } + } + + /** + * Comparison key of a value, according to the attribute's equality matching rule. + * + * @param string $attr Attribute name, possibly with options + * @param mixed $value Attribute value + * @return string Comparison key + * @since COmanage Registry v5.4.0 + */ + protected function attributeValueKey(string $attr, mixed $value): string { + $v = (string)$value; + + return match ($this->attributeMatchingRule($attr)) { + LdapMatchingRuleEnum::CASE_EXACT => $this->caseExactMatchKey($v), + LdapMatchingRuleEnum::CASE_EXACT_IA5 => $this->caseExactIA5MatchKey($v), + LdapMatchingRuleEnum::CASE_IGNORE_IA5 => $this->caseIgnoreIA5MatchKey($v), + LdapMatchingRuleEnum::OCTET_STRING => $this->octetStringMatchKey($v), + LdapMatchingRuleEnum::DN => $this->distinguishedNameMatchKey($v), + LdapMatchingRuleEnum::INTEGER => $this->integerMatchKey($v), + LdapMatchingRuleEnum::NUMERIC_STRING => $this->numericStringMatchKey($v), + LdapMatchingRuleEnum::TELEPHONE_NUMBER => $this->telephoneNumberMatchKey($v), + LdapMatchingRuleEnum::GENERALIZED_TIME => $this->generalizedTimeMatchKey($v), + default => $this->caseIgnoreMatchKey($v), + }; + } + + /** + * Remove values of a single attribute that are equal under its matching rule. + * + * The first value seen for each comparison key is kept, unchanged and in its + * original position. Scalars, null and empty arrays (modify "clear" semantics) + * are returned unchanged. + * + * @param string $attr Attribute name, possibly with options + * @param mixed $values Attribute value(s) + * @return mixed De-duplicated values (re-indexed list), or $values unchanged + * @since COmanage Registry v5.4.0 + */ + protected function uniqueAttributeValues(string $attr, mixed $values): mixed { + if (!is_array($values) || empty($values)) { + return $values; + } + + $seen = []; + $ret = []; + + foreach ($values as $value) { + if (!is_scalar($value) && $value !== null && !($value instanceof \Stringable)) { + // Not something we can compare, keep it as is + $ret[] = $value; + continue; + } + + $key = $this->attributeValueKey($attr, $value); + + if (isset($seen[$key])) { + continue; + } + + $seen[$key] = true; + $ret[] = $value; + } + + return $ret; + } + + /** + * Remove duplicate values from every attribute of an LDAP attribute array. + * + * @param array $attributes LDAP attributes keyed by attribute name + * @return array De-duplicated attributes + * @since COmanage Registry v5.4.0 + */ + protected function uniqueAllAttributeValues(array $attributes): array { + foreach ($attributes as $attr => $values) { + $attributes[$attr] = $this->uniqueAttributeValues((string)$attr, $values); + } + + return $attributes; + } + + /** + * Apply the string preparation shared by the caseIgnore and caseExact rules: + * Unicode NFKC normalization (RFC 4518 section 2.3, when intl is available) and + * insignificant space handling (RFC 4518 section 2.6.1). + * + * @param string $v Valid UTF-8 value + * @return string Prepared value + * @since COmanage Registry v5.4.0 + */ + protected function prepareDirectoryString(string $v): string { + if (class_exists(\Normalizer::class)) { + $normalized = \Normalizer::normalize($v, \Normalizer::FORM_KC); + + if (is_string($normalized)) { + $v = $normalized; + } + } + + return trim((string)(preg_replace('/\s+/u', ' ', $v) ?? $v)); + } + + /** + * Decode the escapes of an RDN value as returned by ldap_explode_dn() + * ("\XX" hex pairs and "\c" escaped characters, RFC 4514 section 2.4). + * + * @param string $v Escaped RDN value + * @return string Decoded value + * @since COmanage Registry v5.4.0 + */ + protected function unescapeDnValue(string $v): string { + return (string)preg_replace_callback( + '/\\\\([0-9A-Fa-f]{2}|.)/s', + static fn(array $m): string => strlen($m[1]) === 2 ? chr(hexdec($m[1])) : $m[1], + $v + ); + } +} diff --git a/app/plugins/CoreServer/src/Model/Table/LdapServersTable.php b/app/plugins/CoreServer/src/Model/Table/LdapServersTable.php index 8d7ff05ff..2591c8ca6 100644 --- a/app/plugins/CoreServer/src/Model/Table/LdapServersTable.php +++ b/app/plugins/CoreServer/src/Model/Table/LdapServersTable.php @@ -44,6 +44,7 @@ class LdapServersTable extends Table { use \App\Lib\Traits\PrimaryLinkTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; const int NETWORK_TIMEOUT = 10; const int LDAP_PROTOCOL_VERSION = 3; @@ -272,6 +273,8 @@ public function queryLdap( * @since COmanage Registry v5.3.0 */ public function addEntry(\LDAP\Connection $cxn, string $dn, array $attributes): void { + // Remove values the LDAP server treats as equal, to avoid "Type or value exists" + $attributes = $this->uniqueAllAttributeValues($attributes); $attributes = $this->sanitizeAttributesForAdd($attributes); $attrKeys = array_keys($attributes); @@ -376,6 +379,9 @@ public function deleteEntry(\LDAP\Connection $cxn, string $dn): void { * @since COmanage Registry v5.3.0 */ public function modReplace(\LDAP\Connection $cxn, string $dn, array $attributes): void { + // Remove values the LDAP server treats as equal, to avoid "Type or value exists" + $attributes = $this->uniqueAllAttributeValues($attributes); + $currentErrorReporting = error_reporting(0); $ok = @ldap_mod_replace($cxn, $dn, $attributes); error_reporting($currentErrorReporting); diff --git a/app/plugins/LdapConnector/src/Model/Table/EduMemberSchemasTable.php b/app/plugins/LdapConnector/src/Model/Table/EduMemberSchemasTable.php index 6c6ee183b..b804825a2 100644 --- a/app/plugins/LdapConnector/src/Model/Table/EduMemberSchemasTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/EduMemberSchemasTable.php @@ -49,6 +49,7 @@ class EduMemberSchemasTable extends Table { use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; use \LdapConnector\Lib\Traits\LdapObjectClassSchemaTrait; /** @@ -268,7 +269,7 @@ protected function assembleIsMemberOf(Person $person): array } } - return array_values(array_unique($names)); + return $this->uniqueAttributeValues('isMemberOf', $names); } /** @@ -316,7 +317,7 @@ protected function assembleHasMember(Group $group, int $typeId, bool $isModifyLi } } - $values = array_values(array_unique(array_filter($values, static fn($v) => $v !== ''))); + $values = $this->uniqueAttributeValues('hasMember', array_filter($values, static fn($v) => $v !== '')); if (!empty($values)) { return $values; diff --git a/app/plugins/LdapConnector/src/Model/Table/EduPersonSchemasTable.php b/app/plugins/LdapConnector/src/Model/Table/EduPersonSchemasTable.php index 94680ad4e..b4de501fd 100644 --- a/app/plugins/LdapConnector/src/Model/Table/EduPersonSchemasTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/EduPersonSchemasTable.php @@ -48,6 +48,7 @@ class EduPersonSchemasTable extends Table use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; use \LdapConnector\Lib\Traits\LdapObjectClassSchemaTrait; /** @@ -394,7 +395,7 @@ protected function assembleEduPersonAffiliation(Person $person, bool $isModifyLi } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('eduPersonAffiliation', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -437,7 +438,7 @@ protected function assembleEduPersonScopedAffiliation(Person $person, string $sc } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('eduPersonScopedAffiliation', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -467,7 +468,7 @@ protected function assembleEduPersonEntitlement(Person $person, bool $isModifyLi } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('eduPersonEntitlement', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -507,7 +508,7 @@ protected function assembleEduPersonNickname(Person $person, ?int $targetTypeId, } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('eduPersonNickname', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -545,7 +546,7 @@ protected function assembleEduPersonOrcid(Person $person, ?int $coId, bool $isMo $vals[] = (string)$id->identifier; } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('eduPersonOrcid', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -622,7 +623,7 @@ protected function assembleEduPersonPrincipalNamePrior(Person $person, ?int $tar $vals[] = (string)$id->identifier; } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('eduPersonPrincipalNamePrior', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -675,7 +676,7 @@ protected function assembleEduPersonUniqueId(Person $person, ?int $targetTypeId, } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('eduPersonUniqueId', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; diff --git a/app/plugins/LdapConnector/src/Model/Table/GroupOfNamesSchemasTable.php b/app/plugins/LdapConnector/src/Model/Table/GroupOfNamesSchemasTable.php index aed249be1..4aa35d2d4 100644 --- a/app/plugins/LdapConnector/src/Model/Table/GroupOfNamesSchemasTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/GroupOfNamesSchemasTable.php @@ -47,6 +47,7 @@ class GroupOfNamesSchemasTable extends Table { use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; use \LdapConnector\Lib\Traits\LdapObjectClassSchemaTrait; /** @@ -290,7 +291,7 @@ protected function assembleMember(Group $group, int $ldapProvisionerId): array } } - $dns = array_values(array_unique(array_filter($dns, static fn($v) => $v !== ''))); + $dns = $this->uniqueAttributeValues('member', array_filter($dns, static fn($v) => $v !== '')); if (empty($dns)) { throw new \UnderflowException('member'); @@ -353,7 +354,7 @@ protected function assembleOwner(Group $group, int $ldapProvisionerId, bool $isM } } - $ownerDns = array_values(array_unique(array_filter($ownerDns, static fn($v) => $v !== ''))); + $ownerDns = $this->uniqueAttributeValues('owner', array_filter($ownerDns, static fn($v) => $v !== '')); if (!empty($ownerDns)) { return $ownerDns; diff --git a/app/plugins/LdapConnector/src/Model/Table/InetOrgPersonSchemasTable.php b/app/plugins/LdapConnector/src/Model/Table/InetOrgPersonSchemasTable.php index 61f8ec0ad..1a4f01a36 100644 --- a/app/plugins/LdapConnector/src/Model/Table/InetOrgPersonSchemasTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/InetOrgPersonSchemasTable.php @@ -47,6 +47,7 @@ class InetOrgPersonSchemasTable extends Table use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; use \LdapConnector\Lib\Traits\LdapObjectClassSchemaTrait; /** @@ -505,7 +506,7 @@ protected function assembleGivenName(Person $person, ?int $targetTypeId, bool $i $vals[] = (string)$person->primary_name->given; } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('givenName', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -583,7 +584,7 @@ protected function assembleO(Person $person, bool $isModifyLike): ?array } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('o', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -628,7 +629,7 @@ protected function assembleLabeledUri(Person $person, ?int $targetTypeId, ?int $ } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('labeledURI', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -665,7 +666,7 @@ protected function assembleMail(Person $person, ?int $targetTypeId, ?int $coId, $vals[] = (string)$em->mail; } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('mail', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -713,7 +714,7 @@ protected function assembleMobile(Person $person, ?int $targetTypeId, ?int $coId $consumeTelList((array)($pr->telephone_numbers ?? [])); } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('mobile', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -791,7 +792,7 @@ protected function assembleEmployeeType(Person $person, bool $isModifyLike): ?ar } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('employeeType', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -841,7 +842,7 @@ protected function assembleRoomNumber(Person $person, ?int $targetTypeId, ?int $ $consumeAddrList((array)($pr->addresses ?? [])); } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('roomNumber', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -882,7 +883,7 @@ protected function assembleUid(Person $person, ?int $targetTypeId, ?int $coId, b $consumeIdentifierList((array)($person->identifiers ?? [])); - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('uid', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; diff --git a/app/plugins/LdapConnector/src/Model/Table/LdapProvisionersTable.php b/app/plugins/LdapConnector/src/Model/Table/LdapProvisionersTable.php index c3e3202f4..4759d65a2 100644 --- a/app/plugins/LdapConnector/src/Model/Table/LdapProvisionersTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/LdapProvisionersTable.php @@ -53,6 +53,7 @@ class LdapProvisionersTable extends Table { use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; /** @@ -1154,7 +1155,8 @@ className: $className, } // Merge attributes; if the same attribute appears in multiple schemas, - // combine values (dedupe) when both are arrays, else last-one-wins. + // combine values when both are arrays, else last-one-wins. Combined values + // are de-duplicated by the attribute's LDAP equality matching rule. foreach ($schemaAttrs as $attr => $value) { if ($attr === '' || $value === null) { continue; @@ -1168,12 +1170,16 @@ className: $className, $existing = $attributes[$attr]; if (is_array($existing) && is_array($value)) { - $attributes[$attr] = array_values(array_unique(array_merge($existing, $value))); + $attributes[$attr] = $this->uniqueAttributeValues((string)$attr, array_merge($existing, $value)); } else { $attributes[$attr] = $value; } } } + + // Remove values the LDAP server treats as equal, including duplicates produced + // within a single schema (eg case variants of a caseIgnoreMatch attribute) + $attributes = $this->uniqueAllAttributeValues($attributes); $this->llog('debug', "Final merged attributes=" . json_encode($attributes)); diff --git a/app/plugins/LdapConnector/src/Model/Table/LdapPublicKeySchemasTable.php b/app/plugins/LdapConnector/src/Model/Table/LdapPublicKeySchemasTable.php index 054cde7df..73d6c26ea 100644 --- a/app/plugins/LdapConnector/src/Model/Table/LdapPublicKeySchemasTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/LdapPublicKeySchemasTable.php @@ -46,6 +46,7 @@ class LdapPublicKeySchemasTable extends Table { use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; use \LdapConnector\Lib\Traits\LdapObjectClassSchemaTrait; /** @@ -222,7 +223,7 @@ protected function assembleSshPublicKey(Person $person, bool $isModifyLike): ?ar $values[] = $v; } - $values = array_values(array_unique(array_filter($values, static fn($v) => $v !== ''))); + $values = $this->uniqueAttributeValues('sshPublicKey', array_filter($values, static fn($v) => $v !== '')); if (!empty($values)) { return $values; diff --git a/app/plugins/LdapConnector/src/Model/Table/OrganizationalPersonSchemasTable.php b/app/plugins/LdapConnector/src/Model/Table/OrganizationalPersonSchemasTable.php index 786ef98d3..bb5baef43 100644 --- a/app/plugins/LdapConnector/src/Model/Table/OrganizationalPersonSchemasTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/OrganizationalPersonSchemasTable.php @@ -47,6 +47,7 @@ class OrganizationalPersonSchemasTable extends Table use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; use \LdapConnector\Lib\Traits\LdapObjectClassSchemaTrait; /** @@ -607,7 +608,7 @@ private function normalizeAttributeMap(array $attrMap): array } $vals = array_map(static fn($v) => (string)$v, $attrMap[$k]); - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues((string)$k, array_filter($vals, static fn($v) => $v !== '')); $attrMap[$k] = $vals; } @@ -639,7 +640,7 @@ private function mergeAttributeMap(array &$into, array $from): void } if (is_array($into[$k]) && is_array($v)) { - $into[$k] = array_values(array_unique(array_merge($into[$k], $v))); + $into[$k] = $this->uniqueAttributeValues((string)$k, array_merge($into[$k], $v)); } else { $into[$k] = $v; } diff --git a/app/plugins/LdapConnector/src/Model/Table/PersonSchemasTable.php b/app/plugins/LdapConnector/src/Model/Table/PersonSchemasTable.php index 06f34e2fb..af901c561 100644 --- a/app/plugins/LdapConnector/src/Model/Table/PersonSchemasTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/PersonSchemasTable.php @@ -47,6 +47,7 @@ class PersonSchemasTable extends Table { use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; use \LdapConnector\Lib\Traits\LdapObjectClassSchemaTrait; /** @@ -286,7 +287,7 @@ protected function assembleUserPassword(Person $person, bool $isModifyLike): ?ar } } - $values = array_values(array_unique(array_filter($values, static fn($v) => $v !== ''))); + $values = $this->uniqueAttributeValues('userPassword', array_filter($values, static fn($v) => $v !== '')); if (!empty($values)) { return $values; diff --git a/app/plugins/LdapConnector/src/Model/Table/VoPersonSchemasTable.php b/app/plugins/LdapConnector/src/Model/Table/VoPersonSchemasTable.php index c7d5df819..7d051a02e 100644 --- a/app/plugins/LdapConnector/src/Model/Table/VoPersonSchemasTable.php +++ b/app/plugins/LdapConnector/src/Model/Table/VoPersonSchemasTable.php @@ -47,6 +47,7 @@ class VoPersonSchemasTable extends Table use \App\Lib\Traits\QueryModificationTrait; use \App\Lib\Traits\TableMetaTrait; use \App\Lib\Traits\ValidationTrait; + use \CoreServer\Lib\Traits\LdapMatchingRulesTrait; use \LdapConnector\Lib\Traits\LdapObjectClassSchemaTrait; /** @@ -483,7 +484,7 @@ protected function assembleVoPersonAffiliation(Person $person, bool $isModifyLik } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('voPersonAffiliation', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -522,7 +523,7 @@ protected function assembleVoPersonAuthorName(Person $person, ?int $targetTypeId } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('voPersonAuthorName', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -564,7 +565,7 @@ protected function assembleVoPersonApplicationUid( */ protected function assembleVoPersonExternalId(Person $person, ?int $targetTypeId, bool $isModifyLike): ?array { - return $this->assembleIdentifierByTypeId($person, $targetTypeId, $isModifyLike); + return $this->assembleIdentifierByTypeId('voPersonExternalID', $person, $targetTypeId, $isModifyLike); } /** @@ -577,7 +578,7 @@ protected function assembleVoPersonExternalId(Person $person, ?int $targetTypeId */ protected function assembleVoPersonId(Person $person, ?int $targetTypeId, bool $isModifyLike): ?array { - return $this->assembleIdentifierByTypeId($person, $targetTypeId, $isModifyLike); + return $this->assembleIdentifierByTypeId('voPersonID', $person, $targetTypeId, $isModifyLike); } /** @@ -590,19 +591,24 @@ protected function assembleVoPersonId(Person $person, ?int $targetTypeId, bool $ */ protected function assembleVoPersonSorid(Person $person, ?int $targetTypeId, bool $isModifyLike): ?array { - return $this->assembleIdentifierByTypeId($person, $targetTypeId, $isModifyLike); + return $this->assembleIdentifierByTypeId('voPersonSoRID', $person, $targetTypeId, $isModifyLike); } /** * Helper: extract identifier values for a specific type_id. * + * @param string $attribute LDAP attribute name, used to select the matching rule for de-duplication. * @param Person $person Provisioned Person entity. * @param int|null $targetTypeId Target Identifier type ID. * @param bool $isModifyLike Whether the provisioning operation is modify/rename. * @return array|null */ - protected function assembleIdentifierByTypeId(Person $person, ?int $targetTypeId, bool $isModifyLike): ?array - { + protected function assembleIdentifierByTypeId( + string $attribute, + Person $person, + ?int $targetTypeId, + bool $isModifyLike + ): ?array { $vals = []; foreach (($person->identifiers ?? []) as $id) { @@ -623,7 +629,7 @@ protected function assembleIdentifierByTypeId(Person $person, ?int $targetTypeId } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues($attribute, array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -673,7 +679,7 @@ protected function assembleVoPersonCertificateDn(Person $person, bool $isModifyL } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('voPersonCertificateDN', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -703,7 +709,7 @@ protected function assembleVoPersonCertificateIssuerDn(Person $person, bool $isM } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('voPersonCertificateIssuerDN', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -733,7 +739,7 @@ protected function assembleVoPersonPolicyAgreement(Person $person, bool $isModif } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('voPersonPolicyAgreement', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -781,7 +787,7 @@ protected function assembleVoPersonToken(Person $person, bool $isModifyLike): ?a } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('voPersonToken', array_filter($vals, static fn($v) => $v !== '')); if (!empty($vals)) { return $vals; @@ -912,7 +918,7 @@ protected function assembleVoPersonApplicationUidAttribute( } } - $vals = array_values(array_unique(array_filter($vals, static fn($v) => $v !== ''))); + $vals = $this->uniqueAttributeValues('voPersonApplicationUID', array_filter($vals, static fn($v) => $v !== '')); $attropts = $this->attributeOptionsEnabled($ldapProvisioner); @@ -1029,7 +1035,7 @@ protected function assembleVoPersonApplicationPasswordAttribute( // Normalize any collected lists foreach (array_keys($ret) as $k) { if (is_array($ret[$k])) { - $ret[$k] = array_values(array_unique(array_filter($ret[$k], static fn($v) => $v !== ''))); + $ret[$k] = $this->uniqueAttributeValues((string)$k, array_filter($ret[$k], static fn($v) => $v !== '')); } }