MediaWiki  master
ApiUnblock.php
Go to the documentation of this file.
1 <?php
28 
35 class ApiUnblock extends ApiBase {
36 
38 
41 
44 
47 
49  private $userCache;
50 
51  public function __construct(
52  ApiMain $main,
53  $action,
58  ) {
59  parent::__construct( $main, $action );
60 
61  $this->permissionCheckerFactory = $permissionCheckerFactory;
62  $this->unblockUserFactory = $unblockUserFactory;
63  $this->permissionManager = $permissionManager;
64  $this->userCache = $userCache;
65  }
66 
70  public function execute() {
71  $performer = $this->getUser();
72  $params = $this->extractRequestParams();
73 
74  $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
75 
76  if ( !$this->permissionManager->userHasRight( $performer, 'block' ) ) {
77  $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
78  }
79 
80  if ( $params['userid'] !== null ) {
81  $username = $this->userCache->getProp( $params['userid'], 'name' );
82 
83  if ( $username === false ) {
84  $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
85  } else {
86  $params['user'] = $username;
87  }
88  }
89 
90  $target = $params['id'] === null ? $params['user'] : "#{$params['id']}";
91 
92  # T17810: blocked admins should have limited access here
93  $status = $this->permissionCheckerFactory
94  ->newBlockPermissionChecker(
95  $target,
96  $performer
97  )->checkBlockPermissions();
98  if ( $status !== true ) {
99  $this->dieWithError(
100  $status,
101  null,
102  [ 'blockinfo' => $this->getBlockDetails( $performer->getBlock() ) ]
103  );
104  }
105 
106  $status = $this->unblockUserFactory->newUnblockUser(
107  $target,
108  $performer,
109  $params['reason'],
110  $params['tags'] ?? []
111  )->unblock();
112 
113  if ( !$status->isOK() ) {
114  $this->dieStatus( $status );
115  }
116 
117  $block = $status->getValue();
118  $target = $block->getType() == DatabaseBlock::TYPE_AUTO ? '' : $block->getTarget();
119  $res = [
120  'id' => $block->getId(),
121  'user' => $target instanceof User ? $target->getName() : $target,
122  'userid' => $target instanceof User ? $target->getId() : 0,
123  'reason' => $params['reason']
124  ];
125  $this->getResult()->addValue( null, $this->getModuleName(), $res );
126  }
127 
128  public function mustBePosted() {
129  return true;
130  }
131 
132  public function isWriteMode() {
133  return true;
134  }
135 
136  public function getAllowedParams() {
137  return [
138  'id' => [
139  ApiBase::PARAM_TYPE => 'integer',
140  ],
141  'user' => [
142  ApiBase::PARAM_TYPE => 'user',
143  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'cidr', 'id' ],
144  ],
145  'userid' => [
146  ApiBase::PARAM_TYPE => 'integer',
148  ],
149  'reason' => '',
150  'tags' => [
151  ApiBase::PARAM_TYPE => 'tags',
152  ApiBase::PARAM_ISMULTI => true,
153  ],
154  ];
155  }
156 
157  public function needsToken() {
158  return 'csrf';
159  }
160 
161  protected function getExamplesMessages() {
162  return [
163  'action=unblock&id=105'
164  => 'apihelp-unblock-example-id',
165  'action=unblock&user=Bob&reason=Sorry%20Bob'
166  => 'apihelp-unblock-example-user',
167  ];
168  }
169 
170  public function getHelpUrls() {
171  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
172  }
173 }
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:48
MediaWiki\Block\BlockPermissionCheckerFactory
Factory class for BlockPermissionChecker.
Definition: BlockPermissionCheckerFactory.php:34
User\getId
getId()
Get the user's ID.
Definition: User.php:2026
ApiUnblock\$permissionCheckerFactory
BlockPermissionCheckerFactory $permissionCheckerFactory
Definition: ApiUnblock.php:40
UserCache
Definition: UserCache.php:32
ApiUnblock\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUnblock.php:136
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1370
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:69
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:558
ApiUnblock\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUnblock.php:132
$res
$res
Definition: testCompression.php:57
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:134
ApiUnblock\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUnblock.php:161
ApiUnblock\__construct
__construct(ApiMain $main, $action, BlockPermissionCheckerFactory $permissionCheckerFactory, UnblockUserFactory $unblockUserFactory, PermissionManager $permissionManager, UserCache $userCache)
Definition: ApiUnblock.php:51
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:25
ApiBase\PARAM_DEPRECATED
const PARAM_DEPRECATED
Definition: ApiBase.php:74
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:50
ApiUnblock\execute
execute()
Unblocks the specified user or provides the reason the unblock failed.
Definition: ApiUnblock.php:70
ApiUnblock
API module that facilitates the unblocking of users.
Definition: ApiUnblock.php:35
ApiUnblock\$permissionManager
PermissionManager $permissionManager
Definition: ApiUnblock.php:46
ApiUnblock\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUnblock.php:170
MediaWiki\Block\UnblockUserFactory
Definition: UnblockUserFactory.php:29
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:706
ApiBlockInfoTrait
trait ApiBlockInfoTrait
Definition: ApiBlockInfoTrait.php:27
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:51
ApiBase\requireOnlyOneParameter
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:843
ApiUnblock\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiUnblock.php:128
ApiUnblock\$unblockUserFactory
UnblockUserFactory $unblockUserFactory
Definition: ApiUnblock.php:43
ApiUnblock\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUnblock.php:157
ApiBase\dieStatus
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1428
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:437
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:68
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
User\getName
getName()
Get the user name, or the IP of an anonymous user.
Definition: User.php:2055
ApiUnblock\$userCache
UserCache $userCache
Definition: ApiUnblock.php:49