jenkins-bot has submitted this change and it was merged.

Change subject: Allow setting local email/realname from remote wiki
......................................................................


Allow setting local email/realname from remote wiki

Change-Id: I9b34be7b49b5d12919f7c7b59b0e70bdca9bd079
---
M OAuthAuthentication.php
M handlers/AuthenticationHandler.php
M i18n/en.json
M i18n/qqq.json
M specials/SpecialOAuthLogin.php
M tests/OAuthAuthConfigTest.php
M tests/OAuthAuthDBTest.php
M tests/OAuthAuthExternalUserTest.php
M tests/OAuthAuthHooksTest.php
M utils/Hooks.php
M utils/OAuthExternalUser.php
11 files changed, 88 insertions(+), 17 deletions(-)

Approvals:
  Reedy: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/OAuthAuthentication.php b/OAuthAuthentication.php
index 2af6352..4cbbe4e 100644
--- a/OAuthAuthentication.php
+++ b/OAuthAuthentication.php
@@ -96,6 +96,7 @@
 $wgAutoloadClasses['MediaWiki\Extensions\OAuthAuthentication\OAuth1Handler'] = 
"$dir/handlers/OAuth1Handler.php";
 $wgAutoloadClasses['MediaWiki\Extensions\OAuthAuthentication\SessionStore'] = 
"$dir/store/SessionStore.php";
 $wgAutoloadClasses['MediaWiki\Extensions\OAuthAuthentication\PhpSessionStore'] 
= "$dir/store/PhpSessionStore.php";
+$wgAutoloadClasses['MediaWiki\Extensions\OAuthAuthentication\OAuthAuthDBTest'] 
= "$dir/tests/OAuthAuthDBTest.php";
 
 ## i18n
 $wgMessagesDirs['OAuthAuthentication'] = "$dir/i18n";
diff --git a/handlers/AuthenticationHandler.php 
b/handlers/AuthenticationHandler.php
index ddedfb1..a497736 100644
--- a/handlers/AuthenticationHandler.php
+++ b/handlers/AuthenticationHandler.php
@@ -12,6 +12,12 @@
                        wfGetDB( DB_MASTER )  #TODO: don't do this
                );
                $exUser->setAccessToken( $accessToken );
+               if ( isset( $identity->realname ) ) {
+                       $exUser->setRealname( $identity->realname );
+               }
+               if ( isset( $identity->email ) ) {
+                       $exUser->setEmail( $identity->email );
+               }
                $exUser->setIdentifyTS( new \MWTimestamp() );
 
                if ( $exUser->attached() ) {
@@ -57,9 +63,9 @@
                                return $status;
                        }
 
-                       /* TODO: Set email, realname, and language, once we can 
get them via /identify
                        $u->setEmail( $exUser->getEmail() );
-                       $u->setRealName( $exUser->getRealName() );
+                       $u->setRealName( $exUser->getRealname() );
+                       /*
                        $u->setOption( 'language', $exUser->getLanguage() );
                        */
 
@@ -100,9 +106,27 @@
                                __METHOD__ . ": Associated user is Anon. 
Aborting." );
                        return \Status::newFatal( 
'oauthauth-login-usernotexists' );
                }
-wfDebugLog( "OAA", __METHOD__ . " updating exuser: " . print_r( $exUser, true 
) );
                $exUser->updateInDatabase( wfGetDB( DB_MASTER ) );
 
+               $changed = false;
+               // update private data if needed
+               if ( $u->getEmail() !== $exUser->getEmail() ) {
+                       if ( $exUser->getEmail() ) {
+                               $u->setEmail( $exUser->getEmail() );
+                               $u->confirmEmail();
+                       } else {
+                               $u->invalidateEmail();
+                       }
+                       $changed = true;
+               }
+               if ( $u->getRealName() !== $exUser->getRealname() ) {
+                       $u->setRealName( $exUser->getRealname() );
+                       $changed = true;
+               }
+
+               if ( $changed ) {
+                       $u->saveSettings();
+               }
                $u->invalidateCache();
 
                if ( !$wgSecureLogin ) {
diff --git a/i18n/en.json b/i18n/en.json
index f48bd79..eff0b85 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -17,5 +17,7 @@
        "oauthauth-login-usernotexists": "The OAuth user is listed as 
connected, but the user does not exist on this wiki.",
        "oauthauth-nologin-policy": "This wiki's policy will not let you 
login.",
        "oauthauth-localuser-not-allowed": "Local account creation is disabled 
on this wiki. \"$1\" creates an account with OAuth.",
-       "oauthauth-loggout-policy": "You have been logged out because the site 
policy no longer allows you to be logged in."
+       "oauthauth-loggout-policy": "You have been logged out because the site 
policy no longer allows you to be logged in.",
+       "oauthauth-set-email": "Your attached account did not have an email 
address the last time you logged in. Set an email address on $1 to set your 
email address here.",
+       "oauthauth-email-set": "$1. Update your email address on $2 to change 
it."
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index 6e7060e..6a6d3ce 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -18,5 +18,7 @@
        "oauthauth-login-usernotexists": "Error when the user logs in with an 
account that was attached on this wiki, but no longer exists.",
        "oauthauth-nologin-policy": "Error the user sees when the local wiki's 
administrator has prevented their login with a policy.",
        "oauthauth-localuser-not-allowed": "Error when the user attempts to 
create an account, but the wiki isn't configured to allow it, and instructing 
the user to login via OAuth.\n\nParameters:\n* $1 - link text {{msg-mw|login}}. 
A link to login.",
-       "oauthauth-loggout-policy": "Error message when a user is logged out 
because their user no longer complies with the requirements set by the site 
administrator"
+       "oauthauth-loggout-policy": "Error message when a user is logged out 
because their user no longer complies with the requirements set by the site 
administrator",
+       "oauthauth-set-email": "Instructions in the user's preferences telling 
a user to set their email address on another website.\n\nParameters:\n* $1 - 
the name of the remote wiki (e.g., 'Wikipedia') where they will set their email 
address.",
+       "oauthauth-email-set": "Showing the user their current email address, 
and instructing them how to change it, by going to another 
wiki.\n\nParameters:\n* $1 - the user's current email address.\n* $2 - the name 
of the remote wiki (e.g., 'Wikipedia') where they will set their email address."
 }
diff --git a/specials/SpecialOAuthLogin.php b/specials/SpecialOAuthLogin.php
index 010ce0b..396a761 100644
--- a/specials/SpecialOAuthLogin.php
+++ b/specials/SpecialOAuthLogin.php
@@ -12,7 +12,6 @@
        public function execute( $subpage ) {
                global $wgUser;
                $request = $this->getRequest();
-
                $this->setHeaders();
 
                if ( !$this->getUser()->isAnon() ) {
diff --git a/tests/OAuthAuthConfigTest.php b/tests/OAuthAuthConfigTest.php
index 07a87b2..e399e49 100644
--- a/tests/OAuthAuthConfigTest.php
+++ b/tests/OAuthAuthConfigTest.php
@@ -1,11 +1,13 @@
 <?php
+namespace MediaWiki\Extensions\OAuthAuthentication;
+
 /**
  * @group OAuthAuthentication
  */
 class OAuthAuthConfigTest extends OAuthAuthDBTest {
 
        public function testGetDefaultConfigAndToken() {
-               list( $config, $token ) = 
\MediaWiki\Extensions\OAuthAuthentication\Config::getDefaultConfigAndToken();
+               list( $config, $token ) = Config::getDefaultConfigAndToken();
                $this->assertInstanceOf( 'MWOAuthClientConfig', $config );
                $this->assertInstanceOf( 'OAuthToken', $token );
        }
diff --git a/tests/OAuthAuthDBTest.php b/tests/OAuthAuthDBTest.php
index 8080ceb..1715a17 100644
--- a/tests/OAuthAuthDBTest.php
+++ b/tests/OAuthAuthDBTest.php
@@ -1,8 +1,9 @@
 <?php
+namespace MediaWiki\Extensions\OAuthAuthentication;
 /**
  * @group OAuthAuthentication
  */
-class OAuthAuthDBTest extends MediaWikiTestCase {
+class OAuthAuthDBTest extends \MediaWikiTestCase {
 
        public function __construct( $name = null, array $data = array(), 
$dataName = '' ) {
                parent::__construct( $name, $data, $dataName );
@@ -16,13 +17,13 @@
                $this->db->sourceFile( __DIR__ . '/../store/oauthauth.sql' );
 
                // TODO: Setup some test data
-               $user = User::newFromName( 'OAuthUser' );
+               $user = \User::newFromName( 'OAuthUser' );
                if ( $user->idForName() == 0 ) {
                        $user->addToDatabase();
                        $user->setPassword( 'OAUP@ssword' );
                        $user->saveSettings();
                }
-               $exUser = new 
\MediaWiki\Extensions\OAuthAuthentication\OAuthExternalUser( 100, 
$user->getId(), 'OAuthUser' );
+               $exUser = new OAuthExternalUser( 100, $user->getId(), 
'OAuthUser' );
                $exUser->addToDatabase( $this->db );
        }
 
diff --git a/tests/OAuthAuthExternalUserTest.php 
b/tests/OAuthAuthExternalUserTest.php
index d739b28..ed28fd7 100644
--- a/tests/OAuthAuthExternalUserTest.php
+++ b/tests/OAuthAuthExternalUserTest.php
@@ -1,18 +1,19 @@
 <?php
+namespace MediaWiki\Extensions\OAuthAuthentication;
 /**
  * @group OAuthAuthentication
  */
 class OAuthAuthExternalUserTest extends OAuthAuthDBTest {
 
        public function testExternalUser() {
-               $exUser = new 
\MediaWiki\Extensions\OAuthAuthentication\OAuthExternalUser( 20, 30, 'ExUser' );
+               $exUser = new OAuthExternalUser( 20, 30, 'ExUser' );
                $this->assertEquals( 'ExUser', $exUser->getName() );
                $this->assertEquals( 30, $exUser->getLocalId() );
        }
 
        public function testNewFromRemoteId() {
                // We added remoteId 120 in parent class
-               $exUser = 
\MediaWiki\Extensions\OAuthAuthentication\OAuthExternalUser::newFromRemoteId( 
120, 'OAuthUser', $this->db );
+               $exUser = OAuthExternalUser::newFromRemoteId( 120, 'OAuthUser', 
$this->db );
                $this->assertInstanceOf( 
'MediaWiki\Extensions\OAuthAuthentication\OAuthExternalUser', $exUser );
                $this->assertEquals( 'OAuthUser', $exUser->getName() );
        }
diff --git a/tests/OAuthAuthHooksTest.php b/tests/OAuthAuthHooksTest.php
index b3b8735..c5a1b47 100644
--- a/tests/OAuthAuthHooksTest.php
+++ b/tests/OAuthAuthHooksTest.php
@@ -1,4 +1,5 @@
 <?php
+namespace MediaWiki\Extensions\OAuthAuthentication;
 /**
  * @group OAuthAuthentication
  */
@@ -11,9 +12,9 @@
                ) );
 
                $personal_urls = array( 'login' => array( 'href' => 'fail' ) );
-               $title = new Title();
+               $title = new \Title();
 
-               
\MediaWiki\Extensions\OAuthAuthentication\Hooks::onPersonalUrls( 
$personal_urls, $title );
+               Hooks::onPersonalUrls( $personal_urls, $title );
 
                $this->assertSame(
                        true,
diff --git a/utils/Hooks.php b/utils/Hooks.php
index cbd47bf..f64c0ee 100644
--- a/utils/Hooks.php
+++ b/utils/Hooks.php
@@ -56,7 +56,7 @@
        }
 
        public static function onGetPreferences( $user, &$preferences ) {
-               global $wgRequirePasswordforEmailChange;
+               global $wgRequirePasswordforEmailChange, 
$wgOAuthAuthenticationRemoteName;
 
                $resetlink = \Linker::link(
                        \SpecialPage::getTitleFor( 'PasswordReset' ),
@@ -80,13 +80,27 @@
                        }
 
                        if ( $wgRequirePasswordforEmailChange ) {
+
+                               $emailMsg = wfMessage(
+                                       'oauthauth-set-email',
+                                       $wgOAuthAuthenticationRemoteName
+                               )->escaped();
+                               $emailCss = 'mw-email-none';
+                               if ( $user->getEmail() ) {
+                                       $emailMsg = wfMessage(
+                                               'oauthauth-email-set',
+                                               $user->getEmail(),
+                                               $wgOAuthAuthenticationRemoteName
+                                       )->escaped();
+                                       $emailCss = 'mw-email-authenticated';
+                               }
                                $preferences['emailaddress'] = array(
                                        'type' => 'info',
                                        'raw' => 1,
-                                       'default' => wfMessage( 
'oauthauth-set-email' )->escaped(),
+                                       'default' => $emailMsg,
                                        'section' => 'personal/email',
                                        'label-message' => 'youremail',
-                                       'cssclass' => 'mw-email-none',
+                                       'cssclass' => $emailCss,
                                );
                        }
 
@@ -99,6 +113,7 @@
                                'label-message' => null,
                        );
                }
+
        }
 
        /**
diff --git a/utils/OAuthExternalUser.php b/utils/OAuthExternalUser.php
index 2b9d21e..900dd4e 100644
--- a/utils/OAuthExternalUser.php
+++ b/utils/OAuthExternalUser.php
@@ -10,6 +10,12 @@
        // Remote Username
        private $username;
 
+       // Remote Realname
+       private $realname;
+
+       // Remote Email
+       private $email;
+
        // Remote unique id
        private $remoteId;
 
@@ -171,4 +177,21 @@
        public function getIdentifyTS() {
                return $this->identifyTS;
        }
+
+       public function setRealname( $realname ) {
+               $this->realname = $realname;
+       }
+
+       public function getRealname() {
+               return $this->realname;
+       }
+
+       public function setEmail( $email ) {
+               $this->email = $email;
+       }
+
+       public function getEmail() {
+               return $this->email;
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/214092
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I9b34be7b49b5d12919f7c7b59b0e70bdca9bd079
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/OAuthAuthentication
Gerrit-Branch: master
Gerrit-Owner: CSteipp <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: Reedy <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: Zhuyifei1999 <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to