MediaWiki  master
ApiUnblock.php
Go to the documentation of this file.
1 <?php
29 
36 class ApiUnblock extends ApiBase {
37 
39 
41  private $permissionCheckerFactory;
42 
44  private $unblockUserFactory;
45 
47  private $userIdentityLookup;
48 
49  public function __construct(
50  ApiMain $main,
51  $action,
52  BlockPermissionCheckerFactory $permissionCheckerFactory,
53  UnblockUserFactory $unblockUserFactory,
54  UserIdentityLookup $userIdentityLookup
55  ) {
56  parent::__construct( $main, $action );
57 
58  $this->permissionCheckerFactory = $permissionCheckerFactory;
59  $this->unblockUserFactory = $unblockUserFactory;
60  $this->userIdentityLookup = $userIdentityLookup;
61  }
62 
66  public function execute() {
67  $performer = $this->getUser();
68  $params = $this->extractRequestParams();
69 
70  $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
71 
72  if ( !$this->getAuthority()->isAllowed( 'block' ) ) {
73  $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
74  }
75 
76  if ( $params['userid'] !== null ) {
77  $identity = $this->userIdentityLookup->getUserIdentityByUserId( $params['userid'] );
78  if ( !$identity ) {
79  $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
80  }
81  $params['user'] = $identity->getName();
82  }
83 
84  $target = $params['id'] === null ? $params['user'] : "#{$params['id']}";
85 
86  # T17810: blocked admins should have limited access here
87  $status = $this->permissionCheckerFactory
88  ->newBlockPermissionChecker(
89  $target,
90  $this->getAuthority()
91  )->checkBlockPermissions();
92  if ( $status !== true ) {
93  $this->dieWithError(
94  $status,
95  null,
96  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable Block is checked and not null
97  [ 'blockinfo' => $this->getBlockDetails( $performer->getBlock() ) ]
98  );
99  }
100 
101  $status = $this->unblockUserFactory->newUnblockUser(
102  $target,
103  $this->getAuthority(),
104  $params['reason'],
105  $params['tags'] ?? []
106  )->unblock();
107 
108  if ( !$status->isOK() ) {
109  $this->dieStatus( $status );
110  }
111 
112  $block = $status->getValue();
113  $targetName = $block->getType() === DatabaseBlock::TYPE_AUTO ? '' : $block->getTargetName();
114  $targetUserId = $block->getTargetUserIdentity() ? $block->getTargetUserIdentity()->getId() : 0;
115  $res = [
116  'id' => $block->getId(),
117  'user' => $targetName,
118  'userid' => $targetUserId,
119  'reason' => $params['reason']
120  ];
121  $this->getResult()->addValue( null, $this->getModuleName(), $res );
122  }
123 
124  public function mustBePosted() {
125  return true;
126  }
127 
128  public function isWriteMode() {
129  return true;
130  }
131 
132  public function getAllowedParams() {
133  return [
134  'id' => [
135  ParamValidator::PARAM_TYPE => 'integer',
136  ],
137  'user' => [
138  ParamValidator::PARAM_TYPE => 'user',
139  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'cidr', 'id' ],
140  ],
141  'userid' => [
142  ParamValidator::PARAM_TYPE => 'integer',
143  ParamValidator::PARAM_DEPRECATED => true,
144  ],
145  'reason' => '',
146  'tags' => [
147  ParamValidator::PARAM_TYPE => 'tags',
148  ParamValidator::PARAM_ISMULTI => true,
149  ],
150  ];
151  }
152 
153  public function needsToken() {
154  return 'csrf';
155  }
156 
157  protected function getExamplesMessages() {
158  return [
159  'action=unblock&id=105'
160  => 'apihelp-unblock-example-id',
161  'action=unblock&user=Bob&reason=Sorry%20Bob'
162  => 'apihelp-unblock-example-user',
163  ];
164  }
165 
166  public function getHelpUrls() {
167  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
168  }
169 }
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:56
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1458
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:903
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:498
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
Definition: ApiBase.php:1521
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:52
API module that facilitates the unblocking of users.
Definition: ApiUnblock.php:36
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiUnblock.php:128
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiUnblock.php:153
execute()
Unblocks the specified user or provides the reason the unblock failed.
Definition: ApiUnblock.php:66
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiUnblock.php:166
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiUnblock.php:132
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiUnblock.php:124
__construct(ApiMain $main, $action, BlockPermissionCheckerFactory $permissionCheckerFactory, UnblockUserFactory $unblockUserFactory, UserIdentityLookup $userIdentityLookup)
Definition: ApiUnblock.php:49
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiUnblock.php:157
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Service for formatting and validating API parameters.
trait ApiBlockInfoTrait