jenkins-bot has submitted this change and it was merged. Change subject: Impersonation for the api ......................................................................
Impersonation for the api Change-Id: If1b3a07873fb95e40c9b43cd7e56db3ff980d836 --- M ZeroPortal.php M includes/ApiZeroPortal.php M includes/PortalSpecialPage.php 3 files changed, 30 insertions(+), 13 deletions(-) Approvals: Yurik: Looks good to me, approved jenkins-bot: Verified diff --git a/ZeroPortal.php b/ZeroPortal.php index 51cdab2..b9249ad 100644 --- a/ZeroPortal.php +++ b/ZeroPortal.php @@ -64,3 +64,5 @@ $wgSpecialPages['ZeroPortal'] = 'ZeroPortal\PortalSpecialPage'; $wgSpecialPageGroups['ZeroPortal'] = 'other'; $wgExtensionMessagesFiles['ZeroPortalAlias'] = __DIR__ . DIRECTORY_SEPARATOR . 'ZeroPortal.alias.php'; + +$wgZeroPortalImpersonateUser = false; diff --git a/includes/ApiZeroPortal.php b/includes/ApiZeroPortal.php index 402a96a..c8f2a2d 100644 --- a/includes/ApiZeroPortal.php +++ b/includes/ApiZeroPortal.php @@ -5,12 +5,14 @@ use ApiBase; use ApiFormatBase; use ApiMain; +use ApiResult; use FauxRequest; use JsonConfig\JCSingleton; use TitleValue; use ZeroBanner\ApiRawJsonPrinter; use ZeroBanner\ZeroConfig; +/** @noinspection PhpInconsistentReturnPointsInspection */ class ApiZeroPortal extends ApiBase { /** * Override built-in handling of format parameter. @@ -33,7 +35,6 @@ $params = $this->extractRequestParams(); $type = $params['type']; $moduleName = $this->getModuleName(); - $result->disableSizeCheck(); if ( !$this->getUser()->isAllowed( 'zero-script' ) ) { $this->dieUsage( "Must be authenticated with zero-script right to use this API", 'login', 401 ); } @@ -46,7 +47,7 @@ $processor = function ( ZeroConfig $content, $title ) use ( $type, $result, $moduleName ) { foreach ( $content->getIpsets() as $name => $ipset ) { $result->addValue( $moduleName, $name === 'default' ? $title : $title . '|' . $name, - $ipset ); + $ipset, ApiResult::NO_SIZE_CHECK ); } }; break; @@ -54,7 +55,7 @@ $processor = function ( ZeroConfig $content, $title ) use ( $type, $result, $moduleName ) { $json = $content->getDataWithDefaults(); unset( $json->ipsets ); - $result->addValue( $moduleName, $title, $json ); + $result->addValue( $moduleName, $title, $json, ApiResult::NO_SIZE_CHECK ); }; break; case 'analyticsconfig': @@ -73,7 +74,7 @@ 'ipsets' => $content->ipsetNames(), ); } - $result->addValue( $moduleName, $title, $val ); + $result->addValue( $moduleName, $title, $val, ApiResult::NO_SIZE_CHECK ); }; break; } @@ -145,11 +146,17 @@ } $reqParams = $reqParamsOriginal; + $ctx = new \DerivativeContext( \RequestContext::getMain() ); + global $wgZeroPortalImpersonateUser; + if ( $wgZeroPortalImpersonateUser ) { + $ctx->setUser( \User::newFromName( $wgZeroPortalImpersonateUser ) ); + } + $hasWarnings = false; while ( true ) { // Get needed data - $req = new FauxRequest( $reqParams ); - $api = new ApiMain( $req ); + $ctx->setRequest( new FauxRequest( $reqParams ) ); + $api = new ApiMain( $ctx ); $api->execute(); $data = $api->getResultData(); // @bug: errors are not returned, they are thrown, when calling api with faux request diff --git a/includes/PortalSpecialPage.php b/includes/PortalSpecialPage.php index ab2002a..596d675 100644 --- a/includes/PortalSpecialPage.php +++ b/includes/PortalSpecialPage.php @@ -34,20 +34,24 @@ $user = $this->getUser(); if ( !$user->isAnon() ) { - $name = $user->getName(); + // Include all zero-config ids if this is an admin + $name = !$user->isAllowed( 'zero-edit' ) ? $user->getName() : true; $configs = array(); ApiZeroPortal::iterateAllConfigs( false, function ( ZeroConfig $content, $title ) use ( $name, &$configs ) { - if ( in_array( $name, $content->admins() ) ) { - $configs[] = 'Zero:' . $title; + if ( $name === true || in_array( $name, $content->admins() ) ) { + $configs[] = $title; } return false; } ); + if ( !$configs ) { + $configs = 'unlisted'; + } } else { - $configs = false; + $configs = 'anonymous'; } - $configs = htmlspecialchars( FormatJson::encode( $configs ) ); - $state = htmlspecialchars( FormatJson::encode( $this->getRequest()->getVal( 's' ) ) ); - $out->addWikiText( "{{PortalHeader}}\n{{#invoke:Portal|main|$configs|$state}}\n{{PortalFooter}}" ); + $configs = self::encodeParameter( $configs ); + $state = self::encodeParameter( $this->getRequest()->getVal( 's' ) ); + $out->addWikiText( "{{PortalHeader}}{{#invoke:Portal|main|$configs|$state}}{{PortalFooter}}" ); // $article = new \WikiPage( Title::newFromText( $title ) ); // if ( $article->exists() ) { // $content = $article->getRevision()->getContent(); @@ -56,4 +60,8 @@ // $out->addHTML( '<h1>Article ' . htmlspecialchars( $article->getTitle() ) . ' does not exist</h1>' ); // } } + + private static function encodeParameter( $value ) { + return str_replace( '|', '|', htmlspecialchars( FormatJson::encode( $value ) ) ); + } } -- To view, visit https://gerrit.wikimedia.org/r/159980 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: If1b3a07873fb95e40c9b43cd7e56db3ff980d836 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ZeroPortal Gerrit-Branch: master Gerrit-Owner: Yurik <yu...@wikimedia.org> Gerrit-Reviewer: Yurik <yu...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits