Anomie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/294852

Change subject: Add $options parameter for testUserForCreation()
......................................................................

Add $options parameter for testUserForCreation()

This will allow providers to know whether the call is just for testing
(from ApiQueryUsers) or for actual creation, and skip duplicate work
when testForAccountCreation() is going to be called.

Change-Id: Id3ef713fd377135d78f66e5100dedd4689293b59
Depends-On: I4af8b3b692f60c42f8685b90be5936da7ba4e2e2
Depends-On: Ie9639a15d04b387be0e72754301eb6d91cd8adc2
Depends-On: I063cbdfbd9a223bf2391fce2b714ab82ddd3272f
Depends-On: I7c67512634f6e72251911238f083857da9fd3f84
---
M includes/auth/AbstractPreAuthenticationProvider.php
M includes/auth/AbstractPrimaryAuthenticationProvider.php
M includes/auth/AbstractSecondaryAuthenticationProvider.php
M includes/auth/AuthManager.php
M includes/auth/CheckBlocksSecondaryAuthenticationProvider.php
M includes/auth/LegacyHookPreAuthenticationProvider.php
M includes/auth/PreAuthenticationProvider.php
M includes/auth/PrimaryAuthenticationProvider.php
M includes/auth/SecondaryAuthenticationProvider.php
9 files changed, 54 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/52/294852/1

diff --git a/includes/auth/AbstractPreAuthenticationProvider.php 
b/includes/auth/AbstractPreAuthenticationProvider.php
index 48a9c88..d997dbb 100644
--- a/includes/auth/AbstractPreAuthenticationProvider.php
+++ b/includes/auth/AbstractPreAuthenticationProvider.php
@@ -45,7 +45,7 @@
                return \StatusValue::newGood();
        }
 
-       public function testUserForCreation( $user, $autocreate ) {
+       public function testUserForCreation( $user, $autocreate, array $options 
= [] ) {
                return \StatusValue::newGood();
        }
 
diff --git a/includes/auth/AbstractPrimaryAuthenticationProvider.php 
b/includes/auth/AbstractPrimaryAuthenticationProvider.php
index 2e0d669..ea3dfa3 100644
--- a/includes/auth/AbstractPrimaryAuthenticationProvider.php
+++ b/includes/auth/AbstractPrimaryAuthenticationProvider.php
@@ -91,7 +91,7 @@
        public function postAccountCreation( $user, $creator, 
AuthenticationResponse $response ) {
        }
 
-       public function testUserForCreation( $user, $autocreate ) {
+       public function testUserForCreation( $user, $autocreate, array $options 
= [] ) {
                return \StatusValue::newGood();
        }
 
diff --git a/includes/auth/AbstractSecondaryAuthenticationProvider.php 
b/includes/auth/AbstractSecondaryAuthenticationProvider.php
index 89fd6f9..00493bc 100644
--- a/includes/auth/AbstractSecondaryAuthenticationProvider.php
+++ b/includes/auth/AbstractSecondaryAuthenticationProvider.php
@@ -77,7 +77,7 @@
        public function postAccountCreation( $user, $creator, 
AuthenticationResponse $response ) {
        }
 
-       public function testUserForCreation( $user, $autocreate ) {
+       public function testUserForCreation( $user, $autocreate, array $options 
= [] ) {
                return \StatusValue::newGood();
        }
 
diff --git a/includes/auth/AuthManager.php b/includes/auth/AuthManager.php
index 7ca9c6a..f7be2d7 100644
--- a/includes/auth/AuthManager.php
+++ b/includes/auth/AuthManager.php
@@ -878,10 +878,21 @@
        /**
         * Determine whether a particular account can be created
         * @param string $username
-        * @param int $flags Bitfield of User:READ_* constants
+        * @param array $options
+        *  - flags: (int) Bitfield of User:READ_* constants, default 
User::READ_NORMAL
         * @return Status
         */
-       public function canCreateAccount( $username, $flags = User::READ_NORMAL 
) {
+       public function canCreateAccount( $username, $options = [] ) {
+               // Back compat
+               if ( is_int( $options ) ) {
+                       $options = [ 'flags' => $options ];
+               }
+               $options += [
+                       'flags' => User::READ_NORMAL,
+                       'creating' => false,
+               ];
+               $flags = $options['flags'];
+
                if ( !$this->canCreateAccounts() ) {
                        return Status::newFatal( 'authmanager-create-disabled' 
);
                }
@@ -905,7 +916,7 @@
                        $this->getPrimaryAuthenticationProviders() +
                        $this->getSecondaryAuthenticationProviders();
                foreach ( $providers as $provider ) {
-                       $status = $provider->testUserForCreation( $user, false 
);
+                       $status = $provider->testUserForCreation( $user, false, 
$options );
                        if ( !$status->isGood() ) {
                                return Status::wrap( $status );
                        }
@@ -1010,7 +1021,9 @@
                        return AuthenticationResponse::newFail( 
$status->getMessage() );
                }
 
-               $status = $this->canCreateAccount( $username, 
User::READ_LOCKING );
+               $status = $this->canCreateAccount(
+                       $username, [ 'flags' => User::READ_LOCKING, 'creating' 
=> true ]
+               );
                if ( !$status->isGood() ) {
                        $this->logger->debug( __METHOD__ . ': {user} cannot be 
created: {reason}', [
                                'user' => $username,
@@ -1575,11 +1588,15 @@
                }
 
                // Denied by providers?
+               $options = [
+                       'flags' => User::READ_LATEST,
+                       'creating' => true,
+               ];
                $providers = $this->getPreAuthenticationProviders() +
                        $this->getPrimaryAuthenticationProviders() +
                        $this->getSecondaryAuthenticationProviders();
                foreach ( $providers as $provider ) {
-                       $status = $provider->testUserForCreation( $user, 
$source );
+                       $status = $provider->testUserForCreation( $user, 
$source, $options );
                        if ( !$status->isGood() ) {
                                $ret = Status::wrap( $status );
                                $this->logger->debug( __METHOD__ . ': Provider 
denied creation of {username}: {reason}', [
diff --git a/includes/auth/CheckBlocksSecondaryAuthenticationProvider.php 
b/includes/auth/CheckBlocksSecondaryAuthenticationProvider.php
index 070da9f..54ccdf4 100644
--- a/includes/auth/CheckBlocksSecondaryAuthenticationProvider.php
+++ b/includes/auth/CheckBlocksSecondaryAuthenticationProvider.php
@@ -75,7 +75,7 @@
                return AuthenticationResponse::newAbstain();
        }
 
-       public function testUserForCreation( $user, $autocreate ) {
+       public function testUserForCreation( $user, $autocreate, array $options 
= [] ) {
                $block = $user->isBlockedFromCreateAccount();
                if ( $block ) {
                        $errorParams = [
diff --git a/includes/auth/LegacyHookPreAuthenticationProvider.php 
b/includes/auth/LegacyHookPreAuthenticationProvider.php
index 1a8a758..c98a184 100644
--- a/includes/auth/LegacyHookPreAuthenticationProvider.php
+++ b/includes/auth/LegacyHookPreAuthenticationProvider.php
@@ -96,7 +96,7 @@
                return StatusValue::newGood();
        }
 
-       public function testUserForCreation( $user, $autocreate ) {
+       public function testUserForCreation( $user, $autocreate, array $options 
= [] ) {
                if ( $autocreate !== false ) {
                        $abortError = '';
                        if ( !\Hooks::run( 'AbortAutoAccount', [ $user, 
&$abortError ] ) ) {
diff --git a/includes/auth/PreAuthenticationProvider.php 
b/includes/auth/PreAuthenticationProvider.php
index 846d16e..13fae6e 100644
--- a/includes/auth/PreAuthenticationProvider.php
+++ b/includes/auth/PreAuthenticationProvider.php
@@ -83,9 +83,17 @@
         *   into such.
         * @param bool|string $autocreate False if this is not an 
auto-creation, or
         *  the source of the auto-creation passed to 
AuthManager::autoCreateUser().
+        * @param array $options
+        *  - flags: (int) Bitfield of User:READ_* constants, default 
User::READ_NORMAL
+        *  - creating: (bool) If false (or missing), this call is only testing 
if
+        *    a user could be created. If set, this (non-autocreation) is for
+        *    actually creating an account and will be followed by a call to
+        *    testForAccountCreation(). In this case, the provider might return
+        *    StatusValue::newGood() here and let the later call to
+        *    testForAccountCreation() do a more thorough test.
         * @return StatusValue
         */
-       public function testUserForCreation( $user, $autocreate );
+       public function testUserForCreation( $user, $autocreate, array $options 
= [] );
 
        /**
         * Post-creation callback
diff --git a/includes/auth/PrimaryAuthenticationProvider.php 
b/includes/auth/PrimaryAuthenticationProvider.php
index 169e7f1..c44c8fc 100644
--- a/includes/auth/PrimaryAuthenticationProvider.php
+++ b/includes/auth/PrimaryAuthenticationProvider.php
@@ -277,9 +277,17 @@
         *   into such.
         * @param bool|string $autocreate False if this is not an 
auto-creation, or
         *  the source of the auto-creation passed to 
AuthManager::autoCreateUser().
+        * @param array $options
+        *  - flags: (int) Bitfield of User:READ_* constants, default 
User::READ_NORMAL
+        *  - creating: (bool) If false (or missing), this call is only testing 
if
+        *    a user could be created. If set, this (non-autocreation) is for
+        *    actually creating an account and will be followed by a call to
+        *    testForAccountCreation(). In this case, the provider might return
+        *    StatusValue::newGood() here and let the later call to
+        *    testForAccountCreation() do a more thorough test.
         * @return StatusValue
         */
-       public function testUserForCreation( $user, $autocreate );
+       public function testUserForCreation( $user, $autocreate, array $options 
= [] );
 
        /**
         * Post-auto-creation callback
diff --git a/includes/auth/SecondaryAuthenticationProvider.php 
b/includes/auth/SecondaryAuthenticationProvider.php
index 0d52d25..1ccc9c6 100644
--- a/includes/auth/SecondaryAuthenticationProvider.php
+++ b/includes/auth/SecondaryAuthenticationProvider.php
@@ -200,9 +200,17 @@
         *   into such.
         * @param bool|string $autocreate False if this is not an 
auto-creation, or
         *  the source of the auto-creation passed to 
AuthManager::autoCreateUser().
+        * @param array $options
+        *  - flags: (int) Bitfield of User:READ_* constants, default 
User::READ_NORMAL
+        *  - creating: (bool) If false (or missing), this call is only testing 
if
+        *    a user could be created. If set, this (non-autocreation) is for
+        *    actually creating an account and will be followed by a call to
+        *    testForAccountCreation(). In this case, the provider might return
+        *    StatusValue::newGood() here and let the later call to
+        *    testForAccountCreation() do a more thorough test.
         * @return StatusValue
         */
-       public function testUserForCreation( $user, $autocreate );
+       public function testUserForCreation( $user, $autocreate, array $options 
= [] );
 
        /**
         * Post-auto-creation callback

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id3ef713fd377135d78f66e5100dedd4689293b59
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <bjor...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to