1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
<?php
namespace MediaWiki\Extension\AbuseFilter;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\AbuseFilter\Filter\AbstractFilter;
use MediaWiki\Extension\AbuseFilter\Filter\Flags;
use MediaWiki\Permissions\Authority;
use MediaWiki\User\Options\UserOptionsLookup;
/**
* This class simplifies the interactions between the AbuseFilter code and Authority, knowing
* what rights are required to perform AF-related actions.
*/
class AbuseFilterPermissionManager {
public const SERVICE_NAME = 'AbuseFilterPermissionManager';
public const CONSTRUCTOR_OPTIONS = [
'AbuseFilterProtectedVariables',
];
/**
* @var string[] Protected variables defined in config via AbuseFilterProtectedVariables
*/
private $protectedVariables;
private UserOptionsLookup $userOptionsLookup;
/**
* @param ServiceOptions $options
* @param UserOptionsLookup $userOptionsLookup
*/
public function __construct(
ServiceOptions $options,
UserOptionsLookup $userOptionsLookup
) {
$this->protectedVariables = $options->get( 'AbuseFilterProtectedVariables' );
$this->userOptionsLookup = $userOptionsLookup;
}
/**
* @param Authority $performer
* @return bool
*/
public function canEdit( Authority $performer ): bool {
$block = $performer->getBlock();
return (
!( $block && $block->isSitewide() ) &&
$performer->isAllowed( 'abusefilter-modify' )
);
}
/**
* @param Authority $performer
* @return bool
*/
public function canEditGlobal( Authority $performer ): bool {
return $performer->isAllowed( 'abusefilter-modify-global' );
}
/**
* Whether the user can edit the given filter.
*
* @param Authority $performer
* @param AbstractFilter $filter
* @return bool
*/
public function canEditFilter( Authority $performer, AbstractFilter $filter ): bool {
return (
$this->canEdit( $performer ) &&
!( $filter->isGlobal() && !$this->canEditGlobal( $performer ) )
);
}
/**
* Whether the user can edit a filter with restricted actions enabled.
*
* @param Authority $performer
* @return bool
*/
public function canEditFilterWithRestrictedActions( Authority $performer ): bool {
return $performer->isAllowed( 'abusefilter-modify-restricted' );
}
/**
* @param Authority $performer
* @return bool
*/
public function canViewPrivateFilters( Authority $performer ): bool {
$block = $performer->getBlock();
return (
!( $block && $block->isSitewide() ) &&
$performer->isAllowedAny(
'abusefilter-modify',
'abusefilter-view-private'
)
);
}
/**
* @param Authority $performer
* @return bool
*/
public function canViewProtectedVariables( Authority $performer ) {
$block = $performer->getBlock();
return (
!( $block && $block->isSitewide() ) &&
$performer->isAllowed( 'abusefilter-access-protected-vars' )
);
}
/**
* @param Authority $performer
* @return bool
*/
public function canViewProtectedVariableValues( Authority $performer ) {
return (
$this->canViewProtectedVariables( $performer ) &&
$this->userOptionsLookup->getOption(
$performer->getUser(),
'abusefilter-protected-vars-view-agreement'
)
);
}
/**
* Return all used protected variables from an array of variables. Ignore user permissions.
*
* @param string[] $usedVariables
* @return string[]
*/
public function getUsedProtectedVariables( array $usedVariables ): array {
return array_intersect( $usedVariables, $this->protectedVariables );
}
/**
* Check if the filter uses variables that the user is not allowed to use (i.e., variables that are protected, if
* the user can't view protected variables), and return them.
*
* @param Authority $performer
* @param string[] $usedVariables
* @return string[]
*/
public function getForbiddenVariables( Authority $performer, array $usedVariables ): array {
$usedProtectedVariables = array_intersect( $usedVariables, $this->protectedVariables );
// All good if protected variables aren't used, or the user can view them.
if ( count( $usedProtectedVariables ) === 0 || $this->canViewProtectedVariables( $performer ) ) {
return [];
}
return $usedProtectedVariables;
}
/**
* Return an array of protected variables (originally defined in configuration)
*
* @return string[]
*/
public function getProtectedVariables() {
return $this->protectedVariables;
}
/**
* @param Authority $performer
* @return bool
*/
public function canViewPrivateFiltersLogs( Authority $performer ): bool {
return $this->canViewPrivateFilters( $performer ) ||
$performer->isAllowed( 'abusefilter-log-private' );
}
/**
* @param Authority $performer
* @return bool
*/
public function canViewAbuseLog( Authority $performer ): bool {
return $performer->isAllowed( 'abusefilter-log' );
}
/**
* @param Authority $performer
* @return bool
*/
public function canHideAbuseLog( Authority $performer ): bool {
return $performer->isAllowed( 'abusefilter-hide-log' );
}
/**
* @param Authority $performer
* @return bool
*/
public function canRevertFilterActions( Authority $performer ): bool {
return $performer->isAllowed( 'abusefilter-revert' );
}
/**
* @param Authority $performer
* @param int $privacyLevel Bitmask of privacy flags
* @todo Take a Filter parameter
* @return bool
*/
public function canSeeLogDetailsForFilter( Authority $performer, int $privacyLevel ): bool {
if ( !$this->canSeeLogDetails( $performer ) ) {
return false;
}
if ( $privacyLevel === Flags::FILTER_PUBLIC ) {
return true;
}
if ( FilterUtils::isHidden( $privacyLevel ) && !$this->canViewPrivateFiltersLogs( $performer ) ) {
return false;
}
if ( FilterUtils::isProtected( $privacyLevel ) && !$this->canViewProtectedVariables( $performer ) ) {
return false;
}
return true;
}
/**
* @param Authority $performer
* @return bool
*/
public function canSeeLogDetails( Authority $performer ): bool {
return $performer->isAllowed( 'abusefilter-log-detail' );
}
/**
* @param Authority $performer
* @return bool
*/
public function canSeePrivateDetails( Authority $performer ): bool {
return $performer->isAllowed( 'abusefilter-privatedetails' );
}
/**
* @param Authority $performer
* @return bool
*/
public function canSeeHiddenLogEntries( Authority $performer ): bool {
return $performer->isAllowed( 'abusefilter-hidden-log' );
}
/**
* @param Authority $performer
* @return bool
*/
public function canUseTestTools( Authority $performer ): bool {
// TODO: make independent
return $this->canViewPrivateFilters( $performer );
}
}
|