MediaWiki master
ApiUnblock.php
Go to the documentation of this file.
1<?php
35
42class ApiUnblock extends ApiBase {
43
46
47 private BlockPermissionCheckerFactory $permissionCheckerFactory;
48 private UnblockUserFactory $unblockUserFactory;
49 private UserIdentityLookup $userIdentityLookup;
50 private WatchedItemStoreInterface $watchedItemStore;
51
52 public function __construct(
53 ApiMain $main,
54 $action,
55 BlockPermissionCheckerFactory $permissionCheckerFactory,
56 UnblockUserFactory $unblockUserFactory,
57 UserIdentityLookup $userIdentityLookup,
58 WatchedItemStoreInterface $watchedItemStore,
59 WatchlistManager $watchlistManager,
60 UserOptionsLookup $userOptionsLookup
61 ) {
62 parent::__construct( $main, $action );
63
64 $this->permissionCheckerFactory = $permissionCheckerFactory;
65 $this->unblockUserFactory = $unblockUserFactory;
66 $this->userIdentityLookup = $userIdentityLookup;
67 $this->watchedItemStore = $watchedItemStore;
68
69 // Variables needed in ApiWatchlistTrait trait
70 $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
71 $this->watchlistMaxDuration =
72 $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
73 $this->watchlistManager = $watchlistManager;
74 $this->userOptionsLookup = $userOptionsLookup;
75 }
76
80 public function execute() {
81 $performer = $this->getUser();
83
84 $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
85
86 if ( !$this->getAuthority()->isAllowed( 'block' ) ) {
87 $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
88 }
89
90 if ( $params['userid'] !== null ) {
91 $identity = $this->userIdentityLookup->getUserIdentityByUserId( $params['userid'] );
92 if ( !$identity ) {
93 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
94 }
95 $params['user'] = $identity->getName();
96 }
97
98 $target = $params['id'] === null ? $params['user'] : "#{$params['id']}";
99
100 # T17810: blocked admins should have limited access here
101 $status = $this->permissionCheckerFactory
102 ->newBlockPermissionChecker(
103 $target,
104 $this->getAuthority()
105 )->checkBlockPermissions();
106 if ( $status !== true ) {
107 $this->dieWithError(
108 $status,
109 null,
110 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
111 [ 'blockinfo' => $this->getBlockDetails( $performer->getBlock() ) ]
112 );
113 }
114
115 $status = $this->unblockUserFactory->newUnblockUser(
116 $target,
117 $this->getAuthority(),
118 $params['reason'],
119 $params['tags'] ?? []
120 )->unblock();
121
122 if ( !$status->isOK() ) {
123 $this->dieStatus( $status );
124 }
125
126 $block = $status->getValue();
127 $targetType = $block->getType();
128 $targetName = $targetType === Block::TYPE_AUTO ? '' : $block->getTargetName();
129 $targetUserId = $block->getTargetUserIdentity() ? $block->getTargetUserIdentity()->getId() : 0;
130
131 $watchlistExpiry = $this->getExpiryFromParams( $params );
132 $watchuser = $params['watchuser'];
133 $userPage = Title::makeTitle( NS_USER, $targetName );
134 if ( $watchuser && $targetType !== Block::TYPE_RANGE && $targetType !== Block::TYPE_AUTO ) {
135 $this->setWatch( 'watch', $userPage, $this->getUser(), null, $watchlistExpiry );
136 } else {
137 $watchuser = false;
138 $watchlistExpiry = null;
139 }
140
141 $res = [
142 'id' => $block->getId(),
143 'user' => $targetName,
144 'userid' => $targetUserId,
145 'reason' => $params['reason'],
146 'watchuser' => $watchuser,
147 ];
148 if ( $watchlistExpiry !== null ) {
149 $res['watchlistexpiry'] = $this->getWatchlistExpiry(
150 $this->watchedItemStore,
151 $userPage,
152 $this->getUser()
153 );
154 }
155 $this->getResult()->addValue( null, $this->getModuleName(), $res );
156 }
157
158 public function mustBePosted() {
159 return true;
160 }
161
162 public function isWriteMode() {
163 return true;
164 }
165
166 public function getAllowedParams() {
167 $params = [
168 'id' => [
169 ParamValidator::PARAM_TYPE => 'integer',
170 ],
171 'user' => [
172 ParamValidator::PARAM_TYPE => 'user',
173 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'cidr', 'id' ],
174 ],
175 'userid' => [
176 ParamValidator::PARAM_TYPE => 'integer',
177 ParamValidator::PARAM_DEPRECATED => true,
178 ],
179 'reason' => '',
180 'tags' => [
181 ParamValidator::PARAM_TYPE => 'tags',
182 ParamValidator::PARAM_ISMULTI => true,
183 ],
184 'watchuser' => false,
185 ];
186
187 // Params appear in the docs in the order they are defined,
188 // which is why this is here and not at the bottom.
189 // @todo Find better way to support insertion at arbitrary position
190 if ( $this->watchlistExpiryEnabled ) {
191 $params += [
192 'watchlistexpiry' => [
193 ParamValidator::PARAM_TYPE => 'expiry',
194 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
195 ExpiryDef::PARAM_USE_MAX => true,
196 ]
197 ];
198 }
199
200 return $params;
201 }
202
203 public function needsToken() {
204 return 'csrf';
205 }
206
207 protected function getExamplesMessages() {
208 return [
209 'action=unblock&id=105'
210 => 'apihelp-unblock-example-id',
211 'action=unblock&user=Bob&reason=Sorry%20Bob'
212 => 'apihelp-unblock-example-user',
213 ];
214 }
215
216 public function getHelpUrls() {
217 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
218 }
219}
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.
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:65
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1540
requireOnlyOneParameter( $params,... $required)
Die if 0 or more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:960
getResult()
Get the result object.
Definition ApiBase.php:681
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:821
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:542
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition ApiBase.php:1595
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:67
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.
__construct(ApiMain $main, $action, BlockPermissionCheckerFactory $permissionCheckerFactory, UnblockUserFactory $unblockUserFactory, UserIdentityLookup $userIdentityLookup, WatchedItemStoreInterface $watchedItemStore, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup)
getExamplesMessages()
Returns usage examples for this module.
A class containing constants representing the names of configuration variables.
Type definition for user types.
Definition UserDef.php:27
Represents a title within MediaWiki.
Definition Title.php:79
Provides access to user options.
Service for formatting and validating API parameters.
Type definition for expiry timestamps.
Definition ExpiryDef.php:17
trait ApiBlockInfoTrait
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