Commit fb8d5b1f authored by catch's avatar catch

Issue #2686765 by sidharthap, thhafner: cacheUntilEntityChanges() is...

Issue #2686765 by sidharthap, thhafner: cacheUntilEntityChanges() is deprecated and should be replaced with addCacheableDependency()
parent 11d0d76e
......@@ -148,7 +148,7 @@ protected function processAccessHookResults(array $access) {
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation == 'delete' && $entity->isNew()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($entity);
return AccessResult::forbidden()->addCacheableDependency($entity);
}
if ($admin_permission = $this->entityType->getAdminPermission()) {
return AccessResult::allowedIfHasPermission($account, $this->entityType->getAdminPermission());
......
......@@ -209,7 +209,7 @@ function hook_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupa
// Example code that would prevent displaying the 'Powered by Drupal' block in
// a region different than the footer.
if ($operation == 'view' && $block->getPluginId() == 'system_powered_by_block') {
return AccessResult::forbiddenIf($block->getRegion() != 'footer')->cacheUntilEntityChanges($block);
return AccessResult::forbiddenIf($block->getRegion() != 'footer')->addCacheableDependency($block);
}
// No opinion.
......
......@@ -96,7 +96,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
// Don't grant access to disabled blocks.
if (!$entity->status()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($entity);
return AccessResult::forbidden()->addCacheableDependency($entity);
}
else {
$conditions = [];
......@@ -151,7 +151,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
$this->mergeCacheabilityFromConditions($access, $conditions);
// Ensure that access is evaluated again when the block changes.
return $access->cacheUntilEntityChanges($entity);
return $access->addCacheableDependency($entity);
}
}
......
......@@ -44,7 +44,7 @@ public function __construct(BookManagerInterface $book_manager) {
* The access result.
*/
public function access(NodeInterface $node) {
return AccessResult::allowedIf($this->bookManager->checkNodeIsRemovable($node))->cacheUntilEntityChanges($node);
return AccessResult::allowedIf($this->bookManager->checkNodeIsRemovable($node))->addCacheableDependency($node);
}
}
......@@ -31,7 +31,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
if ($operation == 'approve') {
return AccessResult::allowedIf($comment_admin && !$entity->isPublished())
->cachePerPermissions()
->cacheUntilEntityChanges($entity);
->addCacheableDependency($entity);
}
if ($comment_admin) {
......@@ -41,11 +41,11 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
switch ($operation) {
case 'view':
return AccessResult::allowedIf($account->hasPermission('access comments') && $entity->isPublished())->cachePerPermissions()->cacheUntilEntityChanges($entity)
return AccessResult::allowedIf($account->hasPermission('access comments') && $entity->isPublished())->cachePerPermissions()->addCacheableDependency($entity)
->andIf($entity->getCommentedEntity()->access($operation, $account, TRUE));
case 'update':
return AccessResult::allowedIf($account->id() && $account->id() == $entity->getOwnerId() && $entity->isPublished() && $account->hasPermission('edit own comments'))->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($entity);
return AccessResult::allowedIf($account->id() && $account->id() == $entity->getOwnerId() && $entity->isPublished() && $account->hasPermission('edit own comments'))->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
default:
// No opinion.
......@@ -122,9 +122,9 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_
$admin_access = AccessResult::allowedIfHasPermission($account, 'administer comments');
$anonymous_access = AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT && $account->hasPermission('post comments'))
->cachePerPermissions()
->cacheUntilEntityChanges($entity)
->cacheUntilEntityChanges($field_definition->getConfig($commented_entity->bundle()))
->cacheUntilEntityChanges($commented_entity);
->addCacheableDependency($entity)
->addCacheableDependency($field_definition->getConfig($commented_entity->bundle()))
->addCacheableDependency($commented_entity);
return $admin_access->orIf($anonymous_access);
}
}
......
......@@ -288,7 +288,7 @@ public function replyFormAccess(EntityInterface $entity, $field_name, $pid = NUL
$status = $entity->{$field_name}->status;
$access = $access->andIf(AccessResult::allowedIf($status == CommentItemInterface::OPEN)
->cacheUntilEntityChanges($entity));
->addCacheableDependency($entity));
// $pid indicates that this is a reply to a comment.
if ($pid) {
......@@ -300,7 +300,7 @@ public function replyFormAccess(EntityInterface $entity, $field_name, $pid = NUL
// Check if the parent comment is published and belongs to the entity.
$access = $access->andIf(AccessResult::allowedIf($comment && $comment->isPublished() && $comment->getCommentedEntityId() == $entity->id()));
if ($comment) {
$access->cacheUntilEntityChanges($comment);
$access->addCacheableDependency($comment);
}
}
return $access;
......
......@@ -79,7 +79,7 @@ public function access(UserInterface $user, AccountInterface $account) {
}
// If requested user has been blocked, do not allow users to contact them.
$access->cacheUntilEntityChanges($contact_account);
$access->addCacheableDependency($contact_account);
if ($contact_account->isBlocked()) {
return $access;
}
......
......@@ -282,7 +282,7 @@ function content_translation_translate_access(EntityInterface $entity) {
$condition = $entity instanceof ContentEntityInterface && $entity->access('view') &&
!$entity->getUntranslated()->language()->isLocked() && \Drupal::languageManager()->isMultilingual() && $entity->isTranslatable() &&
($account->hasPermission('create content translations') || $account->hasPermission('update content translations') || $account->hasPermission('delete content translations'));
return AccessResult::allowedIf($condition)->cachePerPermissions()->cacheUntilEntityChanges($entity);
return AccessResult::allowedIf($condition)->cachePerPermissions()->addCacheableDependency($entity);
}
/**
......
......@@ -81,7 +81,7 @@ public function access(Route $route, RouteMatchInterface $route_match, AccountIn
// Translation operations cannot be performed on the default
// translation.
if ($language->getId() == $entity->getUntranslated()->language()->getId()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($entity);
return AccessResult::forbidden()->addCacheableDependency($entity);
}
// Editors have no access to the translation operations, as entity
// access already grants them an equal or greater access level.
......@@ -110,7 +110,7 @@ public function access(Route $route, RouteMatchInterface $route_match, AccountIn
&& isset($languages[$source_language->getId()])
&& isset($languages[$target_language->getId()])
&& !isset($translations[$target_language->getId()]));
return AccessResult::allowedIf($is_new_translation)->cachePerPermissions()->cacheUntilEntityChanges($entity)
return AccessResult::allowedIf($is_new_translation)->cachePerPermissions()->addCacheableDependency($entity)
->andIf($handler->getTranslationAccess($entity, $operation));
case 'delete':
......@@ -118,7 +118,7 @@ public function access(Route $route, RouteMatchInterface $route_match, AccountIn
$has_translation = isset($languages[$language->getId()])
&& $language->getId() != $entity->getUntranslated()->language()->getId()
&& isset($translations[$language->getId()]);
return AccessResult::allowedIf($has_translation)->cachePerPermissions()->cacheUntilEntityChanges($entity)
return AccessResult::allowedIf($has_translation)->cachePerPermissions()->addCacheableDependency($entity)
->andIf($handler->getTranslationAccess($entity, $operation));
}
}
......
......@@ -26,10 +26,10 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
if ($operation == 'delete') {
$field_storage_entity = $entity->getFieldStorageDefinition();
if ($field_storage_entity->isLocked()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($field_storage_entity);
return AccessResult::forbidden()->addCacheableDependency($field_storage_entity);
}
else {
return AccessResult::allowedIfHasPermission($account, 'administer ' . $entity->getTargetEntityTypeId() . ' fields')->cacheUntilEntityChanges($field_storage_entity);
return AccessResult::allowedIfHasPermission($account, 'administer ' . $entity->getTargetEntityTypeId() . ' fields')->addCacheableDependency($field_storage_entity);
}
}
return AccessResult::allowedIfHasPermission($account, 'administer ' . $entity->getTargetEntityTypeId() . ' fields');
......
......@@ -78,7 +78,7 @@ public function access(Route $route, RouteMatchInterface $route_match, AccountIn
}
if ($form_mode_name != 'default' && $entity_display) {
$access->cacheUntilEntityChanges($entity_display);
$access->addCacheableDependency($entity_display);
}
if ($visibility) {
......
......@@ -78,7 +78,7 @@ public function access(Route $route, RouteMatchInterface $route_match, AccountIn
}
if ($view_mode_name != 'default' && $entity_display) {
$access->cacheUntilEntityChanges($entity_display);
$access->addCacheableDependency($entity_display);
}
if ($visibility) {
......
......@@ -26,13 +26,13 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
switch ($operation) {
case 'update':
/* @var \Drupal\Core\Language\LanguageInterface $entity */
return AccessResult::allowedIf(!$entity->isLocked())->cacheUntilEntityChanges($entity)
return AccessResult::allowedIf(!$entity->isLocked())->addCacheableDependency($entity)
->andIf(parent::checkAccess($entity, $operation, $account));
case 'delete':
/* @var \Drupal\Core\Language\LanguageInterface $entity */
return AccessResult::allowedIf(!$entity->isLocked())->cacheUntilEntityChanges($entity)
->andIf(AccessResult::allowedIf(!$entity->isDefault())->cacheUntilEntityChanges($entity))
return AccessResult::allowedIf(!$entity->isLocked())->addCacheableDependency($entity)
->andIf(AccessResult::allowedIf(!$entity->isDefault())->addCacheableDependency($entity))
->andIf(parent::checkAccess($entity, $operation, $account));
default:
......
......@@ -64,7 +64,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
}
else {
// If there is a URL, this is an external link so always accessible.
$access = AccessResult::allowed()->cachePerPermissions()->cacheUntilEntityChanges($entity);
$access = AccessResult::allowed()->cachePerPermissions()->addCacheableDependency($entity);
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
// We allow access, but only if the link is accessible as well.
if (($url_object = $entity->getUrlObject()) && $url_object->isRouted()) {
......@@ -75,7 +75,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
}
case 'delete':
return AccessResult::allowedIf(!$entity->isNew() && $account->hasPermission('administer menu'))->cachePerPermissions()->cacheUntilEntityChanges($entity);
return AccessResult::allowedIf(!$entity->isNew() && $account->hasPermission('administer menu'))->cachePerPermissions()->addCacheableDependency($entity);
}
}
......
......@@ -336,7 +336,7 @@ function hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Se
return AccessResult::allowed()->cachePerPermissions();
}
else {
return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($node);
return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
}
case 'delete':
......@@ -344,7 +344,7 @@ function hook_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Se
return AccessResult::allowed()->cachePerPermissions();
}
else {
return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($node);
return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
}
default:
......
......@@ -915,7 +915,7 @@ function node_node_access(NodeInterface $node, $op, $account) {
return AccessResult::allowed()->cachePerPermissions();
}
else {
return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($node);
return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
}
case 'delete':
......@@ -923,7 +923,7 @@ function node_node_access(NodeInterface $node, $op, $account) {
return AccessResult::allowed()->cachePerPermissions();
}
else {
return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($node);
return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
}
default:
......
......@@ -105,7 +105,7 @@ protected function checkAccess(EntityInterface $node, $operation, AccountInterfa
// Check if authors can view their own unpublished nodes.
if ($operation === 'view' && !$status && $account->hasPermission('view own unpublished content') && $account->isAuthenticated() && $account->id() == $uid) {
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($node);
return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
}
// Evaluate node grants.
......
......@@ -76,7 +76,7 @@ public function access(NodeInterface $node, $operation, AccountInterface $accoun
// Return the equivalent of the default grant, defined by
// self::writeDefault().
if ($operation === 'view') {
return AccessResult::allowedIf($node->isPublished())->cacheUntilEntityChanges($node);
return AccessResult::allowedIf($node->isPublished())->addCacheableDependency($node);
}
else {
return AccessResult::neutral();
......
......@@ -30,10 +30,10 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
case 'delete':
if ($entity->isLocked()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($entity);
return AccessResult::forbidden()->addCacheableDependency($entity);
}
else {
return parent::checkAccess($entity, $operation, $account)->cacheUntilEntityChanges($entity);
return parent::checkAccess($entity, $operation, $account)->addCacheableDependency($entity);
}
break;
......
......@@ -27,21 +27,21 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
/** @var $entity \Drupal\search\SearchPageInterface */
if (in_array($operation, array('delete', 'disable'))) {
if ($entity->isDefaultSearch()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($entity);
return AccessResult::forbidden()->addCacheableDependency($entity);
}
else {
return parent::checkAccess($entity, $operation, $account)->cacheUntilEntityChanges($entity);
return parent::checkAccess($entity, $operation, $account)->addCacheableDependency($entity);
}
}
if ($operation == 'view') {
if (!$entity->status()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($entity);
return AccessResult::forbidden()->addCacheableDependency($entity);
}
$plugin = $entity->getPlugin();
if ($plugin instanceof AccessibleInterface) {
return $plugin->access($operation, $account, TRUE)->cacheUntilEntityChanges($entity);
return $plugin->access($operation, $account, TRUE)->addCacheableDependency($entity);
}
return AccessResult::allowed()->cacheUntilEntityChanges($entity);
return AccessResult::allowed()->addCacheableDependency($entity);
}
return parent::checkAccess($entity, $operation, $account);
}
......
......@@ -18,6 +18,6 @@ function entity_serialization_test_entity_field_access_alter(array &$grants, arr
// Override default access control from UserAccessControlHandler to allow
// access to 'pass' field for the test user.
if ($context['field_definition']->getName() == 'pass' && $context['account']->getUsername() == 'serialization_test_user') {
$grants[':default'] = AccessResult::allowed()->inheritCacheability($grants[':default'])->cacheUntilEntityChanges($context['items']->getEntity());
$grants[':default'] = AccessResult::allowed()->inheritCacheability($grants[':default'])->addCacheableDependency($context['items']->getEntity());
}
}
......@@ -61,7 +61,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
}
// @todo Fix this bizarre code: how can a shortcut exist without a shortcut
// set? The above if-test is unnecessary. See https://www.drupal.org/node/2339903.
return AccessResult::neutral()->cacheUntilEntityChanges($entity);
return AccessResult::neutral()->addCacheableDependency($entity);
}
/**
......
......@@ -31,7 +31,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
if (!$account->hasPermission('access shortcuts')) {
return AccessResult::neutral()->cachePerPermissions();
}
return AccessResult::allowedIf($account->hasPermission('customize shortcut links') && $entity == shortcut_current_displayed_set($account))->cachePerPermissions()->cacheUntilEntityChanges($entity);
return AccessResult::allowedIf($account->hasPermission('customize shortcut links') && $entity == shortcut_current_displayed_set($account))->cachePerPermissions()->addCacheableDependency($entity);
case 'delete':
return AccessResult::allowedIf($account->hasPermission('administer shortcuts') && $entity->id() != 'default')->cachePerPermissions();
......
......@@ -30,10 +30,10 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
// Locked date formats cannot be updated or deleted.
elseif (in_array($operation, array('update', 'delete'))) {
if ($entity->isLocked()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($entity);
return AccessResult::forbidden()->addCacheableDependency($entity);
}
else {
return parent::checkAccess($entity, $operation, $account)->cacheUntilEntityChanges($entity);
return parent::checkAccess($entity, $operation, $account)->addCacheableDependency($entity);
}
}
......
......@@ -29,10 +29,10 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
// Locked menus could not be deleted.
elseif ($operation == 'delete') {
if ($entity->isLocked()) {
return AccessResult::forbidden()->cacheUntilEntityChanges($entity);
return AccessResult::forbidden()->addCacheableDependency($entity);
}
else {
return parent::checkAccess($entity, $operation, $account)->cacheUntilEntityChanges($entity);
return parent::checkAccess($entity, $operation, $account)->addCacheableDependency($entity);
}
}
......
......@@ -418,7 +418,7 @@ function entity_test_entity_field_access($operation, FieldDefinitionInterface $f
*/
function entity_test_entity_field_access_alter(array &$grants, array $context) {
if ($context['field_definition']->getName() == 'field_test_text' && $context['items']->value == 'access alter value') {
$grants[':default'] = AccessResult::forbidden()->inheritCacheability($grants[':default'])->cacheUntilEntityChanges($context['items']->getEntity());
$grants[':default'] = AccessResult::forbidden()->inheritCacheability($grants[':default'])->addCacheableDependency($context['items']->getEntity());
}
}
......
......@@ -55,7 +55,7 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
case 'view':
// Only allow view access if the account is active.
if ($account->hasPermission('access user profiles') && $entity->isActive()) {
return AccessResult::allowed()->cachePerPermissions()->cacheUntilEntityChanges($entity);
return AccessResult::allowed()->cachePerPermissions()->addCacheableDependency($entity);
}
// Users can view own profiles at all times.
elseif ($account->id() == $entity->id()) {
......
......@@ -39,7 +39,7 @@ function views_entity_test_entity_field_access($operation, FieldDefinitionInterf
if ($field_definition->getName() == 'test_text_access') {
if ($items) {
if ($items->value == 'no access value') {
return AccessResult::forbidden()->cacheUntilEntityChanges($items->getEntity());
return AccessResult::forbidden()->addCacheableDependency($items->getEntity());
}
}
}
......
......@@ -69,7 +69,7 @@ function testFieldAccess() {
$account = User::create($values);
$this->assertFalse($entity->field_test_text->access('view', $account), 'Access to the field was denied.');
$expected = AccessResult::forbidden()->cacheUntilEntityChanges($entity);
$expected = AccessResult::forbidden()->addCacheableDependency($entity);
$this->assertEqual($expected, $entity->field_test_text->access('view', $account, TRUE), 'Access to the field was denied.');
$entity->field_test_text = 'access alter value';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment