MDL-79520 core: upgrade step to update existing Clever issuers to OIDC

This change is an upgrade step which:
- Updates endpoints to the new endpoints
- Updates user field mappings so they map the new OIDC userinfo fields
- Updates the issuer's baseurl
This commit is contained in:
Jake Dallimore
2024-02-20 17:10:04 +08:00
parent d20af220f8
commit b2e184f9ad
2 changed files with 83 additions and 1 deletions
+82
View File
@@ -1004,5 +1004,87 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2024020200.02);
}
if ($oldversion < 2024021500.01) {
// A [name => url] map of new OIDC endpoints to be updated/created.
$endpointuris = [
'authorization_endpoint' => 'https://clever.com/oauth/authorize',
'token_endpoint' => 'https://clever.com/oauth/tokens',
'userinfo_endpoint' => 'https://api.clever.com/userinfo',
'jwks_uri' => 'https://clever.com/oauth/certs',
];
// A [internalfield => externalfield] map of new OIDC-based user field mappings to be updated/created.
$userfieldmappings = [
'idnumber' => 'sub',
'firstname' => 'given_name',
'lastname' => 'family_name',
'email' => 'email',
];
$admin = get_admin();
$adminid = $admin ? $admin->id : '0';
$cleverservices = $DB->get_records('oauth2_issuer', ['servicetype' => 'clever']);
foreach ($cleverservices as $cleverservice) {
$time = time();
// Insert/update the new endpoints.
foreach ($endpointuris as $endpointname => $endpointuri) {
$endpoint = ['issuerid' => $cleverservice->id, 'name' => $endpointname];
$endpointid = $DB->get_field('oauth2_endpoint', 'id', $endpoint);
if ($endpointid) {
$endpoint = array_merge($endpoint, [
'id' => $endpointid,
'url' => $endpointuri,
'timemodified' => $time,
'usermodified' => $adminid,
]);
$DB->update_record('oauth2_endpoint', $endpoint);
} else {
$endpoint = array_merge($endpoint, [
'url' => $endpointuri,
'timecreated' => $time,
'timemodified' => $time,
'usermodified' => $adminid,
]);
$DB->insert_record('oauth2_endpoint', $endpoint);
}
}
// Insert/update new user field mappings.
foreach ($userfieldmappings as $internalfieldname => $externalfieldname) {
$fieldmap = ['issuerid' => $cleverservice->id, 'internalfield' => $internalfieldname];
$fieldmapid = $DB->get_field('oauth2_user_field_mapping', 'id', $fieldmap);
if ($fieldmapid) {
$fieldmap = array_merge($fieldmap, [
'id' => $fieldmapid,
'externalfield' => $externalfieldname,
'timemodified' => $time,
'usermodified' => $adminid,
]);
$DB->update_record('oauth2_user_field_mapping', $fieldmap);
} else {
$fieldmap = array_merge($fieldmap, [
'externalfield' => $externalfieldname,
'timecreated' => $time,
'timemodified' => $time,
'usermodified' => $adminid,
]);
$DB->insert_record('oauth2_user_field_mapping', $fieldmap);
}
}
// Update the baseurl for the issuer.
$cleverservice->baseurl = 'https://clever.com';
$cleverservice->timemodified = $time;
$cleverservice->usermodified = $adminid;
$DB->update_record('oauth2_issuer', $cleverservice);
}
upgrade_main_savepoint(true, 2024021500.01);
}
return true;
}
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2024021500.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2024021500.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.4dev (Build: 20240215)'; // Human-friendly version name