MediaWiki REL1_37
ApiUnblock.php
Go to the documentation of this file.
1<?php
28
35class 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',
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}
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
const PARAM_DEPRECATED
Definition ApiBase.php:101
const PARAM_TYPE
Definition ApiBase.php:81
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:901
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1495
const PARAM_ISMULTI
Definition ApiBase.php:77
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:49
API module that facilitates the unblocking of users.
isWriteMode()
Indicates whether this module requires write mode.
needsToken()
Returns the token type this module requires in order to execute.
execute()
Unblocks the specified user or provides the reason the unblock failed.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
mustBePosted()
Indicates whether this module must be called with a POST request.
BlockPermissionCheckerFactory $permissionCheckerFactory
__construct(ApiMain $main, $action, BlockPermissionCheckerFactory $permissionCheckerFactory, UnblockUserFactory $unblockUserFactory, UserIdentityLookup $userIdentityLookup)
UserIdentityLookup $userIdentityLookup
UnblockUserFactory $unblockUserFactory
getExamplesMessages()
Returns usage examples for this module.
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Type definition for user types.
Definition UserDef.php:25
trait ApiBlockInfoTrait