Permalink
Browse files

Merge branch 'MDL-58774-master' of git://github.com/damyon/moodle

  • Loading branch information...
dmonllao committed May 10, 2017
2 parents 675ffb4 + 4f705f5 commit 396ae85082b69f575615c50ad49d82ab87771ebb
@@ -105,6 +105,10 @@ public static function link_login($userinfo, $issuer, $userid = false, $skipperm
$userid = $USER->id;
}
if (linked_login::count_records(['username' => $userinfo['username']]) > 0) {
throw new moodle_exception('alreadylinked', 'auth_oauth2');
}
if (\core\session\manager::is_loggedinas()) {
throw new moodle_exception('notwhileloggedinas', 'auth_oauth2');
}
@@ -144,9 +148,8 @@ public static function send_confirm_link_login_email($userinfo, $issuer, $userid
$record->issuerid = $issuer->get('id');
$record->username = $userinfo['username'];
$record->userid = $userid;
$existing = linked_login::get_record((array)$record);
if ($existing) {
return false;
if (linked_login::count_records(['username' => $userinfo['username']]) > 0) {
throw new moodle_exception('alreadylinked', 'auth_oauth2');
}
$record->email = $userinfo['email'];
$record->confirmtoken = random_string(32);
@@ -239,6 +242,10 @@ 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) {
throw new moodle_exception('alreadylinked', 'auth_oauth2');
}
$user = new stdClass();
$user->username = $userinfo['username'];
$user->email = $userinfo['email'];
@@ -319,4 +326,18 @@ public static function delete_linked_login($linkedloginid) {
$login->delete();
}
/**
* Delete linked logins for a user.
*
* @param \core\event\user_deleted $event
* @return boolean
*/
public static function user_deleted(\core\event\user_deleted $event) {
global $DB;
$userid = $event->objectid;
return $DB->delete_records(linked_login::TABLE, ['userid' => $userid]);
}
}
@@ -0,0 +1,31 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file definies observers needed by the plugin.
*
* @package auth_oauth2
* @copyright 2017 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
// List of observers.
$observers = [
[
'eventname' => '\core\event\user_deleted',
'callback' => '\auth_oauth2\api::user_deleted',
],
];
@@ -83,3 +83,4 @@
$string['oauth2:managelinkedlogins'] = 'Manage own linked login accounts';
$string['plugindescription'] = 'This authentication plugin displays a list of the configured identity providers on the login page. Selecting an identity provider allows users to login with their credentials from an OAuth 2 provider.';
$string['pluginname'] = 'OAuth 2';
$string['alreadylinked'] = 'This external account is already linked to an account on this site';
@@ -58,8 +58,12 @@
$userinfo = $client->get_userinfo();
if (!empty($userinfo)) {
\auth_oauth2\api::link_login($userinfo, $issuer);
redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
try {
\auth_oauth2\api::link_login($userinfo, $issuer);
redirect($PAGE->url, get_string('changessaved'), null, \core\output\notification::NOTIFY_SUCCESS);
} catch (Exception $e) {
redirect($PAGE->url, $e->getMessage(), null, \core\output\notification::NOTIFY_ERROR);
}
} else {
redirect($PAGE->url, get_string('notloggedin', 'auth_oauth2'), null, \core\output\notification::NOTIFY_ERROR);
}
@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2017051500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2017051501; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2017050500; // Requires this Moodle version.
$plugin->component = 'auth_oauth2'; // Full name of the plugin (used for diagnostics).

0 comments on commit 396ae85

Please sign in to comment.