Skip to content

Commit

Permalink
MDL-58898 oauth: Remove orphaned linked logins
Browse files Browse the repository at this point in the history
  • Loading branch information
John Okely committed May 12, 2017
1 parent b166037 commit 4c8727b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions auth/oauth2/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
*/
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
*
Expand Down
22 changes: 22 additions & 0 deletions auth/oauth2/classes/linked_login.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,26 @@ protected static function define_properties() {
);
}

/**
* 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);
}

}
1 change: 1 addition & 0 deletions auth/oauth2/linkedlogins.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
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);
Expand Down

0 comments on commit 4c8727b

Please sign in to comment.