MediaWiki  master
ApiUnblock.php
Go to the documentation of this file.
1 <?php
25 
32 class ApiUnblock extends ApiBase {
33 
35 
39  public function execute() {
40  $user = $this->getUser();
41  $params = $this->extractRequestParams();
42 
43  $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
44 
45  if ( !$this->getPermissionManager()->userHasRight( $user, 'block' ) ) {
46  $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
47  }
48  # T17810: blocked admins should have limited access here
49  $block = $user->getBlock();
50  if ( $block ) {
51  $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
52  if ( $status !== true ) {
53  $this->dieWithError(
54  $status,
55  null,
56  [ 'blockinfo' => $this->getBlockDetails( $block ) ]
57  );
58  }
59  }
60 
61  // Check if user can add tags
62  if ( $params['tags'] !== null ) {
63  $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
64  if ( !$ableToTag->isOK() ) {
65  $this->dieStatus( $ableToTag );
66  }
67  }
68 
69  if ( $params['userid'] !== null ) {
70  $username = User::whoIs( $params['userid'] );
71 
72  if ( $username === false ) {
73  $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
74  } else {
75  $params['user'] = $username;
76  }
77  }
78 
79  $data = [
80  'Target' => $params['id'] === null ? $params['user'] : "#{$params['id']}",
81  'Reason' => $params['reason'],
82  'Tags' => $params['tags']
83  ];
84  $block = DatabaseBlock::newFromTarget( $data['Target'] );
85  $retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
86  if ( $retval !== true ) {
87  $this->dieStatus( $this->errorArrayToStatus( $retval ) );
88  }
89 
90  $target = $block->getType() == DatabaseBlock::TYPE_AUTO ? '' : $block->getTarget();
91  $res = [
92  'id' => $block->getId(),
93  'user' => $target instanceof User ? $target->getName() : $target,
94  'userid' => $target instanceof User ? $target->getId() : 0,
95  'reason' => $params['reason']
96  ];
97  $this->getResult()->addValue( null, $this->getModuleName(), $res );
98  }
99 
100  public function mustBePosted() {
101  return true;
102  }
103 
104  public function isWriteMode() {
105  return true;
106  }
107 
108  public function getAllowedParams() {
109  return [
110  'id' => [
111  ApiBase::PARAM_TYPE => 'integer',
112  ],
113  'user' => [
114  ApiBase::PARAM_TYPE => 'user',
115  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'cidr', 'id' ],
116  ],
117  'userid' => [
118  ApiBase::PARAM_TYPE => 'integer',
120  ],
121  'reason' => '',
122  'tags' => [
123  ApiBase::PARAM_TYPE => 'tags',
124  ApiBase::PARAM_ISMULTI => true,
125  ],
126  ];
127  }
128 
129  public function needsToken() {
130  return 'csrf';
131  }
132 
133  protected function getExamplesMessages() {
134  return [
135  'action=unblock&id=105'
136  => 'apihelp-unblock-example-id',
137  'action=unblock&user=Bob&reason=Sorry%20Bob'
138  => 'apihelp-unblock-example-user',
139  ];
140  }
141 
142  public function getHelpUrls() {
143  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
144  }
145 }
SpecialUnblock\processUnblock
static processUnblock(array $data, IContextSource $context)
Process the form.
Definition: SpecialUnblock.php:184
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:42
User\getId
getId()
Get the user's ID.
Definition: User.php:2025
SpecialBlock\checkUnblockSelf
static checkUnblockSelf( $target, User $performer)
T17810: Sitewide blocked admins should not be able to block/unblock others with one exception; they c...
Definition: SpecialBlock.php:1164
ApiUnblock\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUnblock.php:108
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
ApiUnblock\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUnblock.php:104
$res
$res
Definition: testCompression.php:57
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ApiUnblock\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUnblock.php:133
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:23
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:76
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:52
ApiUnblock\execute
execute()
Unblocks the specified user or provides the reason the unblock failed.
Definition: ApiUnblock.php:39
ApiUnblock
API module that facilitates the unblocking of users.
Definition: ApiUnblock.php:32
ApiUnblock\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUnblock.php:142
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
ApiBlockInfoTrait
trait ApiBlockInfoTrait
Definition: ApiBlockInfoTrait.php:27
ApiBase\requireOnlyOneParameter
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:854
User\whoIs
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition: User.php:865
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:637
ApiUnblock\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiUnblock.php:100
ChangeTags\canAddTagsAccompanyingChange
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Definition: ChangeTags.php:544
ApiUnblock\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUnblock.php:129
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1440
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:70
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2054
ApiBase\errorArrayToStatus
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1193