Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiUnblock.php
blob35f0b4b22ece145f0df39a0db6288f7599a46c3b
1 <?php
2 /**
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
20 * @file
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;
38 /**
39 * API module that facilitates the unblocking of users. Requires API write mode
40 * to be enabled.
42 * @ingroup API
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(
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 );
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;
79 /**
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'] );
94 if ( !$identity ) {
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(
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() ) ]
117 $status = $this->unblockUserFactory->newUnblockUser(
118 $target,
119 $this->getAuthority(),
120 $params['reason'],
121 $params['tags'] ?? []
122 )->unblock();
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 );
138 } else {
139 $watchuser = false;
140 $watchlistExpiry = null;
143 $res = [
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,
153 $userPage,
154 $this->getUser()
157 $this->getResult()->addValue( null, $this->getModuleName(), $res );
160 public function mustBePosted() {
161 return true;
164 public function isWriteMode() {
165 return true;
168 public function getAllowedParams() {
169 $params = [
170 'id' => [
171 ParamValidator::PARAM_TYPE => 'integer',
173 'user' => [
174 ParamValidator::PARAM_TYPE => 'user',
175 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'cidr', 'id' ],
177 'userid' => [
178 ParamValidator::PARAM_TYPE => 'integer',
179 ParamValidator::PARAM_DEPRECATED => true,
181 'reason' => '',
182 'tags' => [
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 ) {
193 $params += [
194 'watchlistexpiry' => [
195 ParamValidator::PARAM_TYPE => 'expiry',
196 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
197 ExpiryDef::PARAM_USE_MAX => true,
202 return $params;
205 public function needsToken() {
206 return 'csrf';
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',
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' );