Skip to content
Snippets Groups Projects
Commit 575a5342 authored by catch's avatar catch
Browse files

Issue #3371464 by larowlan, Berdir, benjifisher, andypost, dokumori, greggles,...

Issue #3371464 by larowlan, Berdir, benjifisher, andypost, dokumori, greggles, smustgrave, zviryatko, Wim Leers, longwave: CommentAccessControlHandler::checkCreateAccess() does not check commented entity's comment field's status property

(cherry picked from commit e9a09ddd)
parent e370dbe9
No related branches found
No related tags found
20 merge requests!11628Update file MediaLibraryWidget.php,!7564Revert "Issue #3364773 by roshnichordiya, Chris Matthews, thakurnishant_06,...,!5752Issue #3275828 by joachim, quietone, bradjones1, Berdir: document the reason...,!5627Issue #3261805: Field not saved when change of 0 on string start,!5427Issue #3338518: send credentials in ajax if configured in CORS settings.,!5395Issue #3387916 by fjgarlin, Spokje: Each GitLab job exposes user email,!5217Issue #3386607 by alexpott: Improve spell checking in commit-code-check.sh,!5064Issue #3379522 by finnsky, Gauravvvv, kostyashupenko, smustgrave, Chi: Revert...,!5040SDC ComponentElement: Transform slots scalar values to #plain_text instead of throwing an exception,!4958Issue #3392147: Whitelist IP for a Ban module.,!4894Issue #3280279: Add API to allow sites to opt in to upload SVG images in CKEditor 5,!4857Issue #3336994: StringFormatter always displays links to entity even if the user in context does not have access,!4856Issue #3336994: StringFormatter always displays links to entity even if the user in context does not have access,!4788Issue #3272985: RSS Feed header reverts to text/html when cached,!4716Issue #3362929: Improve 400 responses for broken/invalid image style routes,!4553Draft: Issue #2980951: Permission to see own unpublished comments in comment thread,!3679Issue #115801: Allow password on registration without disabling e-mail verification,!3106Issue #3017548: "Filtered HTML" text format does not support manual teaser break (<!--break-->),!925Issue #2339235: Remove taxonomy hard dependency on node module,!872Draft: Issue #3221319: Race condition when creating menu links and editing content deletes menu links
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\comment; namespace Drupal\comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler; use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
...@@ -98,9 +99,22 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_ ...@@ -98,9 +99,22 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_
'field_name', 'field_name',
'pid', 'pid',
]; ];
if ($items && ($entity = $items->getEntity()) && $entity->isNew() && in_array($field_definition->getName(), $create_only_fields, TRUE)) { /** @var \Drupal\comment\CommentInterface|null $entity */
// We are creating a new comment, user can edit create only fields. $entity = $items ? $items->getEntity() : NULL;
return AccessResult::allowedIfHasPermission($account, 'post comments')->addCacheableDependency($entity); $commented_entity = $entity ? $entity->getCommentedEntity() : NULL;
if ($entity && $entity->isNew() && in_array($field_definition->getName(), $create_only_fields, TRUE)) {
$access_result = AccessResult::allowedIfHasPermission($account, 'post comments')
->addCacheableDependency($entity);
$comment_field_name = $entity->get('field_name')->value;
if ($commented_entity && $comment_field_name) {
// We are creating a new comment, user can edit create only fields if
// commenting is open.
$commenting_status = (int) $commented_entity->get($comment_field_name)->status;
$access_result = $access_result
->andIf(AccessResult::allowedIf($commenting_status !== CommentItemInterface::CLOSED))
->addCacheableDependency($commented_entity);
}
return $access_result;
} }
// We are editing an existing comment - create only fields are now read // We are editing an existing comment - create only fields are now read
// only. // only.
...@@ -121,9 +135,6 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_ ...@@ -121,9 +135,6 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_
return AccessResult::forbidden(); return AccessResult::forbidden();
} }
$is_name = $field_definition->getName() === 'name'; $is_name = $field_definition->getName() === 'name';
/** @var \Drupal\comment\CommentInterface $entity */
$entity = $items->getEntity();
$commented_entity = $entity->getCommentedEntity();
$anonymous_contact = $commented_entity->get($entity->getFieldName())->getFieldDefinition()->getSetting('anonymous'); $anonymous_contact = $commented_entity->get($entity->getFieldName())->getFieldDefinition()->getSetting('anonymous');
$admin_access = AccessResult::allowedIfHasPermission($account, 'administer comments'); $admin_access = AccessResult::allowedIfHasPermission($account, 'administer comments');
$anonymous_access = AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && ($anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT || $is_name) && $account->hasPermission('post comments')) $anonymous_access = AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && ($anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT || $is_name) && $account->hasPermission('post comments'))
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
use Drupal\comment\Entity\Comment; use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType; use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait; use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\entity_test\Entity\EntityTest; use Drupal\entity_test\Entity\EntityTest;
...@@ -94,6 +95,7 @@ protected function createEntity() { ...@@ -94,6 +95,7 @@ protected function createEntity() {
$commented_entity = EntityTest::create([ $commented_entity = EntityTest::create([
'name' => 'Camelids', 'name' => 'Camelids',
'type' => 'bar', 'type' => 'bar',
'comment' => CommentItemInterface::OPEN,
]); ]);
$commented_entity->save(); $commented_entity->save();
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
use Drupal\comment\CommentInterface; use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment; use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType; use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait; use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Session\AnonymousUserSession; use Drupal\Core\Session\AnonymousUserSession;
...@@ -103,10 +104,6 @@ public function testAccessToAdministrativeFields() { ...@@ -103,10 +104,6 @@ public function testAccessToAdministrativeFields() {
]); ]);
$comment_type->save(); $comment_type->save();
// Create a comment against a test entity.
$host = EntityTest::create();
$host->save();
// An administrator user. No user exists yet, ensure that the first user // An administrator user. No user exists yet, ensure that the first user
// does not have UID 1. // does not have UID 1.
$comment_admin_user = $this->createUser([ $comment_admin_user = $this->createUser([
...@@ -141,6 +138,15 @@ public function testAccessToAdministrativeFields() { ...@@ -141,6 +138,15 @@ public function testAccessToAdministrativeFields() {
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment'); $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other'); $this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other');
// Create a comment against a test entity.
$host = EntityTest::create();
$host->save();
$host2 = EntityTest::create();
$host2->comment->status = CommentItemInterface::CLOSED;
$host2->comment_other->status = CommentItemInterface::CLOSED;
$host2->save();
// Change the second field's anonymous contact setting. // Change the second field's anonymous contact setting.
$instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other'); $instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other');
// Default is 'May not contact', for this field - they may contact. // Default is 'May not contact', for this field - they may contact.
...@@ -200,10 +206,24 @@ public function testAccessToAdministrativeFields() { ...@@ -200,10 +206,24 @@ public function testAccessToAdministrativeFields() {
'pid' => 0, 'pid' => 0,
'uid' => $anonymous_user->id(), 'uid' => $anonymous_user->id(),
]); ]);
// Note we intentionally don't save this comment so it remains 'new'.
$comment5 = Comment::create([
'entity_type' => 'entity_test',
'hostname' => 'magic.example.com',
// Unpublished.
'status' => 0,
'subject' => 'Wally the Border Collie',
// This one is closed for comments.
'entity_id' => $host2->id(),
'comment_type' => 'comment',
'field_name' => 'comment_other',
'pid' => 0,
'uid' => $anonymous_user->id(),
]);
// Generate permutations. // Generate permutations.
$combinations = [ $combinations = [
'comment' => [$comment1, $comment2, $comment3, $comment4], 'comment' => [$comment1, $comment2, $comment3, $comment4, $comment5],
'user' => [$comment_admin_user, $comment_enabled_user, $comment_no_edit_user, $comment_disabled_user, $anonymous_user], 'user' => [$comment_admin_user, $comment_enabled_user, $comment_no_edit_user, $comment_disabled_user, $anonymous_user],
]; ];
$permutations = $this->generatePermutations($combinations); $permutations = $this->generatePermutations($combinations);
...@@ -278,9 +298,10 @@ public function testAccessToAdministrativeFields() { ...@@ -278,9 +298,10 @@ public function testAccessToAdministrativeFields() {
'@comment' => $set['comment']->getSubject(), '@comment' => $set['comment']->getSubject(),
'@field' => $field, '@field' => $field,
])); ]));
$this->assertEquals($may_update, $set['user']->hasPermission('post comments') && $set['comment']->isNew(), new FormattableMarkup('User @user @state update field @field on comment @comment', [ $expected = $set['user']->hasPermission('post comments') && $set['comment']->isNew() && (int) $set['comment']->getCommentedEntity()->get($set['comment']->getFieldName())->status !== CommentItemInterface::CLOSED;
$this->assertEquals($expected, $may_update, new FormattableMarkup('User @user @state update field @field on comment @comment', [
'@user' => $set['user']->getAccountName(), '@user' => $set['user']->getAccountName(),
'@state' => $may_update ? 'can' : 'cannot', '@state' => $expected ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(), '@comment' => $set['comment']->getSubject(),
'@field' => $field, '@field' => $field,
])); ]));
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
use Drupal\comment\Entity\Comment; use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType; use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait; use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\NestedArray;
...@@ -114,6 +115,7 @@ protected function createEntity() { ...@@ -114,6 +115,7 @@ protected function createEntity() {
$this->commentedEntity = EntityTest::create([ $this->commentedEntity = EntityTest::create([
'name' => 'Camelids', 'name' => 'Camelids',
'type' => 'bar', 'type' => 'bar',
'comment' => CommentItemInterface::OPEN,
]); ]);
$this->commentedEntity->save(); $this->commentedEntity->save();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment