MediaWiki 1.43.1
ApiUnblock.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
37
44class ApiUnblock extends ApiBase {
45
48
49 private BlockPermissionCheckerFactory $permissionCheckerFactory;
50 private UnblockUserFactory $unblockUserFactory;
51 private UserIdentityLookup $userIdentityLookup;
52 private WatchedItemStoreInterface $watchedItemStore;
53
54 public function __construct(
55 ApiMain $main,
56 string $action,
57 BlockPermissionCheckerFactory $permissionCheckerFactory,
58 UnblockUserFactory $unblockUserFactory,
59 UserIdentityLookup $userIdentityLookup,
60 WatchedItemStoreInterface $watchedItemStore,
61 WatchlistManager $watchlistManager,
62 UserOptionsLookup $userOptionsLookup
63 ) {
64 parent::__construct( $main, $action );
65
66 $this->permissionCheckerFactory = $permissionCheckerFactory;
67 $this->unblockUserFactory = $unblockUserFactory;
68 $this->userIdentityLookup = $userIdentityLookup;
69 $this->watchedItemStore = $watchedItemStore;
70
71 // Variables needed in ApiWatchlistTrait trait
72 $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
73 $this->watchlistMaxDuration =
75 $this->watchlistManager = $watchlistManager;
76 $this->userOptionsLookup = $userOptionsLookup;
77 }
78
82 public function execute() {
83 $performer = $this->getUser();
85
86 $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
87
88 if ( !$this->getAuthority()->isAllowed( 'block' ) ) {
89 $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
90 }
91
92 if ( $params['userid'] !== null ) {
93 $identity = $this->userIdentityLookup->getUserIdentityByUserId( $params['userid'] );
94 if ( !$identity ) {
95 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
96 }
97 $params['user'] = $identity->getName();
98 }
99
100 $target = $params['id'] === null ? $params['user'] : "#{$params['id']}";
101
102 # T17810: blocked admins should have limited access here
103 $status = $this->permissionCheckerFactory
104 ->newBlockPermissionChecker(
105 $target,
106 $this->getAuthority()
107 )->checkBlockPermissions();
108 if ( $status !== true ) {
109 $this->dieWithError(
110 $status,
111 null,
112 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
113 [ 'blockinfo' => $this->getBlockDetails( $performer->getBlock() ) ]
114 );
115 }
116
117 $status = $this->unblockUserFactory->newUnblockUser(
118 $target,
119 $this->getAuthority(),
120 $params['reason'],
121 $params['tags'] ?? []
122 )->unblock();
123
124 if ( !$status->isOK() ) {
125 $this->dieStatus( $status );
126 }
127
128 $block = $status->getValue();
129 $targetType = $block->getType();
130 $targetName = $targetType === Block::TYPE_AUTO ? '' : $block->getTargetName();
131 $targetUserId = $block->getTargetUserIdentity() ? $block->getTargetUserIdentity()->getId() : 0;
132
133 $watchlistExpiry = $this->getExpiryFromParams( $params );
134 $watchuser = $params['watchuser'];
135 $userPage = Title::makeTitle( NS_USER, $targetName );
136 if ( $watchuser && $targetType !== Block::TYPE_RANGE && $targetType !== Block::TYPE_AUTO ) {
137 $this->setWatch( 'watch', $userPage, $this->getUser(), null, $watchlistExpiry );
138 } else {
139 $watchuser = false;
140 $watchlistExpiry = null;
141 }
142
143 $res = [
144 'id' => $block->getId(),
145 'user' => $targetName,
146 'userid' => $targetUserId,
147 'reason' => $params['reason'],
148 'watchuser' => $watchuser,
149 ];
150 if ( $watchlistExpiry !== null ) {
151 $res['watchlistexpiry'] = $this->getWatchlistExpiry(
152 $this->watchedItemStore,
153 $userPage,
154 $this->getUser()
155 );
156 }
157 $this->getResult()->addValue( null, $this->getModuleName(), $res );
158 }
159
160 public function mustBePosted() {
161 return true;
162 }
163
164 public function isWriteMode() {
165 return true;
166 }
167
168 public function getAllowedParams() {
169 $params = [
170 'id' => [
171 ParamValidator::PARAM_TYPE => 'integer',
172 ],
173 'user' => [
174 ParamValidator::PARAM_TYPE => 'user',
175 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'cidr', 'id' ],
176 ],
177 'userid' => [
178 ParamValidator::PARAM_TYPE => 'integer',
179 ParamValidator::PARAM_DEPRECATED => true,
180 ],
181 'reason' => '',
182 'tags' => [
183 ParamValidator::PARAM_TYPE => 'tags',
184 ParamValidator::PARAM_ISMULTI => true,
185 ],
186 'watchuser' => false,
187 ];
188
189 // Params appear in the docs in the order they are defined,
190 // which is why this is here and not at the bottom.
191 // @todo Find better way to support insertion at arbitrary position
192 if ( $this->watchlistExpiryEnabled ) {
193 $params += [
194 'watchlistexpiry' => [
195 ParamValidator::PARAM_TYPE => 'expiry',
196 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
197 ExpiryDef::PARAM_USE_MAX => true,
198 ]
199 ];
200 }
201
202 return $params;
203 }
204
205 public function needsToken() {
206 return 'csrf';
207 }
208
209 protected function getExamplesMessages() {
210 return [
211 'action=unblock&id=105'
212 => 'apihelp-unblock-example-id',
213 'action=unblock&user=Bob&reason=Sorry%20Bob'
214 => 'apihelp-unblock-example-user',
215 ];
216 }
217
218 public function getHelpUrls() {
219 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
220 }
221}
222
224class_alias( ApiUnblock::class, 'ApiUnblock' );
const NS_USER
Definition Defines.php:67
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
getResult()
Get the result object.
Definition ApiBase.php:710
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1632
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:990
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
API module that facilitates the unblocking of users.
mustBePosted()
Indicates whether this module must be called with a POST request.
needsToken()
Returns the token type this module requires in order to execute.
execute()
Unblocks the specified user or provides the reason the unblock failed.
isWriteMode()
Indicates whether this module requires write access to the wiki.
__construct(ApiMain $main, string $action, BlockPermissionCheckerFactory $permissionCheckerFactory, UnblockUserFactory $unblockUserFactory, UserIdentityLookup $userIdentityLookup, WatchedItemStoreInterface $watchedItemStore, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
A class containing constants representing the names of configuration variables.
const WatchlistExpiry
Name constant for the WatchlistExpiry setting, for use with Config::get()
const WatchlistExpiryMaxDuration
Name constant for the WatchlistExpiryMaxDuration setting, for use with Config::get()
Type definition for user types.
Definition UserDef.php:27
Represents a title within MediaWiki.
Definition Title.php:78
Provides access to user options.
Service for formatting and validating API parameters.
Type definition for expiry timestamps.
Definition ExpiryDef.php:17
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...
Represents a block that may prevent users from performing specific operations.
Definition Block.php:45
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
setWatch(string $watch, PageIdentity $page, User $user, ?string $userOption=null, ?string $expiry=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
getWatchlistExpiry(WatchedItemStoreInterface $store, PageIdentity $page, UserIdentity $user)
Get existing expiry from the database.