3 namespace Drupal\comment;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityAccessControlHandler;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Field\FieldDefinitionInterface;
9 use Drupal\Core\Field\FieldItemListInterface;
10 use Drupal\Core\Session\AccountInterface;
13 * Defines the access control handler for the comment entity type.
15 * @see \Drupal\comment\Entity\Comment
17 class CommentAccessControlHandler extends EntityAccessControlHandler {
22 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
23 /** @var \Drupal\comment\CommentInterface|\Drupal\user\EntityOwnerInterface $entity */
25 $comment_admin = $account->hasPermission('administer comments');
26 if ($operation == 'approve') {
27 return AccessResult::allowedIf($comment_admin && !$entity->isPublished())
28 ->cachePerPermissions()
29 ->addCacheableDependency($entity);
33 $access = AccessResult::allowed()->cachePerPermissions();
34 return ($operation != 'view') ? $access : $access->andIf($entity->getCommentedEntity()->access($operation, $account, TRUE));
39 $access_result = AccessResult::allowedIf($account->hasPermission('access comments') && $entity->isPublished())->cachePerPermissions()->addCacheableDependency($entity)
40 ->andIf($entity->getCommentedEntity()->access($operation, $account, TRUE));
41 if (!$access_result->isAllowed()) {
42 $access_result->setReason("The 'access comments' permission is required and the comment must be published.");
45 return $access_result;
48 $access_result = AccessResult::allowedIf($account->id() && $account->id() == $entity->getOwnerId() && $entity->isPublished() && $account->hasPermission('edit own comments'))
49 ->cachePerPermissions()->cachePerUser()->addCacheableDependency($entity);
50 if (!$access_result->isAllowed()) {
51 $access_result->setReason("The 'edit own comments' permission is required, the user must be the comment author, and the comment must be published.");
53 return $access_result;
57 return AccessResult::neutral()->cachePerPermissions();
64 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
65 return AccessResult::allowedIfHasPermission($account, 'post comments');
71 protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
72 if ($operation == 'edit') {
73 // Only users with the "administer comments" permission can edit
74 // administrative fields.
75 $administrative_fields = [
81 if (in_array($field_definition->getName(), $administrative_fields, TRUE)) {
82 return AccessResult::allowedIfHasPermission($account, 'administer comments');
85 // No user can change read-only fields.
92 // These fields can be edited during comment creation.
93 $create_only_fields = [
101 if ($items && ($entity = $items->getEntity()) && $entity->isNew() && in_array($field_definition->getName(), $create_only_fields, TRUE)) {
102 // We are creating a new comment, user can edit create only fields.
103 return AccessResult::allowedIfHasPermission($account, 'post comments')->addCacheableDependency($entity);
105 // We are editing an existing comment - create only fields are now read
107 $read_only_fields = array_merge($read_only_fields, $create_only_fields);
108 if (in_array($field_definition->getName(), $read_only_fields, TRUE)) {
109 return AccessResult::forbidden();
112 // If the field is configured to accept anonymous contact details - admins
113 // can edit name, homepage and mail. Anonymous users can also fill in the
114 // fields on comment creation.
115 if (in_array($field_definition->getName(), ['name', 'mail', 'homepage'], TRUE)) {
117 // We cannot make a decision about access to edit these fields if we
118 // don't have any items and therefore cannot determine the Comment
119 // entity. In this case we err on the side of caution and prevent edit
121 return AccessResult::forbidden();
123 $is_name = $field_definition->getName() === 'name';
124 /** @var \Drupal\comment\CommentInterface $entity */
125 $entity = $items->getEntity();
126 $commented_entity = $entity->getCommentedEntity();
127 $anonymous_contact = $commented_entity->get($entity->getFieldName())->getFieldDefinition()->getSetting('anonymous');
128 $admin_access = AccessResult::allowedIfHasPermission($account, 'administer comments');
129 $anonymous_access = AccessResult::allowedIf($entity->isNew() && $account->isAnonymous() && ($anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT || $is_name) && $account->hasPermission('post comments'))
130 ->cachePerPermissions()
131 ->addCacheableDependency($entity)
132 ->addCacheableDependency($field_definition->getConfig($commented_entity->bundle()))
133 ->addCacheableDependency($commented_entity);
134 return $admin_access->orIf($anonymous_access);
138 if ($operation == 'view') {
139 // Nobody has access to the hostname.
140 if ($field_definition->getName() == 'hostname') {
141 return AccessResult::forbidden();
143 // The mail field is hidden from non-admins.
144 if ($field_definition->getName() == 'mail') {
145 return AccessResult::allowedIfHasPermission($account, 'administer comments');
148 return parent::checkFieldAccess($operation, $field_definition, $account, $items);