/** * Performs create access checks. * * This method is supposed to be overwritten by extending classes that * do their own custom access checking. * * @param \Drupal\Core\Session\AccountInterface $account * The user for which to check access. * @param array $context * An array of key-value pairs to pass additional context when needed. * @param string|null $entity_bundle * (optional) The bundle of the entity. Required if the entity supports * bundles, defaults to NULL otherwise. * * @return bool|null * TRUE if access was granted, FALSE if access was denied and NULL if access * could not be determined. */ protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { if ($admin_permission = $this->entityType->getAdminPermission()) { return $account->hasPermission($admin_permission); } else { return NULL; } }
/** * Performs create access checks. * * This method is supposed to be overwritten by extending classes that * do their own custom access checking. * * @param \Drupal\Core\Session\AccountInterface $account * The user for which to check access. * @param array $context * An array of key-value pairs to pass additional context when needed. * @param string|null $entity_bundle * (optional) The bundle of the entity. Required if the entity supports * bundles, defaults to NULL otherwise. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) { if ($admin_permission = $this->entityType->getAdminPermission()) { return AccessResult::allowedIfHasPermission($account, $admin_permission); } else { // No opinion. return AccessResult::neutral(); } }
/** * Gets the collection route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function getCollectionRoute(EntityTypeInterface $entity_type) { if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass()) { $entity_type_id = $entity_type->id(); $route = new Route($entity_type->getLinkTemplate('collection')); $route->setDefaults(['_entity_list' => $entity_type_id, '_title' => (string) $entity_type->getLabel()])->setRequirement('_permission', $entity_type->getAdminPermission())->setOption('_admin_route', TRUE); return $route; } }
/** * Returns the delete multiple form route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function deleteMultipleFormRoute(EntityTypeInterface $entity_type) { if ($entity_type->hasLinkTemplate('delete-multiple-form')) { $route = new Route($entity_type->getLinkTemplate('delete-multiple-form')); $route->setDefault('_form', '\\Drupal\\entity\\Form\\DeleteMultiple'); $route->setDefault('entity_type_id', $entity_type->id()); $route->setRequirement('_permission', $entity_type->getAdminPermission()); return $route; } }
/** * @covers ::checkAccess * * @dataProvider providerTestAccessDelete */ public function testAccessDelete($is_new, $is_fallback, $expected) { $this->entityType->getAdminPermission()->willReturn('test permission'); $page = $this->prophesize(PageInterface::class); $page->isNew()->willReturn($is_new); $page->isFallbackPage()->willReturn($is_fallback); $page->uuid()->shouldBeCalled(); $page->getEntityTypeId()->shouldBeCalled(); // Ensure that the cache tag is added for the temporary conditions. if ($is_new || $is_fallback) { $this->setUpCacheContextsManager(); $page->getCacheTags()->willReturn(['page:1']); $page->getCacheContexts()->willReturn([]); $page->getCacheMaxAge()->willReturn(0); } $account = $this->prophesize(AccountInterface::class); $account->hasPermission('test permission')->willReturn(TRUE); $account->id()->shouldBeCalled(); $this->assertSame($expected, $this->pageAccess->access($page->reveal(), 'delete', NULL, $account->reveal())); }
/** * Gets the settings form route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function getSettingsFormRoute(EntityTypeInterface $entity_type) { if (!$entity_type->getBundleEntityType()) { $route = new Route("/admin/structure/{$entity_type->id()}/settings"); $route->setDefaults(['_form' => 'Drupal\\drupalbristol_sponsors\\Form\\SponsorEntitySettingsForm', '_title' => "{$entity_type->getLabel()} settings"])->setRequirement('_permission', $entity_type->getAdminPermission())->setOption('_admin_route', TRUE); return $route; } }
/** * Gets the collection route. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type. * * @return \Symfony\Component\Routing\Route|null * The generated route, if available. */ protected function getCollectionRoute(EntityTypeInterface $entity_type) { // If the entity type does not provide an admin permission, there is no way // to control access, so we cannot provide a route in a sensible way. if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass() && ($admin_permission = $entity_type->getAdminPermission())) { $route = new Route($entity_type->getLinkTemplate('collection')); $route->addDefaults(['_entity_list' => $entity_type->id(), '_title' => '@label entities', '_title_arguments' => ['@label' => $entity_type->getLabel()]])->setRequirement('_permission', $admin_permission); return $route; } }
protected function getAdminPermission(EntityTypeInterface $entity_type) { switch ($entity_type->id()) { case 'node': return 'administer nodes'; default: return $entity_type->getAdminPermission(); } }