PermissionManager: add throwPermissionErrors()

This method should make it easier to immediately abort a code path if
there are any permission errors, for callers who aren’t interested in
the details of the errors but just want to present them to the user.

Change-Id: Ie64881a003fe8889de3439883840f69781962df0
This commit is contained in:
Lucas Werkmeister 2021-11-30 16:23:54 +01:00
parent f2f0201981
commit 48e8587780

View file

@ -39,6 +39,7 @@ use MediaWiki\User\UserGroupManager;
use MediaWiki\User\UserIdentity;
use MessageSpecifier;
use NamespaceInfo;
use PermissionsError;
use RequestContext;
use SpecialPage;
use Title;
@ -358,6 +359,35 @@ class PermissionManager {
return array_values( $errors );
}
/**
* Like {@link getPermissionErrors}, but immediately throw if there are any errors.
*
* @param string $action Action that permission needs to be checked for
* @param User $user User to check
* @param LinkTarget $page
* @param string $rigor One of PermissionManager::RIGOR_ constants
* - RIGOR_QUICK : does cheap permission checks from replica DBs (usable for GUI creation)
* - RIGOR_FULL : does cheap and expensive checks possibly from a replica DB
* - RIGOR_SECURE : does cheap and expensive checks, using the primary DB as needed
* @param string[] $ignoreErrors Set this to a list of message keys
* whose corresponding errors may be ignored.
*
* @throws PermissionsError
*/
public function throwPermissionErrors(
$action,
User $user,
LinkTarget $page,
$rigor = self::RIGOR_SECURE,
$ignoreErrors = []
): void {
$permissionErrors = $this->getPermissionErrors(
$action, $user, $page, $rigor, $ignoreErrors );
if ( $permissionErrors !== [] ) {
throw new PermissionsError( $action, $permissionErrors );
}
}
/**
* Check if user is blocked from editing a particular article. If the user does not
* have a block, this will return false.