Jeroen De Dauw has submitted this change and it was merged. Change subject: Add getPermissionForTitle method to use in ViewEntityAction ......................................................................
Add getPermissionForTitle method to use in ViewEntityAction Change-Id: Ia56f00ffc1338ab1320e5b38446240b3ee360519 --- M repo/includes/actions/ViewEntityAction.php M repo/includes/content/EntityContentFactory.php M repo/tests/phpunit/includes/content/EntityContentFactoryTest.php 3 files changed, 52 insertions(+), 13 deletions(-) Approvals: Jeroen De Dauw: Looks good to me, approved diff --git a/repo/includes/actions/ViewEntityAction.php b/repo/includes/actions/ViewEntityAction.php index 11af1a3..e814904 100644 --- a/repo/includes/actions/ViewEntityAction.php +++ b/repo/includes/actions/ViewEntityAction.php @@ -163,13 +163,12 @@ if ( $editable && !$content->isRedirect() ) { $permissionChecker = $this->getPermissionChecker(); - $permissionStatus = $permissionChecker->getPermissionStatusForEntity( + $editable = $permissionChecker->getPermissionForTitle( + $this->getArticle()->getTitle(), + $content, $this->getUser(), - 'edit', - $content->getEntity(), - 'quick' ); - - $editable = $permissionStatus->isOK(); + 'edit' + ); } $parserOptions = $this->getArticle()->getPage()->makeParserOptions( $this->getContext()->getUser() ); diff --git a/repo/includes/content/EntityContentFactory.php b/repo/includes/content/EntityContentFactory.php index 218c43a..b130d67 100644 --- a/repo/includes/content/EntityContentFactory.php +++ b/repo/includes/content/EntityContentFactory.php @@ -11,6 +11,7 @@ use User; use Wikibase\DataModel\Entity\Entity; use Wikibase\DataModel\Entity\EntityId; +use Wikibase\EntityContent; use Wikibase\Lib\Store\EntityRedirect; use Wikibase\Lib\Store\EntityTitleLookup; use Wikibase\Repo\Store\EntityPermissionChecker; @@ -189,23 +190,32 @@ } /** - * @see EntityPermissionChecker::getPermissionStatusForEntityId - * * @param User $user * @param string $permission * @param Title $entityPage * @param string $quick * - * @return Status a status object representing the check's result. - * + * //XXX: would be nice to be able to pass the $short flag too, + * as used by getUserPermissionsErrorsInternal. But Title doesn't expose that. * @todo Move to a separate service (merge into WikiPageEntityStore?) + * + * @return Status a status object representing the check's result. */ protected function getPermissionStatus( User $user, $permission, Title $entityPage, $quick = '' ) { wfProfileIn( __METHOD__ ); - - //XXX: would be nice to be able to pass the $short flag too, - // as used by getUserPermissionsErrorsInternal. But Title doesn't expose that. $errors = $entityPage->getUserPermissionsErrors( $permission, $user, $quick !== 'quick' ); + $status = $this->getStatusForPermissionErrors( $errors ); + + wfProfileOut( __METHOD__ ); + return $status; + } + + /** + * @param string[] $errors + * + * @return Status + */ + protected function getStatusForPermissionErrors( array $errors ) { $status = Status::newGood(); foreach ( $errors as $error ) { @@ -301,4 +311,25 @@ return $status; } + /** + * @param Title $title + * @param EntityContent $content + * @param User $user + * @param string $permission + * + * @return boolean + */ + public function getPermissionForTitle( Title $title, EntityContent $content, User $user, $permission ) { + $entityContentTitle = $this->getTitleForId( $content->getEntity()->getId() ); + + if ( $entityContentTitle->getFullText() !== $title->getFullText() ) { + throw new MWException( '$title does not match content' ); + } + + $errors = $title->getUserPermissionsErrors( $permission, $user, 'quick' ); + $permissionStatus = $this->getStatusForPermissionErrors( $errors ); + + return $permissionStatus->isOK(); + } + } diff --git a/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php b/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php index 460cb3f..e18755a 100644 --- a/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php +++ b/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php @@ -151,6 +151,7 @@ 'getPermissionStatusForEntity' => true, 'getPermissionStatusForEntityType' => true, 'getPermissionStatusForEntityId' => true, + 'getPermissionForTitle' => true, ), ), 'edit not allowed' => array( @@ -161,6 +162,7 @@ 'getPermissionStatusForEntity' => false, 'getPermissionStatusForEntityType' => false, 'getPermissionStatusForEntityId' => false, + 'getPermissionForTitle' => false, ), ), 'delete not allowed' => array( @@ -216,6 +218,13 @@ $status = $factory->getPermissionStatusForEntityId( $wgUser, $action, $entity->getId() ); $this->assertEquals( $expectations['getPermissionStatusForEntityId'], $status->isOK() ); } + + if ( isset( $expectations['getPermissionForTitle'] ) ) { + $title = $factory->getTitleForId( $entity->getId() ); + $content = new \Wikibase\ItemContent( $entity ); + $hasPermission = $factory->getPermissionForTitle( $title, $content, $wgUser, $action ); + $this->assertEquals( $expectations['getPermissionForTitle'], $hasPermission ); + } } public function newFromEntityProvider() { -- To view, visit https://gerrit.wikimedia.org/r/168832 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ia56f00ffc1338ab1320e5b38446240b3ee360519 Gerrit-PatchSet: 4 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits