diff --git a/auth/oauth2/classes/api.php b/auth/oauth2/classes/api.php index 430abeef8d3..2c36d7d7c7d 100644 --- a/auth/oauth2/classes/api.php +++ b/auth/oauth2/classes/api.php @@ -39,6 +39,16 @@ defined('MOODLE_INTERNAL') || die(); */ class api { + /** + * Remove all linked logins that are using issuers that have been deleted. + * + * @param int $issuerid The issuer id of the issuer to check, or false to check all (defaults to all) + * @return boolean + */ + public static function clean_orphaned_linked_logins($issuerid = false) { + return linked_login::delete_orphaned($issuerid); + } + /** * List linked logins * diff --git a/auth/oauth2/classes/linked_login.php b/auth/oauth2/classes/linked_login.php index e4a56ca600b..b49392c765b 100644 --- a/auth/oauth2/classes/linked_login.php +++ b/auth/oauth2/classes/linked_login.php @@ -65,4 +65,26 @@ class linked_login extends persistent { ); } + /** + * Remove all linked logins that are using issuers that have been deleted. + * + * @param int $issuerid The issuer id of the issuer to check, or false to check all (defaults to all) + * @return boolean + */ + public static function delete_orphaned($issuerid = false) { + global $DB; + // Delete any linked_login entries with a issuerid + // which does not exist in the issuer table. + // In the left join, the issuer id will be null + // where a match linked_login.issuerid is not found. + $sql = "DELETE FROM {" . self::TABLE . "} + WHERE issuerid NOT IN (SELECT id FROM {" . \core\oauth2\issuer::TABLE . "})"; + $params = []; + if (!empty($issuerid)) { + $sql .= ' AND issuerid = ?'; + $params['issuerid'] = $issuerid; + } + return $DB->execute($sql, $params); + } + } diff --git a/auth/oauth2/linkedlogins.php b/auth/oauth2/linkedlogins.php index 12285306a5a..d8f68f14299 100644 --- a/auth/oauth2/linkedlogins.php +++ b/auth/oauth2/linkedlogins.php @@ -79,6 +79,7 @@ $linkedlogin = null; echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('linkedlogins', 'auth_oauth2')); echo $OUTPUT->doc_link('Linked_Logins', get_string('linkedloginshelp', 'auth_oauth2')); +auth_oauth2\api::clean_orphaned_linked_logins(); $linkedlogins = auth_oauth2\api::get_linked_logins(); echo $renderer->linked_logins_table($linkedlogins);