mediawiki-extensions-AbuseF.../includes/AbuseFilterPermissionManager.php
STran ca23e9f06b Convert af_hidden into a bitmask
Protected variables will cause the filter using them to become
protected as well. `af_hidden` can be used to track this flag,
as it is a TINYINT and can be converted into a bitmask with no
schema changes.

This is not a backwards-compatible change, as now all checks must
check the `hidden` flag specifically or otherwise will be cast to
true if any flag is set.

To support this change:
- "hidden" is considered a flag set in the `af_hidden`. This is a
  change in concept with no need for updates to the column values,
  as there is currently only one flag in the bitmask.
- `Flag`s store the bitmask as well as the state of single flags
  and can return either.
- Any checks against the `af_hidden` value no longer check a
  boolean value and instead now check the `hidden` flag value.

Bug: T363906
Change-Id: I358205cb1119cf1e4004892c37e36e0c0a864f37
2024-05-28 00:59:08 -07:00

156 lines
3.8 KiB
PHP

<?php
namespace MediaWiki\Extension\AbuseFilter;
use MediaWiki\Extension\AbuseFilter\Filter\AbstractFilter;
use MediaWiki\Permissions\Authority;
/**
* 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';
/**
* @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 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 ( FilterUtils::isHidden( $privacyLevel ) ) {
return $this->canSeeLogDetails( $performer )
&& $this->canViewPrivateFiltersLogs( $performer );
}
return $this->canSeeLogDetails( $performer );
}
/**
* @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 );
}
}