MDL-58898 oauth: Remove orphaned linked logins

This commit is contained in:
John Okely 2017-05-12 11:48:11 +08:00
parent b16603770b
commit 4c8727bad6
3 changed files with 33 additions and 0 deletions

View File

@ -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
*

View File

@ -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);
}
}

View File

@ -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);