MediaWiki  1.34.0
ApiUnblock.php
Go to the documentation of this file.
1 <?php
24 
31 class ApiUnblock extends ApiBase {
32 
33  use ApiBlockInfoTrait;
34 
38  public function execute() {
39  $user = $this->getUser();
40  $params = $this->extractRequestParams();
41 
42  $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
43 
44  if ( !$this->getPermissionManager()->userHasRight( $user, 'block' ) ) {
45  $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
46  }
47  # T17810: blocked admins should have limited access here
48  $block = $user->getBlock();
49  if ( $block ) {
50  $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
51  if ( $status !== true ) {
52  $this->dieWithError(
53  $status,
54  null,
55  [ 'blockinfo' => $this->getBlockDetails( $block ) ]
56  );
57  }
58  }
59 
60  // Check if user can add tags
61  if ( !is_null( $params['tags'] ) ) {
62  $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
63  if ( !$ableToTag->isOK() ) {
64  $this->dieStatus( $ableToTag );
65  }
66  }
67 
68  if ( $params['userid'] !== null ) {
69  $username = User::whoIs( $params['userid'] );
70 
71  if ( $username === false ) {
72  $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
73  } else {
74  $params['user'] = $username;
75  }
76  }
77 
78  $data = [
79  'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
80  'Reason' => $params['reason'],
81  'Tags' => $params['tags']
82  ];
83  $block = DatabaseBlock::newFromTarget( $data['Target'] );
84  $retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
85  if ( $retval !== true ) {
86  $this->dieStatus( $this->errorArrayToStatus( $retval ) );
87  }
88 
89  $target = $block->getType() == DatabaseBlock::TYPE_AUTO ? '' : $block->getTarget();
90  $res = [
91  'id' => $block->getId(),
92  'user' => $target instanceof User ? $target->getName() : $target,
93  'userid' => $target instanceof User ? $target->getId() : 0,
94  'reason' => $params['reason']
95  ];
96  $this->getResult()->addValue( null, $this->getModuleName(), $res );
97  }
98 
99  public function mustBePosted() {
100  return true;
101  }
102 
103  public function isWriteMode() {
104  return true;
105  }
106 
107  public function getAllowedParams() {
108  return [
109  'id' => [
110  ApiBase::PARAM_TYPE => 'integer',
111  ],
112  'user' => null,
113  'userid' => [
114  ApiBase::PARAM_TYPE => 'integer'
115  ],
116  'reason' => '',
117  'tags' => [
118  ApiBase::PARAM_TYPE => 'tags',
119  ApiBase::PARAM_ISMULTI => true,
120  ],
121  ];
122  }
123 
124  public function needsToken() {
125  return 'csrf';
126  }
127 
128  protected function getExamplesMessages() {
129  return [
130  'action=unblock&id=105'
131  => 'apihelp-unblock-example-id',
132  'action=unblock&user=Bob&reason=Sorry%20Bob'
133  => 'apihelp-unblock-example-user',
134  ];
135  }
136 
137  public function getHelpUrls() {
138  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
139  }
140 }
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:40
User\getId
getId()
Get the user's ID.
Definition: User.php:2203
SpecialBlock\checkUnblockSelf
static checkUnblockSelf( $target, User $performer)
T17810: blocked admins should not be able to block/unblock others, and probably shouldn't be able to ...
Definition: SpecialBlock.php:1148
ApiUnblock\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUnblock.php:107
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
ApiUnblock\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUnblock.php:103
$res
$res
Definition: testCompression.php:52
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiUnblock\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUnblock.php:128
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:54
ApiUnblock\execute
execute()
Unblocks the specified user or provides the reason the unblock failed.
Definition: ApiUnblock.php:38
ApiUnblock
API module that facilitates the unblocking of users.
Definition: ApiUnblock.php:31
ApiUnblock\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUnblock.php:137
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
User\whoIs
static whoIs( $id)
Get the username corresponding to a given user ID.
Definition: User.php:809
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:710
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:893
ApiUnblock\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiUnblock.php:99
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:521
$status
return $status
Definition: SyntaxHighlight.php:347
ApiUnblock\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUnblock.php:124
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:2086
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2232
ApiBase\errorArrayToStatus
errorArrayToStatus(array $errors, User $user=null)
Turn an array of message keys or key+param arrays into a Status.
Definition: ApiBase.php:1825