| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Block; |
| 23 |
|
| 24 |
use ChangeTags; |
| 25 |
use ManualLogEntry; |
| 26 |
use MediaWiki\HookContainer\HookContainer; |
| 27 |
use MediaWiki\HookContainer\HookRunner; |
| 28 |
use MediaWiki\Permissions\Authority; |
| 29 |
use MediaWiki\Status\Status; |
| 30 |
use MediaWiki\Title\TitleValue; |
| 31 |
use MediaWiki\User\UserFactory; |
| 32 |
use MediaWiki\User\UserIdentity; |
| 33 |
use RevisionDeleteUser; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class UnblockUser { |
| 41 |
private BlockPermissionChecker $blockPermissionChecker; |
| 42 |
private DatabaseBlockStore $blockStore; |
| 43 |
private BlockUtils $blockUtils; |
| 44 |
private UserFactory $userFactory; |
| 45 |
private HookRunner $hookRunner; |
| 46 |
|
| 47 |
|
| 48 |
private $target; |
| 49 |
|
| 50 |
|
| 51 |
private $targetType; |
| 52 |
|
| 53 |
|
| 54 |
private $block; |
| 55 |
|
| 56 |
|
| 57 |
private $performer; |
| 58 |
|
| 59 |
|
| 60 |
private $reason; |
| 61 |
|
| 62 |
|
| 63 |
private $tags = []; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function __construct( |
| 77 |
BlockPermissionCheckerFactory $blockPermissionCheckerFactory, |
| 78 |
DatabaseBlockStore $blockStore, |
| 79 |
BlockUtils $blockUtils, |
| 80 |
UserFactory $userFactory, |
| 81 |
HookContainer $hookContainer, |
| 82 |
$target, |
| 83 |
Authority $performer, |
| 84 |
string $reason, |
| 85 |
array $tags = [] |
| 86 |
) { |
| 87 |
|
| 88 |
$this->blockPermissionChecker = $blockPermissionCheckerFactory |
| 89 |
->newBlockPermissionChecker( |
| 90 |
$target, |
| 91 |
$performer |
| 92 |
); |
| 93 |
$this->blockStore = $blockStore; |
| 94 |
$this->blockUtils = $blockUtils; |
| 95 |
$this->userFactory = $userFactory; |
| 96 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 97 |
|
| 98 |
|
| 99 |
[ $this->target, $this->targetType ] = $this->blockUtils->parseBlockTarget( $target ); |
| 100 |
if ( |
| 101 |
$this->targetType === AbstractBlock::TYPE_AUTO && |
| 102 |
is_numeric( $this->target ) |
| 103 |
) { |
| 104 |
|
| 105 |
$this->target = '#' . $this->target; |
| 106 |
} |
| 107 |
$this->block = $this->blockStore->newFromTarget( $this->target ); |
| 108 |
$this->performer = $performer; |
| 109 |
$this->reason = $reason; |
| 110 |
$this->tags = $tags; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function unblock(): Status { |
| 119 |
$status = Status::newGood(); |
| 120 |
|
| 121 |
$basePermissionCheckResult = $this->blockPermissionChecker->checkBasePermissions( |
| 122 |
$this->block instanceof DatabaseBlock && $this->block->getHideName() |
| 123 |
); |
| 124 |
if ( $basePermissionCheckResult !== true ) { |
| 125 |
return $status->fatal( $basePermissionCheckResult ); |
| 126 |
} |
| 127 |
|
| 128 |
$blockPermissionCheckResult = $this->blockPermissionChecker->checkBlockPermissions(); |
| 129 |
if ( $blockPermissionCheckResult !== true ) { |
| 130 |
return $status->fatal( $blockPermissionCheckResult ); |
| 131 |
} |
| 132 |
|
| 133 |
if ( count( $this->tags ) !== 0 ) { |
| 134 |
$status->merge( |
| 135 |
ChangeTags::canAddTagsAccompanyingChange( |
| 136 |
$this->tags, |
| 137 |
$this->performer |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
if ( !$status->isOK() ) { |
| 143 |
return $status; |
| 144 |
} |
| 145 |
return $this->unblockUnsafe(); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function unblockUnsafe(): Status { |
| 155 |
$status = Status::newGood(); |
| 156 |
|
| 157 |
if ( $this->block === null ) { |
| 158 |
return $status->fatal( 'ipb_cant_unblock', $this->target ); |
| 159 |
} |
| 160 |
|
| 161 |
if ( |
| 162 |
$this->block->getType() === AbstractBlock::TYPE_RANGE && |
| 163 |
$this->targetType === AbstractBlock::TYPE_IP |
| 164 |
) { |
| 165 |
return $status->fatal( 'ipb_blocked_as_range', $this->target, $this->block->getTargetName() ); |
| 166 |
} |
| 167 |
|
| 168 |
$denyReason = [ 'hookaborted' ]; |
| 169 |
$legacyUser = $this->userFactory->newFromAuthority( $this->performer ); |
| 170 |
if ( !$this->hookRunner->onUnblockUser( $this->block, $legacyUser, $denyReason ) ) { |
| 171 |
foreach ( $denyReason as $key ) { |
| 172 |
$status->fatal( $key ); |
| 173 |
} |
| 174 |
return $status; |
| 175 |
} |
| 176 |
|
| 177 |
$deleteBlockStatus = $this->blockStore->deleteBlock( $this->block ); |
| 178 |
if ( !$deleteBlockStatus ) { |
| 179 |
return $status->fatal( 'ipb_cant_unblock', $this->block->getTargetName() ); |
| 180 |
} |
| 181 |
|
| 182 |
$this->hookRunner->onUnblockUserComplete( $this->block, $legacyUser ); |
| 183 |
|
| 184 |
|
| 185 |
$user = $this->block->getTargetUserIdentity(); |
| 186 |
if ( $this->block->getHideName() && $user ) { |
| 187 |
$id = $user->getId(); |
| 188 |
RevisionDeleteUser::unsuppressUserName( $user->getName(), $id ); |
| 189 |
} |
| 190 |
|
| 191 |
$this->log(); |
| 192 |
|
| 193 |
$status->setResult( $status->isOK(), $this->block ); |
| 194 |
return $status; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
private function log() { |
| 201 |
|
| 202 |
if ( $this->block->getType() === DatabaseBlock::TYPE_AUTO ) { |
| 203 |
$page = TitleValue::tryNew( NS_USER, '#' . $this->block->getId() ); |
| 204 |
} else { |
| 205 |
$page = TitleValue::tryNew( NS_USER, $this->block->getTargetName() ); |
| 206 |
} |
| 207 |
|
| 208 |
$logEntry = new ManualLogEntry( 'block', 'unblock' ); |
| 209 |
|
| 210 |
if ( $page !== null ) { |
| 211 |
$logEntry->setTarget( $page ); |
| 212 |
} |
| 213 |
$logEntry->setComment( $this->reason ); |
| 214 |
$logEntry->setPerformer( $this->performer->getUser() ); |
| 215 |
$logEntry->addTags( $this->tags ); |
| 216 |
$logEntry->setRelations( [ 'ipb_id' => $this->block->getId() ] ); |
| 217 |
$logId = $logEntry->insert(); |
| 218 |
$logEntry->publish( $logId ); |
| 219 |
} |
| 220 |
|
| 221 |
} |