MediaWiki  master
UnblockUser.php
Go to the documentation of this file.
1 <?php
2 
22 namespace MediaWiki\Block;
23 
24 use ChangeTags;
25 use ManualLogEntry;
32 use Status;
33 use TitleValue;
34 
40 class UnblockUser {
43 
45  private $blockStore;
46 
48  private $blockUtils;
49 
51  private $userFactory;
52 
54  private $hookRunner;
55 
57  private $target;
58 
60  private $targetType;
61 
63  private $block;
64 
66  private $performer;
67 
69  private $reason;
70 
72  private $tags = [];
73 
85  public function __construct(
86  BlockPermissionCheckerFactory $blockPermissionCheckerFactory,
90  HookContainer $hookContainer,
91  $target,
93  string $reason,
94  array $tags = []
95  ) {
96  // Process dependencies
97  $this->blockPermissionChecker = $blockPermissionCheckerFactory
99  $target,
100  $performer
101  );
102  $this->blockStore = $blockStore;
103  $this->blockUtils = $blockUtils;
104  $this->userFactory = $userFactory;
105  $this->hookRunner = new HookRunner( $hookContainer );
106 
107  // Process params
108  list( $this->target, $this->targetType ) = $this->blockUtils->parseBlockTarget( $target );
109  if (
110  $this->targetType === AbstractBlock::TYPE_AUTO &&
111  is_numeric( $this->target )
112  ) {
113  // Needed, because BlockUtils::parseBlockTarget will strip the # from autoblocks.
114  $this->target = '#' . $this->target;
115  }
116  $this->block = DatabaseBlock::newFromTarget( $this->target );
117  $this->performer = $performer;
118  $this->reason = $reason;
119  $this->tags = $tags;
120  }
121 
127  public function unblock(): Status {
128  $status = Status::newGood();
129 
130  $basePermissionCheckResult = $this->blockPermissionChecker->checkBasePermissions(
131  $this->block instanceof DatabaseBlock && $this->block->getHideName()
132  );
133  if ( $basePermissionCheckResult !== true ) {
134  return $status->fatal( $basePermissionCheckResult );
135  }
136 
137  $blockPermissionCheckResult = $this->blockPermissionChecker->checkBlockPermissions();
138  if ( $blockPermissionCheckResult !== true ) {
139  return $status->fatal( $blockPermissionCheckResult );
140  }
141 
142  if ( count( $this->tags ) !== 0 ) {
143  $status->merge(
145  $this->tags,
146  $this->performer
147  )
148  );
149  }
150 
151  if ( !$status->isOK() ) {
152  return $status;
153  }
154  return $this->unblockUnsafe();
155  }
156 
163  public function unblockUnsafe(): Status {
164  $status = Status::newGood();
165 
166  if ( $this->block === null ) {
167  return $status->fatal( 'ipb_cant_unblock', $this->target );
168  }
169 
170  if (
171  $this->block->getType() === AbstractBlock::TYPE_RANGE &&
172  $this->targetType === AbstractBlock::TYPE_IP
173  ) {
174  return $status->fatal( 'ipb_blocked_as_range', $this->target, $this->block->getTargetName() );
175  }
176 
177  $denyReason = [ 'hookaborted' ];
178  $legacyUser = $this->userFactory->newFromAuthority( $this->performer );
179  if ( !$this->hookRunner->onUnblockUser( $this->block, $legacyUser, $denyReason ) ) {
180  foreach ( $denyReason as $key ) {
181  $status->fatal( $key );
182  }
183  return $status;
184  }
185 
186  $deleteBlockStatus = $this->blockStore->deleteBlock( $this->block );
187  if ( !$deleteBlockStatus ) {
188  return $status->fatal( 'ipb_cant_unblock', $this->block->getTargetName() );
189  }
190 
191  $this->hookRunner->onUnblockUserComplete( $this->block, $legacyUser );
192 
193  // Unset _deleted fields as needed
194  $user = $this->block->getTargetUserIdentity();
195  if ( $this->block->getHideName() && $user ) {
196  $id = $user->getId();
197  RevisionDeleteUser::unsuppressUserName( $user->getName(), $id );
198  }
199 
200  $this->log();
201 
202  $status->setResult( $status->isOK(), $this->block );
203  return $status;
204  }
205 
209  private function log() {
210  // Redact IP for autoblocks
211  if ( $this->block->getType() === DatabaseBlock::TYPE_AUTO ) {
212  $page = TitleValue::tryNew( NS_USER, '#' . $this->block->getId() );
213  } else {
214  $page = TitleValue::tryNew( NS_USER, $this->block->getTargetName() );
215  }
216 
217  $logEntry = new ManualLogEntry( 'block', 'unblock' );
218 
219  if ( $page !== null ) {
220  $logEntry->setTarget( $page );
221  }
222  $logEntry->setComment( $this->reason );
223  $logEntry->setPerformer( $this->performer->getUser() );
224  $logEntry->addTags( $this->tags );
225  $logEntry->setRelations( [ 'ipb_id' => $this->block->getId() ] );
226  $logId = $logEntry->insert();
227  $logEntry->publish( $logId );
228  }
229 
230 }
MediaWiki\Block\BlockPermissionCheckerFactory
Factory class for BlockPermissionChecker.
Definition: BlockPermissionCheckerFactory.php:33
MediaWiki\Block\UnblockUser\$userFactory
UserFactory $userFactory
Definition: UnblockUser.php:51
MediaWiki\Block
Definition: AbstractBlock.php:21
MediaWiki\Block\UnblockUser\$target
UserIdentity string $target
Definition: UnblockUser.php:57
MediaWiki\Block\BlockUtils
Backend class for blocking utils.
Definition: BlockUtils.php:45
MediaWiki\Block\UnblockUser\__construct
__construct(BlockPermissionCheckerFactory $blockPermissionCheckerFactory, DatabaseBlockStore $blockStore, BlockUtils $blockUtils, UserFactory $userFactory, HookContainer $hookContainer, $target, Authority $performer, string $reason, array $tags=[])
Definition: UnblockUser.php:85
RevisionDeleteUser\unsuppressUserName
static unsuppressUserName( $name, $userId, IDatabase $dbw=null)
Definition: RevisionDeleteUser.php:183
MediaWiki\Block\DatabaseBlockStore
Definition: DatabaseBlockStore.php:47
MediaWiki\Block\UnblockUser\$targetType
int $targetType
Definition: UnblockUser.php:60
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\Block\UnblockUser\$block
DatabaseBlock null $block
Definition: UnblockUser.php:63
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
MediaWiki\Block\UnblockUser\$blockStore
DatabaseBlockStore $blockStore
Definition: UnblockUser.php:45
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:50
MediaWiki\Block\UnblockUser\log
log()
Log the unblock to Special:Log/block.
Definition: UnblockUser.php:209
TitleValue\tryNew
static tryNew( $namespace, $title, $fragment='', $interwiki='')
Constructs a TitleValue, or returns null if the parameters are not valid.
Definition: TitleValue.php:82
ChangeTags
Definition: ChangeTags.php:32
MediaWiki\Block\UnblockUser\$performer
Authority $performer
Definition: UnblockUser.php:66
MediaWiki\Block\UnblockUser
Backend class for unblocking users.
Definition: UnblockUser.php:40
MediaWiki\Block\Block\TYPE_RANGE
const TYPE_RANGE
Definition: Block.php:43
MediaWiki\Block\BlockPermissionCheckerFactory\newBlockPermissionChecker
newBlockPermissionChecker( $target, Authority $performer)
Definition: BlockPermissionCheckerFactory.php:65
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
MediaWiki\Block\BlockPermissionChecker
Block permissions.
Definition: BlockPermissionChecker.php:38
MediaWiki\Block\DatabaseBlock\newFromTarget
static newFromTarget( $specificTarget, $vagueTarget=null, $fromPrimary=false)
Given a target and the target's type, get an existing block object if possible.
Definition: DatabaseBlock.php:851
ChangeTags\canAddTagsAccompanyingChange
static canAddTagsAccompanyingChange(array $tags, Authority $performer=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Definition: ChangeTags.php:625
MediaWiki\Block\UnblockUser\unblock
unblock()
Unblock user.
Definition: UnblockUser.php:127
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
MediaWiki\Block\Block\TYPE_AUTO
const TYPE_AUTO
Definition: Block.php:44
NS_USER
const NS_USER
Definition: Defines.php:66
RevisionDeleteUser
Backend functions for suppressing and unsuppressing all references to a given user,...
Definition: RevisionDeleteUser.php:35
MediaWiki\Block\UnblockUser\unblockUnsafe
unblockUnsafe()
Unblock user without any sort of permission checks.
Definition: UnblockUser.php:163
MediaWiki\Block\UnblockUser\$tags
array $tags
Definition: UnblockUser.php:72
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:44
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
MediaWiki\Block\AbstractBlock\getHideName
getHideName()
Get whether the block hides the target's username.
Definition: AbstractBlock.php:174
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:554
MediaWiki\Block\UnblockUser\$reason
string $reason
Definition: UnblockUser.php:69
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:41
MediaWiki\Block\UnblockUser\$hookRunner
HookRunner $hookRunner
Definition: UnblockUser.php:54
MediaWiki\Block\UnblockUser\$blockPermissionChecker
BlockPermissionChecker $blockPermissionChecker
Definition: UnblockUser.php:42
MediaWiki\Block\Block\TYPE_IP
const TYPE_IP
Definition: Block.php:42
MediaWiki\Block\UnblockUser\$blockUtils
BlockUtils $blockUtils
Definition: UnblockUser.php:48
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40