Revert "selenium: add new message banner test to user spec"
[lhc/web/wiklou.git] / includes / api / ApiUnblock.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 7, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * API module that facilitates the unblocking of users. Requires API write mode
29 * to be enabled.
30 *
31 * @ingroup API
32 */
33 class ApiUnblock extends ApiBase {
34
35 /**
36 * Unblocks the specified user or provides the reason the unblock failed.
37 */
38 public function execute() {
39 $user = $this->getUser();
40 $params = $this->extractRequestParams();
41
42 $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
43
44 if ( !$user->isAllowed( 'block' ) ) {
45 $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
46 }
47 # T17810: blocked admins should have limited access here
48 if ( $user->isBlocked() ) {
49 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
50 if ( $status !== true ) {
51 $this->dieWithError(
52 $status,
53 null,
54 [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
55 );
56 }
57 }
58
59 // Check if user can add tags
60 if ( !is_null( $params['tags'] ) ) {
61 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
62 if ( !$ableToTag->isOK() ) {
63 $this->dieStatus( $ableToTag );
64 }
65 }
66
67 if ( $params['userid'] !== null ) {
68 $username = User::whoIs( $params['userid'] );
69
70 if ( $username === false ) {
71 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
72 } else {
73 $params['user'] = $username;
74 }
75 }
76
77 $data = [
78 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
79 'Reason' => $params['reason'],
80 'Tags' => $params['tags']
81 ];
82 $block = Block::newFromTarget( $data['Target'] );
83 $retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
84 if ( $retval !== true ) {
85 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
86 }
87
88 $res['id'] = $block->getId();
89 $target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
90 $res['user'] = $target instanceof User ? $target->getName() : $target;
91 $res['userid'] = $target instanceof User ? $target->getId() : 0;
92 $res['reason'] = $params['reason'];
93 $this->getResult()->addValue( null, $this->getModuleName(), $res );
94 }
95
96 public function mustBePosted() {
97 return true;
98 }
99
100 public function isWriteMode() {
101 return true;
102 }
103
104 public function getAllowedParams() {
105 return [
106 'id' => [
107 ApiBase::PARAM_TYPE => 'integer',
108 ],
109 'user' => null,
110 'userid' => [
111 ApiBase::PARAM_TYPE => 'integer'
112 ],
113 'reason' => '',
114 'tags' => [
115 ApiBase::PARAM_TYPE => 'tags',
116 ApiBase::PARAM_ISMULTI => true,
117 ],
118 ];
119 }
120
121 public function needsToken() {
122 return 'csrf';
123 }
124
125 protected function getExamplesMessages() {
126 return [
127 'action=unblock&id=105'
128 => 'apihelp-unblock-example-id',
129 'action=unblock&user=Bob&reason=Sorry%20Bob'
130 => 'apihelp-unblock-example-user',
131 ];
132 }
133
134 public function getHelpUrls() {
135 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
136 }
137 }