2020-09-18 14:49:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\AbuseFilter;
|
|
|
|
|
2024-05-23 14:49:17 +00:00
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2020-10-10 17:20:21 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\AbstractFilter;
|
2024-07-02 20:43:09 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\Filter\Flags;
|
2021-06-05 05:06:22 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2024-08-14 14:11:10 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2020-09-18 14:49:13 +00:00
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* This class simplifies the interactions between the AbuseFilter code and Authority, knowing
|
2020-09-18 14:49:13 +00:00
|
|
|
* what rights are required to perform AF-related actions.
|
|
|
|
*/
|
|
|
|
class AbuseFilterPermissionManager {
|
|
|
|
public const SERVICE_NAME = 'AbuseFilterPermissionManager';
|
|
|
|
|
2024-05-23 14:49:17 +00:00
|
|
|
public const CONSTRUCTOR_OPTIONS = [
|
|
|
|
'AbuseFilterProtectedVariables',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string[] Protected variables defined in config via AbuseFilterProtectedVariables
|
|
|
|
*/
|
|
|
|
private $protectedVariables;
|
|
|
|
|
2024-08-14 14:11:10 +00:00
|
|
|
private UserOptionsLookup $userOptionsLookup;
|
|
|
|
|
2024-05-23 14:49:17 +00:00
|
|
|
/**
|
|
|
|
* @param ServiceOptions $options
|
2024-08-14 14:11:10 +00:00
|
|
|
* @param UserOptionsLookup $userOptionsLookup
|
2024-05-23 14:49:17 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2024-08-14 14:11:10 +00:00
|
|
|
ServiceOptions $options,
|
|
|
|
UserOptionsLookup $userOptionsLookup
|
2024-05-23 14:49:17 +00:00
|
|
|
) {
|
|
|
|
$this->protectedVariables = $options->get( 'AbuseFilterProtectedVariables' );
|
2024-08-14 14:11:10 +00:00
|
|
|
$this->userOptionsLookup = $userOptionsLookup;
|
2024-05-23 14:49:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 14:49:13 +00:00
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canEdit( Authority $performer ): bool {
|
|
|
|
$block = $performer->getBlock();
|
2020-09-18 14:49:13 +00:00
|
|
|
return (
|
|
|
|
!( $block && $block->isSitewide() ) &&
|
2021-06-05 05:06:22 +00:00
|
|
|
$performer->isAllowed( 'abusefilter-modify' )
|
2020-09-18 14:49:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canEditGlobal( Authority $performer ): bool {
|
|
|
|
return $performer->isAllowed( 'abusefilter-modify-global' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the user can edit the given filter.
|
|
|
|
*
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-10-10 17:20:21 +00:00
|
|
|
* @param AbstractFilter $filter
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canEditFilter( Authority $performer, AbstractFilter $filter ): bool {
|
2020-09-18 14:49:13 +00:00
|
|
|
return (
|
2021-06-05 05:06:22 +00:00
|
|
|
$this->canEdit( $performer ) &&
|
|
|
|
!( $filter->isGlobal() && !$this->canEditGlobal( $performer ) )
|
2020-09-18 14:49:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the user can edit a filter with restricted actions enabled.
|
|
|
|
*
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canEditFilterWithRestrictedActions( Authority $performer ): bool {
|
|
|
|
return $performer->isAllowed( 'abusefilter-modify-restricted' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canViewPrivateFilters( Authority $performer ): bool {
|
2022-10-12 21:15:53 +00:00
|
|
|
$block = $performer->getBlock();
|
|
|
|
return (
|
|
|
|
!( $block && $block->isSitewide() ) &&
|
|
|
|
$performer->isAllowedAny(
|
|
|
|
'abusefilter-modify',
|
|
|
|
'abusefilter-view-private'
|
|
|
|
)
|
2020-09-18 14:49:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-23 14:49:17 +00:00
|
|
|
/**
|
|
|
|
* @param Authority $performer
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canViewProtectedVariables( Authority $performer ) {
|
|
|
|
$block = $performer->getBlock();
|
|
|
|
return (
|
|
|
|
!( $block && $block->isSitewide() ) &&
|
|
|
|
$performer->isAllowed( 'abusefilter-access-protected-vars' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-14 14:11:10 +00:00
|
|
|
/**
|
|
|
|
* @param Authority $performer
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canViewProtectedVariableValues( Authority $performer ) {
|
|
|
|
return (
|
|
|
|
$this->canViewProtectedVariables( $performer ) &&
|
|
|
|
$this->userOptionsLookup->getOption(
|
|
|
|
$performer->getUser(),
|
|
|
|
'abusefilter-protected-vars-view-agreement'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-12 14:45:57 +00:00
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
}
|
|
|
|
|
2024-05-23 14:49:17 +00:00
|
|
|
/**
|
2024-07-02 20:53:28 +00:00
|
|
|
* 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.
|
2024-05-23 14:49:17 +00:00
|
|
|
*
|
|
|
|
* @param Authority $performer
|
|
|
|
* @param string[] $usedVariables
|
2024-07-02 20:53:28 +00:00
|
|
|
* @return string[]
|
2024-05-23 14:49:17 +00:00
|
|
|
*/
|
2024-07-02 20:53:28 +00:00
|
|
|
public function getForbiddenVariables( Authority $performer, array $usedVariables ): array {
|
2024-05-23 14:49:17 +00:00
|
|
|
$usedProtectedVariables = array_intersect( $usedVariables, $this->protectedVariables );
|
2024-07-02 20:53:28 +00:00
|
|
|
// All good if protected variables aren't used, or the user can view them.
|
|
|
|
if ( count( $usedProtectedVariables ) === 0 || $this->canViewProtectedVariables( $performer ) ) {
|
|
|
|
return [];
|
2024-05-23 14:49:17 +00:00
|
|
|
}
|
2024-07-02 20:53:28 +00:00
|
|
|
return $usedProtectedVariables;
|
2024-05-23 14:49:17 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 14:11:10 +00:00
|
|
|
/**
|
|
|
|
* Return an array of protected variables (originally defined in configuration)
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getProtectedVariables() {
|
|
|
|
return $this->protectedVariables;
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:49:13 +00:00
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canViewPrivateFiltersLogs( Authority $performer ): bool {
|
|
|
|
return $this->canViewPrivateFilters( $performer ) ||
|
|
|
|
$performer->isAllowed( 'abusefilter-log-private' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canViewAbuseLog( Authority $performer ): bool {
|
|
|
|
return $performer->isAllowed( 'abusefilter-log' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canHideAbuseLog( Authority $performer ): bool {
|
|
|
|
return $performer->isAllowed( 'abusefilter-hide-log' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canRevertFilterActions( Authority $performer ): bool {
|
|
|
|
return $performer->isAllowed( 'abusefilter-revert' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2024-05-16 10:40:53 +00:00
|
|
|
* @param int $privacyLevel Bitmask of privacy flags
|
2020-10-27 19:21:44 +00:00
|
|
|
* @todo Take a Filter parameter
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2024-05-16 10:40:53 +00:00
|
|
|
public function canSeeLogDetailsForFilter( Authority $performer, int $privacyLevel ): bool {
|
2024-07-02 20:43:09 +00:00
|
|
|
if ( !$this->canSeeLogDetails( $performer ) ) {
|
|
|
|
return false;
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-02 20:43:09 +00:00
|
|
|
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;
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-02 20:43:09 +00:00
|
|
|
return true;
|
2020-10-27 19:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-10-27 19:21:44 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canSeeLogDetails( Authority $performer ): bool {
|
|
|
|
return $performer->isAllowed( 'abusefilter-log-detail' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canSeePrivateDetails( Authority $performer ): bool {
|
|
|
|
return $performer->isAllowed( 'abusefilter-privatedetails' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2020-09-18 14:49:13 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canSeeHiddenLogEntries( Authority $performer ): bool {
|
|
|
|
return $performer->isAllowed( 'abusefilter-hidden-log' );
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|
2021-02-19 17:50:38 +00:00
|
|
|
|
|
|
|
/**
|
2021-06-05 05:06:22 +00:00
|
|
|
* @param Authority $performer
|
2021-02-19 17:50:38 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-06-05 05:06:22 +00:00
|
|
|
public function canUseTestTools( Authority $performer ): bool {
|
2021-02-19 17:50:38 +00:00
|
|
|
// TODO: make independent
|
2021-06-05 05:06:22 +00:00
|
|
|
return $this->canViewPrivateFilters( $performer );
|
2021-02-19 17:50:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 14:49:13 +00:00
|
|
|
}
|