Skip to content

local_o365: Improve processing of suspend/delete users feature. #2819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: MOODLE_404_STABLE
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions local/o365/classes/feature/usersync/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -1716,10 +1716,45 @@ public function suspend_users(array $entraidusers, bool $delete = false) {

$apiclient = $this->construct_user_api();

// Create a temporary table to store to the entra users then query against the user table.
$temptablename = 'local_o365_objects_entra';
$dbman = $DB->get_manager();
$temptable = new \xmldb_table($temptablename);
$temptable->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE);
$temptable->add_field('objectid', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL);
$temptable->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
if ($dbman->table_exists($temptable)) {
$dbman->drop_table($temptable);
}
$dbman->create_temp_table($temptable);

// Insert entra users into the temporary table.
$this->mtrace("Storing " . count($entraidusers) . " Entra users.");
$bulk_insert_records = 1000;
$eusers = [];
foreach ($entraidusers as $entraiduser) {
$eusers[] = $entraiduser['id'];
if (count($eusers) >= $bulk_insert_records) {
$euserssql = 'INSERT INTO {' . $temptablename . '} (objectid) VALUES ';
$euserssql .= implode(",", array_fill(0, count($eusers), "(?)"));
$DB->execute($euserssql, array_values($eusers));
$eusers = [];
}
}

// Insert any remaining users.
if (!empty($eusers)) {
$euserssql = 'INSERT INTO {' . $temptablename . '} (objectid) VALUES ';
$euserssql .= implode(",", array_fill(0, count($eusers), "(?)"));
$DB->execute($euserssql, array_values($eusers));
$eusers = [];
}

try {
$deletedusersids = [];

$deletedusers = $apiclient->list_deleted_users();
$this->mtrace("Processing " . count($deletedusers) . " Deleted users.");
foreach ($deletedusers as $deleteduser) {
if (!empty($deleteduser) && isset($deleteduser['id'])) {
// Check for synced user.
Expand All @@ -1735,7 +1770,7 @@ public function suspend_users(array $entraidusers, bool $delete = false) {
$synceduser = $DB->get_record_sql($sql, $params);
if (!empty($synceduser)) {
$synceduser->suspended = 1;
user_update_user($synceduser, false);
$DB->set_field('user', 'suspended', $synceduser->suspended, array('id' => $synceduser->id));
$this->mtrace($synceduser->username . ' was deleted in Entra ID, the matching account is suspended.');
}
$deletedusersids[] = $deleteduser['id'];
Expand All @@ -1756,7 +1791,14 @@ public function suspend_users(array $entraidusers, bool $delete = false) {
$existingsqlparams = array_merge($existingsqlparams, $objectidparams);
}

// Include users that aren't in Entra.
$existingsql .= ' AND obj.objectid not in (select objectid from {' . $temptablename . '})';
// Include users that aren't suspended.
$existingsql .= ' AND u.suspended = ?';
$existingsqlparams[] = 0;
$existingusers = $DB->get_records_sql($existingsql, $existingsqlparams);
$this->mtrace("Suspending / Deleting " . count($existingusers) . " Moodle users.");

$validentraiduserids = [];
foreach ($entraidusers as $entraiduser) {
$validentraiduserids[] = $entraiduser['id'];
Expand All @@ -1776,15 +1818,17 @@ public function suspend_users(array $entraidusers, bool $delete = false) {
' in Microsoft Entra ID. Suspending user...');
$existinguser->suspended = 1;
unset($existinguser->objectid);
user_update_user($existinguser, false);
$DB->set_field('user', 'suspended', $existinguser->suspended, array('id' => $existinguser->id));
}
}
}
$dbman->drop_table($temptable); // Drop table on completion.

return true;
} catch (moodle_exception $e) {
utils::debug('Could not delete users', __METHOD__, $e);

$dbman->drop_table($temptable); // Drop table on failure.
return false;
}
}
Expand Down