Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.95% |
34 / 42 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| BlockPermissionChecker | |
80.95% |
34 / 42 |
|
60.00% |
3 / 5 |
26.66 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| setTarget | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| checkBasePermissions | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| checkBlockPermissions | |
75.86% |
22 / 29 |
|
0.00% |
0 / 1 |
18.16 | |||
| checkEmailPermissions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @license GPL-2.0-or-later |
| 5 | * @file |
| 6 | */ |
| 7 | |
| 8 | namespace MediaWiki\Block; |
| 9 | |
| 10 | use InvalidArgumentException; |
| 11 | use MediaWiki\Config\ServiceOptions; |
| 12 | use MediaWiki\MainConfigNames; |
| 13 | use MediaWiki\Permissions\Authority; |
| 14 | use MediaWiki\User\UserIdentity; |
| 15 | use Wikimedia\Rdbms\IDBAccessObject; |
| 16 | |
| 17 | /** |
| 18 | * Block permissions |
| 19 | * |
| 20 | * This class is responsible for making sure a user has permission to block. |
| 21 | * |
| 22 | * This class is usable for both blocking and unblocking. |
| 23 | * |
| 24 | * @since 1.35 |
| 25 | */ |
| 26 | class BlockPermissionChecker { |
| 27 | /** |
| 28 | * Legacy target state |
| 29 | * @var BlockTarget|null Block target or null when unknown |
| 30 | */ |
| 31 | private $target; |
| 32 | |
| 33 | /** |
| 34 | * @var BlockTargetFactory |
| 35 | */ |
| 36 | private $blockTargetFactory; |
| 37 | |
| 38 | /** |
| 39 | * @var Authority Block performer |
| 40 | */ |
| 41 | private $performer; |
| 42 | |
| 43 | /** |
| 44 | * @internal only for use by ServiceWiring and BlockPermissionCheckerFactory |
| 45 | */ |
| 46 | public const CONSTRUCTOR_OPTIONS = [ |
| 47 | MainConfigNames::EnableUserEmail, |
| 48 | ]; |
| 49 | |
| 50 | private ServiceOptions $options; |
| 51 | |
| 52 | /** |
| 53 | * @param ServiceOptions $options |
| 54 | * @param BlockTargetFactory $blockTargetFactory For legacy branches only |
| 55 | * @param Authority $performer |
| 56 | */ |
| 57 | public function __construct( |
| 58 | ServiceOptions $options, |
| 59 | BlockTargetFactory $blockTargetFactory, |
| 60 | Authority $performer |
| 61 | ) { |
| 62 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 63 | $this->options = $options; |
| 64 | $this->blockTargetFactory = $blockTargetFactory; |
| 65 | $this->performer = $performer; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @internal To support deprecated method BlockPermissionCheckerFactory::newBlockPermissionChecker() |
| 70 | * @param UserIdentity|string $target |
| 71 | * @return void |
| 72 | */ |
| 73 | public function setTarget( $target ) { |
| 74 | $this->target = $this->blockTargetFactory->newFromLegacyUnion( $target ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Check the base permission that applies to either block or unblock |
| 79 | * |
| 80 | * @since 1.36 |
| 81 | * @param bool $checkHideuser |
| 82 | * @return bool|string |
| 83 | */ |
| 84 | public function checkBasePermissions( $checkHideuser = false ) { |
| 85 | if ( !$this->performer->isAllowed( 'block' ) ) { |
| 86 | return 'badaccess-group0'; |
| 87 | } |
| 88 | |
| 89 | if ( |
| 90 | $checkHideuser && |
| 91 | !$this->performer->isAllowed( 'hideuser' ) |
| 92 | ) { |
| 93 | return 'unblock-hideuser'; |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Checks block-related permissions (doesn't check any other permissions) |
| 101 | * |
| 102 | * T17810: Site-wide blocked admins should not be able to block/unblock |
| 103 | * others with one exception; they can block the user who blocked them, |
| 104 | * to reduce advantage of a malicious account blocking all admins (T150826). |
| 105 | * |
| 106 | * T208965: Partially blocked admins can block and unblock others as normal. |
| 107 | * |
| 108 | * @param BlockTarget|UserIdentity|string|null $target The target of the |
| 109 | * proposed block or unblock operation. Passing null for this parameter |
| 110 | * is deprecated. This parameter will soon be required. Passing a |
| 111 | * UserIdentity or string for this parameter is deprecated. Pass a |
| 112 | * BlockTarget in new code. |
| 113 | * @param int $freshness Indicates whether slightly stale data is acceptable |
| 114 | * in exchange for a fast response. |
| 115 | * @return bool|string True when checks passed, message code for failures |
| 116 | */ |
| 117 | public function checkBlockPermissions( |
| 118 | $target = null, |
| 119 | $freshness = IDBAccessObject::READ_NORMAL |
| 120 | ) { |
| 121 | if ( $target === null ) { |
| 122 | if ( $this->target ) { |
| 123 | wfDeprecatedMsg( |
| 124 | 'Passing null to checkBlockPermissions() for $target is deprecated since 1.44', |
| 125 | '1.44' ); |
| 126 | $target = $this->target; |
| 127 | } else { |
| 128 | throw new InvalidArgumentException( 'A target is required' ); |
| 129 | } |
| 130 | } elseif ( !( $target instanceof BlockTarget ) ) { |
| 131 | $target = $this->blockTargetFactory->newFromLegacyUnion( $target ); |
| 132 | if ( !$target ) { |
| 133 | throw new InvalidArgumentException( 'Invalid block target' ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | $block = $this->performer->getBlock( $freshness ); |
| 138 | if ( !$block ) { |
| 139 | // User is not blocked, process as normal |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | if ( !$block->isSitewide() ) { |
| 144 | // T208965: Partially blocked admins should have full access |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | $performerIdentity = $this->performer->getUser(); |
| 149 | |
| 150 | if ( |
| 151 | $target instanceof UserBlockTarget && |
| 152 | $target->getUserIdentity()->getId() === $performerIdentity->getId() |
| 153 | ) { |
| 154 | // Blocked admin is trying to alter their own block |
| 155 | |
| 156 | // Self-blocked admins can always remove or alter their block |
| 157 | if ( $block->getBlocker() && $performerIdentity->equals( $block->getBlocker() ) ) { |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | // Users with 'unblockself' right can unblock themselves or alter their own block |
| 162 | if ( $this->performer->isAllowed( 'unblockself' ) ) { |
| 163 | return true; |
| 164 | } else { |
| 165 | return 'ipbnounblockself'; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if ( |
| 170 | $target instanceof UserBlockTarget && |
| 171 | $block->getBlocker() && |
| 172 | $target->getUserIdentity()->equals( $block->getBlocker() ) |
| 173 | ) { |
| 174 | // T150826: Blocked admins can always block the admin who blocked them |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | // User is blocked and no exception took effect |
| 179 | return 'ipbblocked'; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Check permission to block emailing |
| 184 | * |
| 185 | * @since 1.36 |
| 186 | * @return bool |
| 187 | */ |
| 188 | public function checkEmailPermissions() { |
| 189 | return $this->options->get( MainConfigNames::EnableUserEmail ) && |
| 190 | $this->performer->isAllowed( 'blockemail' ); |
| 191 | } |
| 192 | } |