Skip to content

Commit

Permalink
Make sure to support username logins and updating
Browse files Browse the repository at this point in the history
  • Loading branch information
rrusso committed Feb 1, 2024
1 parent 1d8c24f commit e43ecf2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
4 changes: 4 additions & 0 deletions auth/oauth2/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ public static function create_new_confirmed_account($userinfo, $issuer) {
require_once($CFG->dirroot.'/user/lib.php');

$user = new stdClass();
// BEGIN LSU case fixes.
$user->username = trim(core_text::strtolower($userinfo['username']));
$user->email = trim(core_text::strtolower($userinfo['email']));
// END LSU case fixes.
$user->auth = 'oauth2';
$user->mnethostid = $CFG->mnet_localhost_id;
$user->secret = random_string(15);
Expand Down
47 changes: 44 additions & 3 deletions auth/oauth2/classes/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,9 @@ public function complete_login(client $client, $redirecturl) {
redirect(new moodle_url('/login/index.php'));
} else if ($mappeduser && ($mappeduser->confirmed || !$issuer->get('requireconfirmation'))) {
// Update user fields.
$userinfo = $this->update_user($userinfo, $mappeduser);
// BEGIN LSU oauth Fixes.
$userinfo = self::update_userinfo($userinfo, $mappeduser);
// END LSU oauth Fixes.
$userwasmapped = true;
} else {
// Trigger login failed event.
Expand Down Expand Up @@ -523,8 +525,13 @@ public function complete_login(client $client, $redirecturl) {

if (!$userwasmapped) {
// No defined mapping - we need to see if there is an existing account with the same email.
// BEGIN LSU oauth fixes.
$moodleuser = \core_user::get_user_by_username($userinfo['username']);
if (empty($moodleuser)) {
$moodleuser = \core_user::get_user_by_email($userinfo['email']);
}
// END LSU oauth fixes.

$moodleuser = \core_user::get_user_by_email($userinfo['email']);
if (!empty($moodleuser)) {
if ($issuer->get('requireconfirmation')) {
$PAGE->set_url('/auth/oauth2/confirm-link-login.php');
Expand All @@ -541,7 +548,9 @@ public function complete_login(client $client, $redirecturl) {
// We dont have profile loaded on $moodleuser, so load it.
require_once($CFG->dirroot.'/user/profile/lib.php');
profile_load_custom_fields($moodleuser);
$userinfo = $this->update_user($userinfo, $moodleuser);
// BEGIN LSU oauth fixes.
$userinfo = self::update_userinfo($userinfo, $moodleuser);
// END LSU oauth fixes.
// No redirect, we will complete this login.
}

Expand Down Expand Up @@ -624,6 +633,38 @@ public function complete_login(client $client, $redirecturl) {
redirect($redirecturl);
}

// BEGIN LSU oauth fixes.
/**
* Update user data according to data sent by authorization server.
*
* @param array $userinfo data from authorization server
* @param stdClass $moodleuser Current data of the user to be updated
* @return stdClass The updated user record, or the existing one if there's nothing to be updated.
*/
public static function update_userinfo($userinfo, $moodleuser) {
global $DB;
$table = 'user';
$userinfo["id"] = $moodleuser->id;
// Unset the user's first name to keep whatever first name they have in Moodle.
unset($userinfo['firstname']);

// Make sure username and email are lowercase.
if (isset($userinfo->username)) {
$userinfo->username = trim(core_text::strtolower($userinfo->username));
}
if (isset($userinfo->email)) {
$userinfo->email = trim(core_text::strtolower($userinfo->email));
}

// Update the record.
$update = $DB->update_record($table, $userinfo, $bulk=null);

// Get the complete record.
$user = $DB->get_record($table, array("id" => $moodleuser->id));
return $user;
}
// END LSU oauth fixes.

/**
* Returns information on how the specified user can change their password.
* The password of the oauth2 accounts is not stored in Moodle.
Expand Down

0 comments on commit e43ecf2

Please sign in to comment.