Block API: Allow blocking/unblocking by user's ID
authorSubin Siby <subins2000@gmail.com>
Wed, 7 Dec 2016 17:04:02 +0000 (22:34 +0530)
committerSubin Siby <subins2000@gmail.com>
Tue, 13 Dec 2016 12:46:24 +0000 (18:16 +0530)
Add feature to block/unblock users by their ID. For this,a new
parameter `userid` is added to block & unblock API request.

Bug: T34496
Change-Id: I084a4e275cd937053c505cd388a365b316990ece

includes/api/ApiBlock.php
includes/api/ApiUnblock.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json
tests/phpunit/includes/api/ApiBlockTest.php

index a4ea385..3774f09 100644 (file)
@@ -46,6 +46,8 @@ class ApiBlock extends ApiBase {
                $user = $this->getUser();
                $params = $this->extractRequestParams();
 
+               $this->requireOnlyOneParameter( $params, 'user', 'userid' );
+
                # bug 15810: blocked admins should have limited access here
                if ( $user->isBlocked() ) {
                        $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
@@ -58,13 +60,24 @@ class ApiBlock extends ApiBase {
                        }
                }
 
-               $target = User::newFromName( $params['user'] );
-               // Bug 38633 - if the target is a user (not an IP address), but it
-               // doesn't exist or is unusable, error.
-               if ( $target instanceof User &&
-                       ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
-               ) {
-                       $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
+               if ( $params['userid'] !== null ) {
+                       $username = User::whoIs( $params['userid'] );
+
+                       if ( $username === false ) {
+                               $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
+                       } else {
+                               $params['user'] = $username;
+                       }
+               } else {
+                       $target = User::newFromName( $params['user'] );
+
+                       // Bug 38633 - if the target is a user (not an IP address), but it
+                       // doesn't exist or is unusable, error.
+                       if ( $target instanceof User &&
+                               ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
+                       ) {
+                               $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
+                       }
                }
 
                if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
@@ -137,7 +150,9 @@ class ApiBlock extends ApiBase {
                return [
                        'user' => [
                                ApiBase::PARAM_TYPE => 'user',
-                               ApiBase::PARAM_REQUIRED => true
+                       ],
+                       'userid' => [
+                               ApiBase::PARAM_TYPE => 'integer',
                        ],
                        'expiry' => 'never',
                        'reason' => '',
index 523a888..3eeb7a4 100644 (file)
@@ -39,7 +39,7 @@ class ApiUnblock extends ApiBase {
                $user = $this->getUser();
                $params = $this->extractRequestParams();
 
-               $this->requireOnlyOneParameter( $params, 'id', 'user' );
+               $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
 
                if ( !$user->isAllowed( 'block' ) ) {
                        $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
@@ -64,6 +64,16 @@ class ApiUnblock extends ApiBase {
                        }
                }
 
+               if ( $params['userid'] !== null ) {
+                       $username = User::whoIs( $params['userid'] );
+
+                       if ( $username === false ) {
+                               $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
+                       } else {
+                               $params['user'] = $username;
+                       }
+               }
+
                $data = [
                        'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
                        'Reason' => $params['reason'],
@@ -97,6 +107,9 @@ class ApiUnblock extends ApiBase {
                                ApiBase::PARAM_TYPE => 'integer',
                        ],
                        'user' => null,
+                       'userid' => [
+                               ApiBase::PARAM_TYPE => 'integer'
+                       ],
                        'reason' => '',
                        'tags' => [
                                ApiBase::PARAM_TYPE => 'tags',
index a37b7cf..240e167 100644 (file)
@@ -25,7 +25,8 @@
        "apihelp-main-param-errorsuselocal": "If given, error texts will use locally-customized messages from the {{ns:MediaWiki}} namespace.",
 
        "apihelp-block-description": "Block a user.",
-       "apihelp-block-param-user": "Username, IP address, or IP address range to block.",
+       "apihelp-block-param-user": "Username, IP address, or IP address range to block. Cannot be used together with <var>$1userid</var>",
+       "apihelp-block-param-userid": "User ID to block. Cannot be used together with <var>$1user</var>.",
        "apihelp-block-param-expiry": "Expiry time. May be relative (e.g. <kbd>5 months</kbd> or <kbd>2 weeks</kbd>) or absolute (e.g. <kbd>2014-09-18T12:34:56Z</kbd>). If set to <kbd>infinite</kbd>, <kbd>indefinite</kbd>, or <kbd>never</kbd>, the block will never expire.",
        "apihelp-block-param-reason": "Reason for block.",
        "apihelp-block-param-anononly": "Block anonymous users only (i.e. disable anonymous edits for this IP address).",
        "apihelp-tokens-example-emailmove": "Retrieve an email token and a move token.",
 
        "apihelp-unblock-description": "Unblock a user.",
-       "apihelp-unblock-param-id": "ID of the block to unblock (obtained through <kbd>list=blocks</kbd>). Cannot be used together with <var>$1user</var>.",
-       "apihelp-unblock-param-user": "Username, IP address or IP address range to unblock. Cannot be used together with <var>$1id</var>.",
+       "apihelp-unblock-param-id": "ID of the block to unblock (obtained through <kbd>list=blocks</kbd>). Cannot be used together with <var>$1user</var> or <var>$luserid</var>.",
+       "apihelp-unblock-param-user": "Username, IP address or IP address range to unblock. Cannot be used together with <var>$1id</var> or <var>$luserid</var>.",
+       "apihelp-unblock-param-userid": "User ID to unblock. Cannot be used together with <var>$1id</var> or <var>$1user</var>.",
        "apihelp-unblock-param-reason": "Reason for unblock.",
        "apihelp-unblock-param-tags": "Change tags to apply to the entry in the block log.",
        "apihelp-unblock-example-id": "Unblock block ID #<kbd>105</kbd>.",
        "apierror-nosuchrevid": "There is no revision with ID $1.",
        "apierror-nosuchsection": "There is no section $1.",
        "apierror-nosuchsection-what": "There is no section $1 in $2.",
+       "apierror-nosuchuserid": "There is no user with ID $1.",
        "apierror-notarget": "You have not specified a valid target for this action.",
        "apierror-notpatrollable": "The revision r$1 can't be patrolled as it's too old.",
        "apierror-nouploadmodule": "No upload module set.",
index 0b78fa5..2a1c913 100644 (file)
@@ -35,6 +35,7 @@
        "apihelp-main-param-errorsuselocal": "{{doc-apihelp-param|main|errorsuselocal}}",
        "apihelp-block-description": "{{doc-apihelp-description|block}}",
        "apihelp-block-param-user": "{{doc-apihelp-param|block|user}}",
+       "apihelp-block-param-userid": "{{doc-apihelp-param|block|userid}}",
        "apihelp-block-param-expiry": "{{doc-apihelp-param|block|expiry}}\n{{doc-important|Do not translate \"5 months\", \"2 weeks\", \"infinite\", \"indefinite\" or \"never\"!}}",
        "apihelp-block-param-reason": "{{doc-apihelp-param|block|reason}}",
        "apihelp-block-param-anononly": "{{doc-apihelp-param|block|anononly}}\n* See also {{msg-mw|ipb-hardblock}}",
        "apihelp-unblock-description": "{{doc-apihelp-description|unblock}}",
        "apihelp-unblock-param-id": "{{doc-apihelp-param|unblock|id}}",
        "apihelp-unblock-param-user": "{{doc-apihelp-param|unblock|user}}",
+       "apihelp-unblock-param-userid": "{{doc-apihelp-param|unblock|userid}}",
        "apihelp-unblock-param-reason": "{{doc-apihelp-param|unblock|reason}}",
        "apihelp-unblock-param-tags": "{{doc-apihelp-param|unblock|tags}}",
        "apihelp-unblock-example-id": "{{doc-apihelp-example|unblock}}",
        "apierror-nosuchrevid": "{{doc-apierror}}\n\nParameters:\n* $1 - Revision ID number.",
        "apierror-nosuchsection": "{{doc-apierror}}\n\nParameters:\n* $1 - Section identifier. Probably a number or \"T-\" followed by a number.",
        "apierror-nosuchsection-what": "{{doc-apierror}}\n\nParameters:\n* $1 - Section identifier. Probably a number or \"T-\" followed by a number.\n* $2 - Page title, revision ID formatted with {{msg-mw|revid}}, or page ID formatted with {{msg-mw|pageid}}.",
+       "apierror-nosuchuserid": "{{doc-apierror}}",
        "apierror-notarget": "{{doc-apierror}}",
        "apierror-notpatrollable": "{{doc-apierror}}\n\nParameters:\n* $1 - Revision ID number.",
        "apierror-nouploadmodule": "{{doc-apierror}}",
index 08fc128..832a113 100644 (file)
@@ -13,6 +13,14 @@ class ApiBlockTest extends ApiTestCase {
                $this->doLogin();
        }
 
+       protected function tearDown() {
+               $block = Block::newFromTarget( 'UTApiBlockee' );
+               if ( !is_null( $block ) ) {
+                       $block->delete();
+               }
+               parent::tearDown();
+       }
+
        protected function getTokens() {
                return $this->getTokenList( self::$users['sysop'] );
        }
@@ -64,6 +72,35 @@ class ApiBlockTest extends ApiTestCase {
                $this->assertEquals( 'infinity', $block->mExpiry );
        }
 
+       /**
+        * Block by user ID
+        */
+       public function testMakeNormalBlockId() {
+               $tokens = $this->getTokens();
+               $user = User::newFromName( 'UTApiBlockee' );
+
+               if ( !$user->getId() ) {
+                       $this->markTestIncomplete( "The user UTApiBlockee does not exist." );
+               }
+
+               if ( !array_key_exists( 'blocktoken', $tokens ) ) {
+                       $this->markTestIncomplete( "No block token found" );
+               }
+
+               $data = $this->doApiRequest( [
+                       'action' => 'block',
+                       'userid' => $user->getId(),
+                       'reason' => 'Some reason',
+                       'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
+
+               $block = Block::newFromTarget( 'UTApiBlockee' );
+
+               $this->assertTrue( !is_null( $block ), 'Block is valid.' );
+               $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
+               $this->assertEquals( 'Some reason', $block->mReason );
+               $this->assertEquals( 'infinity', $block->mExpiry );
+       }
+
        /**
         * @expectedException ApiUsageException
         * @expectedExceptionMessage The "token" parameter must be set