in classes/task/processmatchqueue.php [78:248]
public function execute() {
global $DB;
if (\local_o365\utils::is_configured() !== true) {
return false;
}
$auth = new \auth_plugin_oidc;
$sql = 'SELECT mq.*,
u.id as muserid,
muserconn.id as muserexistingconnectionid,
officeconn.id as officeuserexistingconnectionid,
oidctok.id as officeuserexistingoidctoken,
officeobj.id as officeuserobjectrecid
FROM {local_o365_matchqueue} mq
LEFT JOIN {user} u ON mq.musername = u.username
LEFT JOIN {local_o365_connections} muserconn ON muserconn.muserid = u.id
LEFT JOIN {local_o365_connections} officeconn ON officeconn.aadupn = mq.o365username
LEFT JOIN {local_o365_objects} officeobj ON officeobj.moodleid = u.id AND officeobj.o365name = mq.o365username
LEFT JOIN {auth_oidc_token} oidctok ON oidctok.oidcusername = mq.o365username
WHERE mq.completed = ? AND mq.errormessage = ?
ORDER BY mq.id ASC';
$params = ['0', ''];
$matchqueue = $DB->get_recordset_sql($sql, $params, 0, 100);
$apiclient = $this->get_api();
foreach ($matchqueue as $matchrec) {
mtrace('Processing '.$matchrec->musername.'/'.$matchrec->o365username);
try {
// Check for matching Moodle user.
if (empty($matchrec->muserid)) {
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->errormessage = get_string('task_processmatchqueue_err_nomuser', 'local_o365');
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace($updatedrec->errormessage);
continue;
}
// Check whether Moodle user is already o365 connected.
if (\local_o365\utils::is_o365_connected($matchrec->muserid)) {
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->errormessage = get_string('task_processmatchqueue_err_museralreadyo365', 'local_o365');
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace($updatedrec->errormessage);
continue;
}
// Check existing matches for Moodle user.
if (!empty($matchrec->muserexistingconnectionid)) {
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->errormessage = get_string('task_processmatchqueue_err_museralreadymatched', 'local_o365');
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace($updatedrec->errormessage);
continue;
}
// Check existing matches for Microsoft 365 user.
if (!empty($matchrec->officeuserexistingconnectionid)) {
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->errormessage = get_string('task_processmatchqueue_err_o365useralreadymatched', 'local_o365');
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace($updatedrec->errormessage);
continue;
}
// Check existing tokens for Microsoft 365 user (indicates o365 user is already connected to someone).
if (!empty($matchrec->officeuserexistingoidctoken)) {
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->errormessage = get_string('task_processmatchqueue_err_o365useralreadyconnected', 'local_o365');
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace($updatedrec->errormessage);
continue;
}
// Check if a o365 user object record already exists.
if (!empty($matchrec->officeuserobjectrecid)) {
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->errormessage = get_string('task_processmatchqueue_err_o365useralreadyconnected', 'local_o365');
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace($updatedrec->errormessage);
continue;
}
// Check o365 username.
$userfound = false;
try {
$o365user = $apiclient->get_user_by_upn($matchrec->o365username);
$userfound = true;
} catch (\Exception $e) {
$userfound = false;
}
if ($userfound !== true) {
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->errormessage = get_string('task_processmatchqueue_err_noo365user', 'local_o365');
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace($updatedrec->errormessage);
continue;
}
if (empty($matchrec->openidconnect)) {
// Match validated.
$connectionrec = new \stdClass;
$connectionrec->muserid = $matchrec->muserid;
$connectionrec->aadupn = \core_text::strtolower($o365user['userPrincipalName']);
$connectionrec->uselogin = 0;
$DB->insert_record('local_o365_connections', $connectionrec);
} else {
$userobjectid = null;
if (\local_o365\rest\unified::is_configured()) {
$userobjectid = $o365user['id'];
} else {
$userobjectid = $o365user['objectId'];
}
mtrace('Adding o365 object record for user.');
$now = time();
$userobjectdata = (object)[
'type' => 'user',
'subtype' => '',
'objectid' => $userobjectid,
'o365name' => $o365user['userPrincipalName'],
'moodleid' => $matchrec->muserid,
'timecreated' => $now,
'timemodified' => $now,
];
$DB->insert_record('local_o365_objects', $userobjectdata);
// Updated the user's authentication method field.
mtrace('Updating user authentication record.');
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->muserid;
$updatedrec->auth = $auth->authtype;
$DB->update_record('user', $updatedrec);
}
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace('Match record created for userid #' . $matchrec->muserid . ' and o365 user ' .
\core_text::strtolower($o365user['userPrincipalName']));
} catch (\Exception $e) {
$exceptionstring = $e->getMessage().': '.$e->debuginfo;
$updatedrec = new \stdClass;
$updatedrec->id = $matchrec->id;
$updatedrec->errormessage = $exceptionstring;
$updatedrec->completed = 1;
$DB->update_record('local_o365_matchqueue', $updatedrec);
mtrace($exceptionstring);
}
}
$matchqueue->close();
return true;
}