diff --git a/core/lib/Drupal/Core/Entity/DefaultEntityPermissionProvider.php b/core/lib/Drupal/Core/Entity/DefaultEntityPermissionProvider.php index 01fc3bc3f3..2de24c1a81 100644 --- a/core/lib/Drupal/Core/Entity/DefaultEntityPermissionProvider.php +++ b/core/lib/Drupal/Core/Entity/DefaultEntityPermissionProvider.php @@ -75,43 +75,25 @@ public function getCollectionPermission() { /** * {@inheritdoc} */ - public function getViewAnyPublishedPermission($bundle = NULL) { + public function getViewPermission($scope = 'any', $bundle = NULL) { } /** * {@inheritdoc} */ - public function getViewOwnPublishedPermission($bundle = NULL) { + public function getViewUnpublishedPermission($scope = 'own') { } /** * {@inheritdoc} */ - public function getViewOwnUnpublishedPermission() { + public function getUpdatePermission($scope = 'any', $bundle = NULL) { } /** * {@inheritdoc} */ - public function getUpdateAnyPermission($bundle = NULL) { - } - - /** - * {@inheritdoc} - */ - public function getUpdateOwnPermission($bundle = NULL) { - } - - /** - * {@inheritdoc} - */ - public function getDeleteAnyPermission($bundle = NULL) { - } - - /** - * {@inheritdoc} - */ - public function getDeleteOwnPermission($bundle = NULL) { + public function getDeletePermission($scope = 'any', $bundle = NULL) { } /** diff --git a/core/lib/Drupal/Core/Entity/EditorialEntityPermissionProvider.php b/core/lib/Drupal/Core/Entity/EditorialEntityPermissionProvider.php index 4b86960961..2299913187 100644 --- a/core/lib/Drupal/Core/Entity/EditorialEntityPermissionProvider.php +++ b/core/lib/Drupal/Core/Entity/EditorialEntityPermissionProvider.php @@ -420,75 +420,45 @@ public function getCollectionPermission() { /** * {@inheritdoc} */ - public function getViewAnyPublishedPermission($bundle = NULL) { + public function getViewPermission($scope = 'any', $bundle = NULL) { + assert(in_array($scope, ['any', 'own'], TRUE)); if ($this->bundleGranularity && $this->viewBundleGranularity) { - return $this->getPermissionNameForBundle('view any published', $bundle); + return $this->getPermissionNameForBundle("view $scope published", $bundle); } - return $this->permissionMap['view any published']; + return $this->permissionMap["view $scope published"]; } /** * {@inheritdoc} */ - public function getViewOwnPublishedPermission($bundle = NULL) { - if ($this->bundleGranularity && $this->viewBundleGranularity && $this->ownerGranularity) { - return $this->getPermissionNameForBundle('view own published', $bundle); - } - - return $this->permissionMap['view own published']; + public function getViewUnpublishedPermission($scope = 'own') { + assert($scope === 'own'); + return $this->permissionMap["view $scope unpublished"]; } /** * {@inheritdoc} */ - public function getViewOwnUnpublishedPermission() { - return $this->permissionMap['view own unpublished']; - } - - /** - * {@inheritdoc} - */ - public function getUpdateAnyPermission($bundle = NULL) { + public function getUpdatePermission($scope = 'any', $bundle = NULL) { + assert(in_array($scope, ['any', 'own'], TRUE)); if ($this->bundleGranularity) { - return $this->getPermissionNameForBundle('edit any', $bundle); + return $this->getPermissionNameForBundle("edit $scope", $bundle); } - return $this->permissionMap['edit any']; + return $this->permissionMap["edit $scope"]; } /** * {@inheritdoc} */ - public function getUpdateOwnPermission($bundle = NULL) { - if ($this->bundleGranularity && $this->ownerGranularity) { - return $this->getPermissionNameForBundle('edit own', $bundle); - } - - return $this->permissionMap['edit own']; - } - - /** - * {@inheritdoc} - */ - public function getDeleteAnyPermission($bundle = NULL) { + public function getDeletePermission($scope = 'any', $bundle = NULL) { if ($this->bundleGranularity) { - return $this->getPermissionNameForBundle('delete any', $bundle); + return $this->getPermissionNameForBundle("delete $scope", $bundle); } - return $this->permissionMap['delete any']; - - } - - /** - * {@inheritdoc} - */ - public function getDeleteOwnPermission($bundle = NULL) { - if ($this->bundleGranularity && $this->ownerGranularity) { - return $this->getPermissionNameForBundle('delete own', $bundle); - } + return $this->permissionMap["delete $scope"]; - return $this->permissionMap['delete own']; } /** diff --git a/core/lib/Drupal/Core/Entity/EntityPermissionProviderInterface.php b/core/lib/Drupal/Core/Entity/EntityPermissionProviderInterface.php index 8f55429962..5e17011817 100644 --- a/core/lib/Drupal/Core/Entity/EntityPermissionProviderInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityPermissionProviderInterface.php @@ -37,71 +37,58 @@ public function getAdminPermission(); public function getCollectionPermission(); /** - * Gets the name of the 'view any published' permission. + * Gets the name of the 'view any|own published' permission. * + * @param string $scope + * (optional) Whether the 'view any published' or 'view own published' + * permission name should be returned. Defaults to 'any'. * @param string $bundle * (optional) A bundle name. * * @return string|null */ - public function getViewAnyPublishedPermission($bundle = NULL); + public function getViewPermission($scope = 'any', $bundle = NULL); /** - * Gets the name of the 'view own published' permission. + * Gets the name of the 'view any|own unpublished' permission. * - * @param string $bundle - * (optional) A bundle name. - * - * @return string|null - */ - public function getViewOwnPublishedPermission($bundle = NULL); - - /** - * Gets the name of the 'view own unpublished' permission. - * - * @return string|null - */ - public function getViewOwnUnpublishedPermission(); - - /** - * Gets the name of the 'edit any' permission. - * - * @param string $bundle - * (optional) A bundle name. + * Note that only the 'view own unpublished' permission is provided by Drupal + * core, but contrib can implement support for 'view own unpublished' in a + * custom permission provider handler. * - * @return string|null - */ - public function getUpdateAnyPermission($bundle = NULL); - - /** - * Gets the name of the 'edit own' permission. - * - * @param string $bundle - * (optional) A bundle name. + * @param string $scope + * (optional) Whether the 'view any unpublished' or 'view own unpublished' + * permission name should be returned. Defaults to 'own'. * * @return string|null */ - public function getUpdateOwnPermission($bundle = NULL); + public function getViewUnpublishedPermission($scope = 'own'); /** - * Gets the name of the 'delete any' permission. + * Gets the name of the 'edit any|own' permission. * + * @param string $scope + * (optional) Whether the 'edit any' or 'edit own' permission name should be + * returned. Defaults to 'any'. * @param string $bundle * (optional) A bundle name. * * @return string|null */ - public function getDeleteAnyPermission($bundle = NULL); + public function getUpdatePermission($scope = 'any', $bundle = NULL); /** - * Gets the name of the 'delete own' permission. + * Gets the name of the 'delete any|own' permission. * + * @param string $scope + * (optional) Whether the 'delete any' or 'delete own' permission name + * should be returned. Defaults to 'any'. * @param string $bundle * (optional) A bundle name. * * @return string|null */ - public function getDeleteOwnPermission($bundle = NULL); + public function getDeletePermission($scope = 'any', $bundle = NULL); /** * Gets the name of the 'create' permission. diff --git a/core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php b/core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php index 045955e9f9..0e9097bac4 100644 --- a/core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php +++ b/core/lib/Drupal/Core/Entity/Routing/DefaultHtmlRouteProvider.php @@ -324,7 +324,7 @@ protected function getCollectionRoute(EntityTypeInterface $entity_type) { $collection_permission = $permission_provider->getCollectionPermission(); if ($admin_permission || $collection_permission) { - $permission = implode('+', [$admin_permission, $collection_permission]); + $permission = trim(implode('+', [$admin_permission, $collection_permission]), '+'); /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */ $label = $entity_type->getCollectionLabel(); diff --git a/core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php b/core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php index 6d3f8b76c9..b4ddea90cc 100644 --- a/core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/Routing/DefaultHtmlRouteProviderTest.php @@ -8,7 +8,9 @@ namespace Drupal\Tests\Core\Entity\Routing; use Drupal\Core\Config\Entity\ConfigEntityTypeInterface; +use Drupal\Core\Entity\DefaultEntityPermissionProvider; use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityPermissionProviderInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; @@ -54,6 +56,7 @@ protected function setUp() { parent::setUp(); $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class); + $this->entityTypeManager->getPermissionProvider('the_entity_type_id')->willReturn($this->prophesize(EntityPermissionProviderInterface::class)); $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); $this->routeProvider = new TestDefaultHtmlRouteProvider($this->entityTypeManager->reveal(), $this->entityFieldManager->reveal()); @@ -261,7 +264,12 @@ public function providerTestGetCanonicalRoute() { * @dataProvider providerTestGetCollectionRoute */ public function testGetCollectionRoute(Route $expected = NULL, EntityTypeInterface $entity_type) { - $route = $this->routeProvider->getCollectionRoute($entity_type); + $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class); + $entity_type_manager->getPermissionProvider('the_entity_type_id')->willReturn(new DefaultEntityPermissionProvider($entity_type)); + + $route_provider = new TestDefaultHtmlRouteProvider($entity_type_manager->reveal(), $this->entityFieldManager->reveal()); + + $route = $route_provider->getCollectionRoute($entity_type); $this->assertEquals($expected, $route); } @@ -280,6 +288,7 @@ public function providerTestGetCollectionRoute() { $entity_type3 = $this->getEntityType($entity_type2); $entity_type3->hasListBuilderClass()->willReturn(TRUE); $entity_type3->getAdminPermission()->willReturn(FALSE); + $entity_type3->id()->willReturn('the_entity_type_id'); $data['no_admin_permission'] = [NULL, $entity_type3->reveal()]; $entity_type4 = $this->getEntityType($entity_type3);