Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/168832
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, 58 insertions(+), 8 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/32/168832/2 diff --git a/repo/includes/actions/ViewEntityAction.php b/repo/includes/actions/ViewEntityAction.php index 11af1a3..a4900f6 100644 --- a/repo/includes/actions/ViewEntityAction.php +++ b/repo/includes/actions/ViewEntityAction.php @@ -163,11 +163,12 @@ if ( $editable && !$content->isRedirect() ) { $permissionChecker = $this->getPermissionChecker(); - $permissionStatus = $permissionChecker->getPermissionStatusForEntity( + $permissionStatus = $permissionChecker->getPermissionForTitle( + $this->getArticle()->getTitle(), + $content, $this->getUser(), - 'edit', - $content->getEntity(), - 'quick' ); + 'edit' + ); $editable = $permissionStatus->isOK(); } diff --git a/repo/includes/content/EntityContentFactory.php b/repo/includes/content/EntityContentFactory.php index 218c43a..ff03275 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,8 +190,20 @@ } /** - * @see EntityPermissionChecker::getPermissionStatusForEntityId + * @param User $user + * @param string $permission + * @param Title $entityPage + * @param string $quick * + * @return string[] + */ + protected function getPermissionErrors( User $user, $permission, Title $entityPage, $quick = '' ) { + //XXX: would be nice to be able to pass the $short flag too, + // as used by getUserPermissionsErrorsInternal. But Title doesn't expose that. + return $entityPage->getUserPermissionsErrors( $permission, $user, $quick !== 'quick' ); + } + + /** * @param User $user * @param string $permission * @param Title $entityPage @@ -202,10 +215,19 @@ */ protected function getPermissionStatus( User $user, $permission, Title $entityPage, $quick = '' ) { wfProfileIn( __METHOD__ ); + $errors = $this->getPermissionErrors( $user, $permission, $entityPage, $quick ); + $status = $this->getStatusForPermissionErrors( $errors ); - //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' ); + wfProfileOut( __METHOD__ ); + return $status; + } + + /** + * @param string[] $errors + * + * @return Status + */ + protected function getStatusForPermissionErrors( array $errors ) { $status = Status::newGood(); foreach ( $errors as $error ) { @@ -301,4 +323,22 @@ return $status; } + /** + * @param Title $title + * @param User $user + * @param string $permission + * + * @return Status + */ + 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' ); + return $this->getStatusForPermissionErrors( $errors ); + } + } diff --git a/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php b/repo/tests/phpunit/includes/content/EntityContentFactoryTest.php index 460cb3f..f922d92 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 ); + $status = $factory->getPermissionForTitle( $title, $content, $wgUser, $action ); + $this->assertEquals( $expectations['getPermissionForTitle'], $status->isOK() ); + } } public function newFromEntityProvider() { -- To view, visit https://gerrit.wikimedia.org/r/168832 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia56f00ffc1338ab1320e5b38446240b3ee360519 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits