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

Change subject: Disallow User::setPassword() on users not in database
......................................................................


Disallow User::setPassword() on users not in database

Change I2c736ad mostly removed the password handling from the User
object, but left in a little password handling to preserve the existing
ability to call $user->setPassword() before the user was actually added
to the database. That ability is now removed.

Bug: T47716
Change-Id: Id3d40742f2e2b197ad6facd149cc6350006bf289
---
M includes/User.php
M maintenance/createAndPromote.php
2 files changed, 25 insertions(+), 32 deletions(-)

Approvals:
  Gergő Tisza: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/User.php b/includes/User.php
index a6b897d..eb3ab9d 100644
--- a/includes/User.php
+++ b/includes/User.php
@@ -185,8 +185,6 @@
        public $mName;
        /** @var string */
        public $mRealName;
-       /** @var Password|null */
-       private $mPassword = null;
 
        /** @var string */
        public $mEmail;
@@ -2400,32 +2398,32 @@
 
        /**
         * Actually set the password and such
+        * @since 1.27 cannot set a password for a user not in the database
         * @param string|null $str New password to set or null to set an invalid
         *  password hash meaning that the user will not be able to log in
         *  through the web interface.
         */
        private function setPasswordInternal( $str ) {
                $id = self::idFromName( $this->getName() );
-               if ( $id ) {
-                       $passwordFactory = new PasswordFactory();
-                       $passwordFactory->init( 
RequestContext::getMain()->getConfig() );
-                       $dbw = wfGetDB( DB_MASTER );
-                       $dbw->update(
-                               'user',
-                               array(
-                                       'user_password' => 
$passwordFactory->newFromPlaintext( $str )->toString(),
-                                       'user_newpassword' => 
PasswordFactory::newInvalidPassword()->toString(),
-                                       'user_newpass_time' => 
$dbw->timestampOrNull( null ),
-                               ),
-                               array(
-                                       'user_id' => $id,
-                               ),
-                               __METHOD__
-                       );
-                       $this->mPassword = null;
-               } else {
-                       $this->mPassword = $str;
+               if ( $id == 0 ) {
+                       throw new LogicException( 'Cannot set a password for a 
user that is not in the database.' );
                }
+
+               $passwordFactory = new PasswordFactory();
+               $passwordFactory->init( RequestContext::getMain()->getConfig() 
);
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->update(
+                       'user',
+                       array(
+                               'user_password' => 
$passwordFactory->newFromPlaintext( $str )->toString(),
+                               'user_newpassword' => 
PasswordFactory::newInvalidPassword()->toString(),
+                               'user_newpass_time' => $dbw->timestampOrNull( 
null ),
+                       ),
+                       array(
+                               'user_id' => $id,
+                       ),
+                       __METHOD__
+               );
        }
 
        /**
@@ -3881,11 +3879,6 @@
                        return Status::newFatal( 'userexists' );
                }
                $this->mId = $dbw->insertId();
-
-               // Set the password now that it's in the DB, if applicable
-               if ( $this->mPassword !== null ) {
-                       $this->setPasswordInternal( $this->mPassword );
-               }
 
                // Clear instance cache other than user table data, which is 
already accurate
                $this->clearInstanceCache();
diff --git a/maintenance/createAndPromote.php b/maintenance/createAndPromote.php
index 861b364..c1a2022 100644
--- a/maintenance/createAndPromote.php
+++ b/maintenance/createAndPromote.php
@@ -106,6 +106,12 @@
                        }
                }
 
+               if ( !$exists ) {
+                       # Insert the account into the database
+                       $user->addToDatabase();
+                       $user->saveSettings();
+               }
+
                if ( $password ) {
                        # Try to set the password
                        try {
@@ -117,12 +123,6 @@
                        } catch ( PasswordError $pwe ) {
                                $this->error( $pwe->getText(), true );
                        }
-               }
-
-               if ( !$exists ) {
-                       # Insert the account into the database
-                       $user->addToDatabase();
-                       $user->saveSettings();
                }
 
                # Promote user

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Id3d40742f2e2b197ad6facd149cc6350006bf289
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: GergÅ‘ Tisza <[email protected]>
Gerrit-Reviewer: Parent5446 <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to