MediaWiki  master
ApiUnblock.php
Go to the documentation of this file.
1 <?php
28 
35 class ApiUnblock extends ApiBase {
36 
38 
41 
44 
47 
48  public function __construct(
49  ApiMain $main,
50  $action,
54  ) {
55  parent::__construct( $main, $action );
56 
57  $this->permissionCheckerFactory = $permissionCheckerFactory;
58  $this->unblockUserFactory = $unblockUserFactory;
59  $this->userIdentityLookup = $userIdentityLookup;
60  }
61 
65  public function execute() {
66  $performer = $this->getUser();
67  $params = $this->extractRequestParams();
68 
69  $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
70 
71  if ( !$this->getAuthority()->isAllowed( 'block' ) ) {
72  $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
73  }
74 
75  if ( $params['userid'] !== null ) {
76  $identity = $this->userIdentityLookup->getUserIdentityByUserId( $params['userid'] );
77  if ( !$identity ) {
78  $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
79  }
80  $params['user'] = $identity->getName();
81  }
82 
83  $target = $params['id'] === null ? $params['user'] : "#{$params['id']}";
84 
85  # T17810: blocked admins should have limited access here
86  $status = $this->permissionCheckerFactory
87  ->newBlockPermissionChecker(
88  $target,
89  $this->getAuthority()
90  )->checkBlockPermissions();
91  if ( $status !== true ) {
92  $this->dieWithError(
93  $status,
94  null,
95  [ 'blockinfo' => $this->getBlockDetails( $performer->getBlock() ) ]
96  );
97  }
98 
99  $status = $this->unblockUserFactory->newUnblockUser(
100  $target,
101  $this->getAuthority(),
102  $params['reason'],
103  $params['tags'] ?? []
104  )->unblock();
105 
106  if ( !$status->isOK() ) {
107  $this->dieStatus( $status );
108  }
109 
110  $block = $status->getValue();
111  $targetName = $block->getType() === DatabaseBlock::TYPE_AUTO ? '' : $block->getTargetName();
112  $targetUserId = $block->getTargetUserIdentity() ? $block->getTargetUserIdentity()->getId() : 0;
113  $res = [
114  'id' => $block->getId(),
115  'user' => $targetName,
116  'userid' => $targetUserId,
117  'reason' => $params['reason']
118  ];
119  $this->getResult()->addValue( null, $this->getModuleName(), $res );
120  }
121 
122  public function mustBePosted() {
123  return true;
124  }
125 
126  public function isWriteMode() {
127  return true;
128  }
129 
130  public function getAllowedParams() {
131  return [
132  'id' => [
133  ApiBase::PARAM_TYPE => 'integer',
134  ],
135  'user' => [
136  ApiBase::PARAM_TYPE => 'user',
137  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'cidr', 'id' ],
138  ],
139  'userid' => [
140  ApiBase::PARAM_TYPE => 'integer',
142  ],
143  'reason' => '',
144  'tags' => [
145  ApiBase::PARAM_TYPE => 'tags',
146  ApiBase::PARAM_ISMULTI => true,
147  ],
148  ];
149  }
150 
151  public function needsToken() {
152  return 'csrf';
153  }
154 
155  protected function getExamplesMessages() {
156  return [
157  'action=unblock&id=105'
158  => 'apihelp-unblock-example-id',
159  'action=unblock&user=Bob&reason=Sorry%20Bob'
160  => 'apihelp-unblock-example-user',
161  ];
162  }
163 
164  public function getHelpUrls() {
165  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
166  }
167 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
MediaWiki\Block\BlockPermissionCheckerFactory
Factory class for BlockPermissionChecker.
Definition: BlockPermissionCheckerFactory.php:33
ApiUnblock\$permissionCheckerFactory
BlockPermissionCheckerFactory $permissionCheckerFactory
Definition: ApiUnblock.php:40
ApiUnblock\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUnblock.php:130
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1379
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ApiUnblock\__construct
__construct(ApiMain $main, $action, BlockPermissionCheckerFactory $permissionCheckerFactory, UnblockUserFactory $unblockUserFactory, UserIdentityLookup $userIdentityLookup)
Definition: ApiUnblock.php:48
ApiUnblock\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUnblock.php:126
$res
$res
Definition: testCompression.php:57
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
ApiUnblock\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUnblock.php:155
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:55
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:26
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
Definition: ApiBase.php:77
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:65
ApiUnblock
API module that facilitates the unblocking of users.
Definition: ApiUnblock.php:35
ApiUnblock\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUnblock.php:164
MediaWiki\Block\UnblockUserFactory
Definition: UnblockUserFactory.php:30
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
ApiBlockInfoTrait
trait ApiBlockInfoTrait
Definition: ApiBlockInfoTrait.php:27
MediaWiki\User\UserIdentityLookup
Definition: UserIdentityLookup.php:33
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:844
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiUnblock\$userIdentityLookup
UserIdentityLookup $userIdentityLookup
Definition: ApiUnblock.php:46
ApiUnblock\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiUnblock.php:122
ApiUnblock\$unblockUserFactory
UnblockUserFactory $unblockUserFactory
Definition: ApiUnblock.php:43
ApiUnblock\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUnblock.php:151
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1442
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71