bug: Fix checking of editinterface permission

This commit is contained in:
H. C. Kruse 2023-02-11 08:32:01 +01:00
parent ec626cf1f6
commit ce31917593
No known key found for this signature in database
GPG key ID: 0EADE441303A09B4
3 changed files with 36 additions and 38 deletions

View file

@ -1,6 +1,6 @@
{
"name": "octfx/template-styles-extender",
"version": "1.1.3",
"version": "1.1.4",
"type": "mediawiki-extension",
"description": "Extends TemplateStyles with new CSS properties",
"homepage": "http://www.mediawiki.org/wiki/Extension:TemplateStylesExtender",

View file

@ -1,6 +1,6 @@
{
"name": "TemplateStylesExtender",
"version": "1.1.3",
"version": "1.1.4",
"author": [
"[https://www.mediawiki.org/wiki/User:Octfx Octfx]"
],

View file

@ -9,45 +9,43 @@ use MediaWiki\MediaWikiServices;
class MainHooks implements ParserFirstCallInitHook {
/**
* @throws \MWException
*/
public function onParserFirstCallInit( $parser ) {
$parser->setHook( 'templatestyles', [ __CLASS__, 'handleTag' ] );
}
/**
* @throws \MWException
*/
public function onParserFirstCallInit( $parser ) {
$parser->setHook( 'templatestyles', [ __CLASS__, 'handleTag' ] );
}
/**
* This is a wrapper for <templatestyles> tags, that allows unscoping of css for users with 'edit-interface' permissions
* @see Hooks::handleTag()
*/
public static function handleTag( $text, $params, $parser, $frame ) {
if ( $parser->getOptions() === null || !MediaWikiServices::getInstance()->getMainConfig()->get( 'TemplateStylesExtenderEnableUnscopingSupport' ) ) {
return Hooks::handleTag( $text, $params, $parser, $frame );
}
/**
* This is a wrapper for <templatestyles> tags, that allows unscoping of css for users with 'edit-interface' permissions
* @see Hooks::handleTag()
*/
public static function handleTag( $text, $params, $parser, $frame ): string
{
if ( $parser->getOptions() === null || !MediaWikiServices::getInstance()->getMainConfig()->get( 'TemplateStylesExtenderEnableUnscopingSupport' ) ) {
return Hooks::handleTag( $text, $params, $parser, $frame );
}
$options = $parser->getOptions();
$wrapClass = $options->getWrapOutputClass();
$options = $parser->getOptions();
$wrapClass = $options->getWrapOutputClass();
if ( isset( $params['wrapclass'] ) ) {
$userCan = MediaWikiServices::getInstance()->getPermissionManager()->quickUserCan(
'editinterface',
MediaWikiServices::getInstance()->getUserFactory()->newFromUserIdentity( $parser->getUserIdentity() ),
$frame->getTitle()
);
if ( isset( $params['wrapclass'] ) ) {
$permManager = MediaWikiServices::getInstance()->getPermissionManager();
$user = MediaWikiServices::getInstance()->getUserFactory()->newFromUserIdentity( $parser->getUserIdentity() );
if ( $userCan ) {
$options->setOption( 'wrapclass', $params['wrapclass'] );
} else {
return Html::element(
'p',
[ 'class' => 'mw-message-box mw-message-box-error' ],
'User is not allowed to unscope this css. Needs "editinterface" rights.'
);
}
}
$out = Hooks::handleTag( $text, $params, $parser, $frame );
$options->setOption( 'wrapclass', $wrapClass );
if ( $permManager->userHasRight( $user, 'editinterface' ) || $permManager->userCan( 'editinterface', $user, $frame->getTitle() ) ) {
$options->setOption( 'wrapclass', $params['wrapclass'] );
} else {
return Html::element(
'p',
[ 'class' => 'mw-message-box mw-message-box-error' ],
'User is not allowed to unscope this css. Needs "editinterface" rights.'
);
}
}
$out = Hooks::handleTag( $text, $params, $parser, $frame );
$options->setOption( 'wrapclass', $wrapClass );
return $out;
}
return $out;
}
}