3 * Copyright © 2007 Roan Kattouw <roan.kattouw@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Api
;
25 use MediaWiki\Block\Block
;
26 use MediaWiki\Block\BlockPermissionCheckerFactory
;
27 use MediaWiki\Block\UnblockUserFactory
;
28 use MediaWiki\MainConfigNames
;
29 use MediaWiki\ParamValidator\TypeDef\UserDef
;
30 use MediaWiki\Title\Title
;
31 use MediaWiki\User\Options\UserOptionsLookup
;
32 use MediaWiki\User\UserIdentityLookup
;
33 use MediaWiki\Watchlist\WatchedItemStoreInterface
;
34 use MediaWiki\Watchlist\WatchlistManager
;
35 use Wikimedia\ParamValidator\ParamValidator
;
36 use Wikimedia\ParamValidator\TypeDef\ExpiryDef
;
39 * API module that facilitates the unblocking of users. Requires API write mode
44 class ApiUnblock
extends ApiBase
{
46 use ApiBlockInfoTrait
;
47 use ApiWatchlistTrait
;
49 private BlockPermissionCheckerFactory
$permissionCheckerFactory;
50 private UnblockUserFactory
$unblockUserFactory;
51 private UserIdentityLookup
$userIdentityLookup;
52 private WatchedItemStoreInterface
$watchedItemStore;
54 public function __construct(
57 BlockPermissionCheckerFactory
$permissionCheckerFactory,
58 UnblockUserFactory
$unblockUserFactory,
59 UserIdentityLookup
$userIdentityLookup,
60 WatchedItemStoreInterface
$watchedItemStore,
61 WatchlistManager
$watchlistManager,
62 UserOptionsLookup
$userOptionsLookup
64 parent
::__construct( $main, $action );
66 $this->permissionCheckerFactory
= $permissionCheckerFactory;
67 $this->unblockUserFactory
= $unblockUserFactory;
68 $this->userIdentityLookup
= $userIdentityLookup;
69 $this->watchedItemStore
= $watchedItemStore;
71 // Variables needed in ApiWatchlistTrait trait
72 $this->watchlistExpiryEnabled
= $this->getConfig()->get( MainConfigNames
::WatchlistExpiry
);
73 $this->watchlistMaxDuration
=
74 $this->getConfig()->get( MainConfigNames
::WatchlistExpiryMaxDuration
);
75 $this->watchlistManager
= $watchlistManager;
76 $this->userOptionsLookup
= $userOptionsLookup;
80 * Unblocks the specified user or provides the reason the unblock failed.
82 public function execute() {
83 $performer = $this->getUser();
84 $params = $this->extractRequestParams();
86 $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
88 if ( !$this->getAuthority()->isAllowed( 'block' ) ) {
89 $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
92 if ( $params['userid'] !== null ) {
93 $identity = $this->userIdentityLookup
->getUserIdentityByUserId( $params['userid'] );
95 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
97 $params['user'] = $identity->getName();
100 $target = $params['id'] === null ?
$params['user'] : "#{$params['id']}";
102 # T17810: blocked admins should have limited access here
103 $status = $this->permissionCheckerFactory
104 ->newBlockPermissionChecker(
106 $this->getAuthority()
107 )->checkBlockPermissions();
108 if ( $status !== true ) {
112 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
113 [ 'blockinfo' => $this->getBlockDetails( $performer->getBlock() ) ]
117 $status = $this->unblockUserFactory
->newUnblockUser(
119 $this->getAuthority(),
121 $params['tags'] ??
[]
124 if ( !$status->isOK() ) {
125 $this->dieStatus( $status );
128 $block = $status->getValue();
129 $targetType = $block->getType();
130 $targetName = $targetType === Block
::TYPE_AUTO ?
'' : $block->getTargetName();
131 $targetUserId = $block->getTargetUserIdentity() ?
$block->getTargetUserIdentity()->getId() : 0;
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 );
140 $watchlistExpiry = null;
144 'id' => $block->getId(),
145 'user' => $targetName,
146 'userid' => $targetUserId,
147 'reason' => $params['reason'],
148 'watchuser' => $watchuser,
150 if ( $watchlistExpiry !== null ) {
151 $res['watchlistexpiry'] = $this->getWatchlistExpiry(
152 $this->watchedItemStore
,
157 $this->getResult()->addValue( null, $this->getModuleName(), $res );
160 public function mustBePosted() {
164 public function isWriteMode() {
168 public function getAllowedParams() {
171 ParamValidator
::PARAM_TYPE
=> 'integer',
174 ParamValidator
::PARAM_TYPE
=> 'user',
175 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'ip', 'temp', 'cidr', 'id' ],
178 ParamValidator
::PARAM_TYPE
=> 'integer',
179 ParamValidator
::PARAM_DEPRECATED
=> true,
183 ParamValidator
::PARAM_TYPE
=> 'tags',
184 ParamValidator
::PARAM_ISMULTI
=> true,
186 'watchuser' => false,
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
) {
194 'watchlistexpiry' => [
195 ParamValidator
::PARAM_TYPE
=> 'expiry',
196 ExpiryDef
::PARAM_MAX
=> $this->watchlistMaxDuration
,
197 ExpiryDef
::PARAM_USE_MAX
=> true,
205 public function needsToken() {
209 protected function getExamplesMessages() {
211 'action=unblock&id=105'
212 => 'apihelp-unblock-example-id',
213 'action=unblock&user=Bob&reason=Sorry%20Bob'
214 => 'apihelp-unblock-example-user',
218 public function getHelpUrls() {
219 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
223 /** @deprecated class alias since 1.43 */
224 class_alias( ApiUnblock
::class, 'ApiUnblock' );