diff --git a/core/modules/comment/src/CommentAccessControlHandler.php b/core/modules/comment/src/CommentAccessControlHandler.php index 836c243..2dec0d9 100644 --- a/core/modules/comment/src/CommentAccessControlHandler.php +++ b/core/modules/comment/src/CommentAccessControlHandler.php @@ -13,6 +13,7 @@ use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\field\Entity\FieldInstanceConfig; /** * Defines the access control handler for the comment entity type. @@ -66,7 +67,7 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_ 'date', ); if ($operation == 'edit' && in_array($field_definition->getName(), $administrative_fields, TRUE)) { - return $account->hasPermission('administer comments'); + return AccessResult::allowedIfHasPermission($account, 'administer comments'); } // No user can change read-only fields. @@ -83,7 +84,7 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_ 'field_name', ); if ($operation == 'edit' && in_array($field_definition->getName(), $read_only_fields, TRUE)) { - return FALSE; + return AccessResult::forbidden(); } /** @var \Drupal\comment\CommentInterface $entity */ @@ -93,18 +94,19 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_ // Only admins or anonymous users can edit name and mail. if ($operation == 'edit' && in_array($field_definition->getName(), ['name', 'mail', 'homepage'], TRUE)) { - if ($account->hasPermission('administer comments')) { - return TRUE; - } // Anonymous users can only access these fields if the field is configured // to accept anonymous contact details. - if ($entity->isNew() && $account->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT && $account->hasPermission('post comments')) { - return TRUE; - } - return FALSE; + return AccessResult::allowedIf($account->hasPermission('administer comments'))->cachePerRole() + ->orIf(AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT && $account->hasPermission('post comments')) + ->cachePerRole() + ->cacheUntilEntityChanges($entity) + ->cacheUntilEntityChanges($field_definition->getConfig($commented_entity->bundle())) + ->cacheUntilEntityChanges($commented_entity)); } if ($operation == 'view' && !$account->hasPermission('administer comments') && (!$account->hasPermission('access comments') || !$entity->isPublished())) { - return FALSE; + return AccessResult::forbiddenIf(!$account->hasPermission('administer comments'))->cachePerRole() + ->orIf(AccessResult::forbiddenIf(!$account->hasPermission('access comments') || !$entity->isPublished()) + ->cacheUntilEntityChanges($entity)); } return parent::checkFieldAccess($operation, $field_definition, $account, $items); } diff --git a/core/modules/comment/src/Tests/CommentFieldAccessTest.php b/core/modules/comment/src/Tests/CommentFieldAccessTest.php index 6b73b28..4b00397 100644 --- a/core/modules/comment/src/Tests/CommentFieldAccessTest.php +++ b/core/modules/comment/src/Tests/CommentFieldAccessTest.php @@ -144,8 +144,8 @@ public function testAccessToAdministrativeFields() { $comment1 = Comment::create([ 'entity_type' => 'entity_test', 'name' => 'Tony', - 'hostname' => 'magical.ponies.com', - 'mail' => 'tony.the.magical@ponies.com', + 'hostname' => 'magic.example.com', + 'mail' => 'tonythemagicalpony@example.com', 'subject' => 'Bruce the Mesopotamian moose', 'entity_id' => $host->id(), 'comment_type' => 'comment', @@ -157,7 +157,7 @@ public function testAccessToAdministrativeFields() { $comment1->save(); $comment2 = Comment::create([ 'entity_type' => 'entity_test', - 'hostname' => 'magical.ponies.com', + 'hostname' => 'magic.example.com', 'subject' => 'Brian the messed up lion', 'entity_id' => $host->id(), 'comment_type' => 'comment', @@ -169,7 +169,7 @@ public function testAccessToAdministrativeFields() { $comment2->save(); $comment3 = Comment::create([ 'entity_type' => 'entity_test', - 'hostname' => 'magical.ponies.com', + 'hostname' => 'magic.example.com', // Unpublished. 'status' => 0, 'subject' => 'Gail the minky whale', @@ -183,7 +183,7 @@ public function testAccessToAdministrativeFields() { // Note we intentionally don't save this comment so it remains 'new'. $comment4 = Comment::create([ 'entity_type' => 'entity_test', - 'hostname' => 'magical.ponies.com', + 'hostname' => 'magic.example.com', // Unpublished. 'status' => 0, 'subject' => 'Gail the minky whale',