feat: Allow to specify a custom permission for css unscoping

This commit is contained in:
H. C. Kruse 2023-04-06 20:52:07 +02:00
parent 75eda85afa
commit 1a71b2c664
No known key found for this signature in database
GPG key ID: 0EADE441303A09B4
5 changed files with 44 additions and 8 deletions

View file

@ -38,6 +38,10 @@ Example:
**Note**: Including such a call in a page essentially limits editing to users with the `editinterface` right. You can alternatively include a call to a template that includes the styles. **Note**: Including such a call in a page essentially limits editing to users with the `editinterface` right. You can alternatively include a call to a template that includes the styles.
`$wgTemplateStylesExtenderUnscopingPermission`
Default: `editinterface`
Specify a permission group that is allowed to unscope css.
## Notes on CSS vars ## Notes on CSS vars
Currently using `:root` selectors won't work due to template styles prepending `.mw-parser-output`. Currently using `:root` selectors won't work due to template styles prepending `.mw-parser-output`.

View file

@ -29,8 +29,15 @@
"TemplateStylesExtenderEnableUnscopingSupport": { "TemplateStylesExtenderEnableUnscopingSupport": {
"description": "Allow to unscope css by changing '.mw-parser-output' to a custom class", "description": "Allow to unscope css by changing '.mw-parser-output' to a custom class",
"value": false "value": false
},
"TemplateStylesExtenderUnscopingPermission": {
"description": "Speficy the permission a user must have to use unscoping. Defaults to 'editinterface'.",
"value": "editinterface"
} }
}, },
"ConfigRegistry": {
"TemplateStylesExtender": "GlobalVarConfig::newInstance"
},
"MessagesDirs": { "MessagesDirs": {
"TemplateStylesExtender": [ "TemplateStylesExtender": [
"i18n" "i18n"

View file

@ -1,4 +1,4 @@
{ {
"ext-templatestylesextender-desc": "Extends [https://www.mediawiki.org/wiki/Extension:TemplateStyles TemplateStyles] with new selectors and matchers.", "ext-templatestylesextender-desc": "Extends [https://www.mediawiki.org/wiki/Extension:TemplateStyles TemplateStyles] with new selectors and matchers.",
"templatestylesextender-unscope-no-permisson": "Only users with 'editinterface' permissions can unscope css." "templatestylesextender-unscope-no-permisson": "Only users with 'editinterface' permissions (or a permission set in $wgTemplateStylesExtenderUnscopingPermission) can unscope css."
} }

View file

@ -3,6 +3,7 @@
namespace MediaWiki\Extension\TemplateStylesExtender\Hooks; namespace MediaWiki\Extension\TemplateStylesExtender\Hooks;
use MediaWiki\Extension\TemplateStyles\Hooks; use MediaWiki\Extension\TemplateStyles\Hooks;
use MediaWiki\Extension\TemplateStylesExtender\TemplateStylesExtender;
use MediaWiki\Hook\EditPage__attemptSaveHook; use MediaWiki\Hook\EditPage__attemptSaveHook;
use MediaWiki\Hook\ParserFirstCallInitHook; use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
@ -24,7 +25,10 @@ class MainHooks implements ParserFirstCallInitHook, EditPage__attemptSaveHook {
* @see Hooks::handleTag() * @see Hooks::handleTag()
*/ */
public static function handleTag( $text, $params, $parser, $frame ): string { public static function handleTag( $text, $params, $parser, $frame ): string {
if ( $parser->getOptions() === null || !MediaWikiServices::getInstance()->getMainConfig()->get( 'TemplateStylesExtenderEnableUnscopingSupport' ) ) { if (
$parser->getOptions() === null ||
!TemplateStylesExtender::getConfigValue( 'TemplateStylesExtenderEnableUnscopingSupport' )
) {
return Hooks::handleTag( $text, $params, $parser, $frame ); return Hooks::handleTag( $text, $params, $parser, $frame );
} }
@ -50,7 +54,11 @@ class MainHooks implements ParserFirstCallInitHook, EditPage__attemptSaveHook {
*/ */
public function onEditPage__attemptSave( $editpage_Obj ): bool { public function onEditPage__attemptSave( $editpage_Obj ): bool {
$revision = $editpage_Obj->getExpectedParentRevision(); $revision = $editpage_Obj->getExpectedParentRevision();
if ( $revision === null || !MediaWikiServices::getInstance()->getMainConfig()->get( 'TemplateStylesExtenderEnableUnscopingSupport' ) ) {
if (
$revision === null ||
!TemplateStylesExtender::getConfigValue( 'TemplateStylesExtenderEnableUnscopingSupport' )
) {
return true; return true;
} }
@ -59,13 +67,18 @@ class MainHooks implements ParserFirstCallInitHook, EditPage__attemptSaveHook {
return true; return true;
} }
$permManager = MediaWikiServices::getInstance()->getPermissionManager(); $permission = TemplateStylesExtender::getConfigValue( 'TemplateStylesExtenderUnscopingPermission' );
$user = MediaWikiServices::getInstance()->getUserFactory()->newFromUserIdentity( $editpage_Obj->getContext()->getUser() );
$userCan = $permManager->userHasRight( $user, 'editinterface' ) || $permManager->userCan( 'editinterface', $user, $editpage_Obj->getTitle() ); $permManager = MediaWikiServices::getInstance()->getPermissionManager();
$user = MediaWikiServices::getInstance()
->getUserFactory()
->newFromUserIdentity( $editpage_Obj->getContext()->getUser() );
$userCan = $permManager->userHasRight( $user, $permission ) ||
$permManager->userCan( $permission, $user, $editpage_Obj->getTitle() );
if ( strpos( $content->getText(), 'wrapclass' ) !== false && !$userCan ) { if ( strpos( $content->getText(), 'wrapclass' ) !== false && !$userCan ) {
throw new PermissionsError( 'editinterface', [ 'templatestylesextender-unscope-no-permisson' ] ); throw new PermissionsError( $permission, [ 'templatestylesextender-unscope-no-permisson' ] );
} }
return true; return true;

View file

@ -21,6 +21,7 @@ declare( strict_types=1 );
namespace MediaWiki\Extension\TemplateStylesExtender; namespace MediaWiki\Extension\TemplateStylesExtender;
use Config;
use ConfigException; use ConfigException;
use InvalidArgumentException; use InvalidArgumentException;
use MediaWiki\Extension\TemplateStylesExtender\Matcher\VarNameMatcher; use MediaWiki\Extension\TemplateStylesExtender\Matcher\VarNameMatcher;
@ -37,6 +38,11 @@ use Wikimedia\CSS\Sanitizer\StylePropertySanitizer;
class TemplateStylesExtender { class TemplateStylesExtender {
/**
* @var Config
*/
private static $config;
/** /**
* Adds a css wide keyword matcher for css variables * Adds a css wide keyword matcher for css variables
* Matches 0-INF preceding css declarations at least one var( --content ) and 0-INF following declarations * Matches 0-INF preceding css declarations at least one var( --content ) and 0-INF following declarations
@ -264,8 +270,14 @@ class TemplateStylesExtender {
* @return mixed|null * @return mixed|null
*/ */
public static function getConfigValue( string $key, $default = null ) { public static function getConfigValue( string $key, $default = null ) {
if ( self::$config === null ) {
self::$config = MediaWikiServices::getInstance()
->getConfigFactory()
->makeConfig( 'TemplateStylesExtender' );
}
try { try {
$value = MediaWikiServices::getInstance()->getMainConfig()->get( $key ); $value = self::$config->get( $key );
} catch ( ConfigException $e ) { } catch ( ConfigException $e ) {
wfLogWarning( wfLogWarning(
sprintf( sprintf(