Skip to content

Commit

Permalink
Bug fix in handling country profile field mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
weilai-irl committed Feb 7, 2025
1 parent 3139219 commit e95a8cd
Showing 1 changed file with 18 additions and 32 deletions.
50 changes: 18 additions & 32 deletions local/o365/classes/feature/usersync/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,15 @@ public static function apply_configured_fieldmap(array $entraiduserdata, stdClas
}

$usersync = new self();

$countrymapping = get_string_manager()->get_list_of_countries();
foreach ($countrymapping as $code => $country) {
$country = strtolower($country);
$country = preg_replace('/\s*\([^)]*\)/', '', $country);
$country = preg_replace('/\s*(the\s+)?republic\s+of/', '', $country);
$countrymapping[$code] = $country;
}

foreach ($fieldmappings as $localfield => $fieldmapping) {
$remotefield = $fieldmapping['field_map'];
$behavior = $fieldmapping['update_local'];
Expand All @@ -559,12 +568,17 @@ public static function apply_configured_fieldmap(array $entraiduserdata, stdClas
case 'country':
// Update country with two-letter country code.
$incoming = strtoupper($entraiduserdata[$remotefield]);
$countrymap = get_string_manager()->get_list_of_countries();
if (isset($countrymap[$incoming])) {
if (isset($countrymapping[$incoming])) {
$countrycode = $incoming;
} else {
$countrycode = array_search($entraiduserdata[$remotefield],
get_string_manager()->get_list_of_countries());
$incoming = strtolower($incoming);
foreach ($countrymapping as $code => $country) {
if (stripos($country, $incoming) !== false ||
stripos($incoming, $country) !== false) {
$countrycode = $code;
break;
}
}
}
$user->$localfield = (!empty($countrycode)) ? $countrycode : '';
break;
Expand Down Expand Up @@ -845,20 +859,6 @@ public function create_user_from_entra_id_data($entraiduserdata, $syncoptions) {
return false;
}

// Locate country code.
if (isset($entraiduserdata['country'])) {
$countries = get_string_manager()->get_list_of_countries(true, 'en');
foreach ($countries as $code => $name) {
if ($entraiduserdata['country'] == $name) {
$entraiduserdata['country'] = $code;
}
}
if (strlen($entraiduserdata['country']) > 2) {
// Limit string to 2 chars to prevent sql error.
$entraiduserdata['country'] = substr($entraiduserdata['country'], 0, 2);
}
}

$username = $entraiduserdata['useridentifier'];
if (isset($entraiduserdata['convertedidentifier']) && $entraiduserdata['convertedidentifier']) {
$username = $entraiduserdata['convertedidentifier'];
Expand Down Expand Up @@ -934,20 +934,6 @@ public function create_user_from_entra_id_data($entraiduserdata, $syncoptions) {
* @return bool An boolean indicating that was created Moodle user.
*/
public function update_user_from_entra_id_data($entraiduserdata, $fullexistinguser) {
// Locate country code.
if (isset($entraiduserdata['country'])) {
$countries = get_string_manager()->get_list_of_countries(true, 'en');
foreach ($countries as $code => $name) {
if ($entraiduserdata['country'] == $name) {
$entraiduserdata['country'] = $code;
}
}
if (strlen($entraiduserdata['country']) > 2) {
// Limit string to 2 chars to prevent sql error.
$entraiduserdata['country'] = substr($entraiduserdata['country'], 0, 2);
}
}

$existinguser = static::apply_configured_fieldmap($entraiduserdata, $fullexistinguser, 'login');

if (!empty($existinguser->email)) {
Expand Down

0 comments on commit e95a8cd

Please sign in to comment.