Skip to content

Commit 95e71f4

Browse files
committed
Test for syncing email updates
1 parent 918273e commit 95e71f4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Topcoder/class.topcoder.plugin.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ public function gdn_auth_startAuthenticator_handler() {
505505

506506
if ($userID) {
507507
$this->syncTopcoderRoles($userID,$topcoderRoles);
508+
$this->syncTopcoderEmail($userID,$decodedToken->getClaim('email'));
508509
Gdn::authenticator()->setIdentity($userID, true);
509510
Gdn::session()->start($userID, true);
510511
Gdn::authenticator()->trigger(Gdn_Authenticator::AUTH_SUCCESS);
@@ -659,6 +660,45 @@ private function syncTopcoderRoles($userID, $roles) {
659660
}
660661
}
661662

663+
/**
664+
* Sync the e-mail addressof Topcoder for an user
665+
* @param $userID
666+
* @param $roles array a list of role names
667+
*
668+
*/
669+
private function syncTopcoderEmail($userID,$topcoder_email) {
670+
$userModel = new UserModel();
671+
$user = $userModel->getID($userID);
672+
$vanilla_email = val('Email', $user);
673+
674+
// Update if two e-mail addresses are different
675+
if($vanilla_email !== $topcoder_email) {
676+
$userData = [
677+
"UserID" => $userID,
678+
"Email" => $topcoder_email,
679+
"EmailConfirmed" => true
680+
];
681+
682+
$settings = [
683+
'NoConfirmEmail' => true
684+
];
685+
$ret = $userModel->save($userData, $settings);
686+
if($ret) {
687+
$modified_user = $userModel->getID($userID);
688+
$modified_email = val('Email', $user);
689+
if($modified_email === $topcoder_email) {
690+
self::log('Succeeded to modify e-mail', ["new_email"=>$modified_email]);
691+
} else {
692+
self::log('Failed to modify e-mail', []);
693+
}
694+
} else {
695+
self::log('Failed to modify e-mail', []);
696+
}
697+
} else {
698+
self::log('No need to modify e-mail.', []);
699+
}
700+
}
701+
662702
/**
663703
* Get a role by name and type.
664704
*

0 commit comments

Comments
 (0)