wiki.techinc.nl/includes/exception/PermissionsError.php
DannyS712 f057d2253c Replace deprecated uses of PermissionManager with GroupPermissionsLookup
In a few places where a PermissionManager is used
but only GroupPermissionsLookup is needed

Also update references to the class in PermissionManager
that referred to it as GroupPermissionLookup

Change-Id: I5d7a13900852a38768a106aeee1ce012c3a04ea2
2021-05-26 05:47:21 +00:00

87 lines
2.6 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\PermissionStatus;
/**
* Show an error when a user tries to do something they do not have the necessary
* permissions for.
*
* @newable
* @since 1.18
* @ingroup Exception
*/
class PermissionsError extends ErrorPageError {
public $permission, $errors;
/**
* @stable to call
*
* @param string|null $permission A permission name or null if unknown
* @param array|PermissionStatus $errors Error message keys or [key, param...] arrays or
* PermissionStatus containing an array of errors; must not be empty if $permission is null
* @throws \InvalidArgumentException
*/
public function __construct( $permission, $errors = [] ) {
global $wgLang;
if ( $errors instanceof PermissionStatus ) {
$errors = $errors->toLegacyErrorArray();
}
if ( $permission === null && !$errors ) {
throw new \InvalidArgumentException( __METHOD__ .
': $permission and $errors cannot both be empty' );
}
$this->permission = $permission;
if ( !count( $errors ) ) {
$groups = [];
foreach ( MediaWikiServices::getInstance()
->getGroupPermissionsLookup()
->getGroupsWithPermission( $this->permission ) as $group
) {
$groups[] = UserGroupMembership::getLink( $group, RequestContext::getMain(), 'wiki' );
}
if ( $groups ) {
$errors[] = [ 'badaccess-groups', $wgLang->commaList( $groups ), count( $groups ) ];
} else {
$errors[] = [ 'badaccess-group0' ];
}
}
$this->errors = $errors;
// Give the parent class something to work with
parent::__construct( 'permissionserrors', Message::newFromSpecifier( $errors[0] ) );
}
public function report( $action = self::SEND_OUTPUT ) {
global $wgOut;
$wgOut->showPermissionsErrorPage( $this->errors, $this->permission );
if ( $action === self::SEND_OUTPUT ) {
$wgOut->output();
}
}
}