Permalink
Browse files

Merge branch 'MDL-58774-master-fix' of git://github.com/andrewnicols/…

…moodle
  • Loading branch information...
dmonllao committed May 12, 2017
2 parents d9d2c2f + 95dd5e3 commit 3227ac837484b75a860ea26cbca39cc4a59b532b
Showing with 26 additions and 3 deletions.
  1. +3 −3 auth/oauth2/classes/api.php
  2. +23 −0 auth/oauth2/classes/linked_login.php
@@ -105,7 +105,7 @@ public static function link_login($userinfo, $issuer, $userid = false, $skipperm
$userid = $USER->id;
}
if (linked_login::count_records(['username' => $userinfo['username']]) > 0) {
if (linked_login::has_existing_issuer_match($issuer, $userinfo['username'])) {
throw new moodle_exception('alreadylinked', 'auth_oauth2');
}
@@ -148,7 +148,7 @@ public static function send_confirm_link_login_email($userinfo, $issuer, $userid
$record->issuerid = $issuer->get('id');
$record->username = $userinfo['username'];
$record->userid = $userid;
if (linked_login::count_records(['username' => $userinfo['username']]) > 0) {
if (linked_login::has_existing_issuer_match($issuer, $userinfo['username'])) {
throw new moodle_exception('alreadylinked', 'auth_oauth2');
}
$record->email = $userinfo['email'];
@@ -242,7 +242,7 @@ public static function send_confirm_account_email($userinfo, $issuer) {
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');
if (linked_login::count_records(['username' => $userinfo['username']]) > 0) {
if (linked_login::has_existing_issuer_match($issuer, $userinfo['username'])) {
throw new moodle_exception('alreadylinked', 'auth_oauth2');
}
@@ -65,4 +65,27 @@ protected static function define_properties() {
);
}
/**
* Check whether there are any valid linked accounts for this issuer
* and username combination.
*
* @param \core\oauth2\issuer $issuer The issuer
* @param string $username The username to check
*/
public static function has_existing_issuer_match(\core\oauth2\issuer $issuer, $username) {
global $DB;
$where = "issuerid = :issuerid
AND username = :username
AND (confirmtokenexpires = 0 OR confirmtokenexpires > :maxexpiry)";
$count = $DB->count_records_select(static::TABLE, $where, [
'issuerid' => $issuer->get('id'),
'username' => $username,
'maxexpiry' => (new \DateTime('NOW'))->getTimestamp(),
]);
return $count > 0;
}
}

0 comments on commit 3227ac8

Please sign in to comment.