Skip to content

Commit

Permalink
Merge branch 'wip-81513-m39' into MOODLE_39_STABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
weilai-irl committed Mar 25, 2022
2 parents bfb800d + b39917b commit e4e4824
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
3 changes: 3 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
*
* @package auth_oidc
* @author James McQuillan <[email protected]>
* @author Lai Wei <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright (C) 2014 onwards Microsoft, Inc. (http://microsoft.com/)
*/

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/auth/oidc/lib.php');

/**
* Update plugin.
*
Expand Down
59 changes: 55 additions & 4 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function auth_oidc_get_remote_fields() {
}
} else {
$remotefields = [
'' => '',
'' => get_string('settings_fieldmap_feild_not_mapped', 'auth_oidc'),
'objectId' => get_string('settings_fieldmap_field_objectId', 'auth_oidc'),
'userPrincipalName' => get_string('settings_fieldmap_field_userPrincipalName', 'auth_oidc'),
'givenName' => get_string('settings_fieldmap_field_givenName', 'auth_oidc'),
Expand All @@ -288,6 +288,20 @@ function auth_oidc_get_remote_fields() {
return $remotefields;
}

/**
* Return the list of available remote fields to map email field.
*
* @return array
*/
function auth_oidc_get_email_remote_fields() {
$remotefields = [
'mail' => get_string('settings_fieldmap_field_mail', 'auth_oidc'),
'userPrincipalName' => get_string('settings_fieldmap_field_userPrincipalName', 'auth_oidc'),
];

return $remotefields;
}

/**
* Return the current field mapping settings in an array.
*
Expand Down Expand Up @@ -324,9 +338,41 @@ function auth_oidc_get_field_mappings() {
}
}

if (!array_key_exists('email', $fieldmappings)) {
$fieldmappings['email'] = auth_oidc_apply_default_email_mapping();
}

return $fieldmappings;
}

/**
* Apply default email mapping settings.
*
* @return array
*/
function auth_oidc_apply_default_email_mapping() {
set_config('field_map_email', 'mail', 'auth_oidc');

$authoidcconfig = get_config('auth_oidc');

$fieldsetting = [];
$fieldsetting['field_map'] = 'mail';

if (property_exists($authoidcconfig, 'field_lock_email')) {
$fieldsetting['field_lock'] = $authoidcconfig->field_lock_email;
} else {
$fieldsetting['field_lock'] = 'unlocked';
}

if (property_exists($authoidcconfig, 'field_updatelocal_email')) {
$fieldsetting['update_local'] = $authoidcconfig->field_updatelocal_email;
} else {
$fieldsetting['update_local'] = 'always';
}

return $fieldsetting;
}

/**
* Helper function used to print mapping and locking for auth_oidc plugin on admin pages.
*
Expand Down Expand Up @@ -381,6 +427,7 @@ function auth_oidc_display_auth_lock_options($settings, $auth, $userfields, $hel
}

$remotefields = auth_oidc_get_remote_fields();
$emailremotefields = auth_oidc_get_email_remote_fields();

foreach ($userfields as $field) {
// Define the fieldname we display to the user.
Expand Down Expand Up @@ -415,8 +462,13 @@ function auth_oidc_display_auth_lock_options($settings, $auth, $userfields, $hel
} else if ($mapremotefields) {
// We are mapping to a remote field here.
// Mapping.
$settings->add(new admin_setting_configselect("auth_oidc/field_map_{$field}",
get_string('auth_fieldmapping', 'auth', $fieldname), '', null, $remotefields));
if ($field == 'email') {
$settings->add(new admin_setting_configselect("auth_oidc/field_map_{$field}",
get_string('auth_fieldmapping', 'auth', $fieldname), '', null, $emailremotefields));
} else {
$settings->add(new admin_setting_configselect("auth_oidc/field_map_{$field}",
get_string('auth_fieldmapping', 'auth', $fieldname), '', null, $remotefields));
}

// Update local.
$settings->add(new admin_setting_configselect("auth_{$auth}/field_updatelocal_{$field}",
Expand All @@ -431,7 +483,6 @@ function auth_oidc_display_auth_lock_options($settings, $auth, $userfields, $hel
// Lock fields.
$settings->add(new admin_setting_configselect("auth_{$auth}/field_lock_{$field}",
get_string('auth_fieldlockfield', 'auth', $fieldname), '', 'unlocked', $lockoptions));

} else {
// Lock fields Only.
$settings->add(new admin_setting_configselect("auth_{$auth}/field_lock_{$field}",
Expand Down

0 comments on commit e4e4824

Please sign in to comment.