6e811a97d31f6ecbeb61526b38f5dce022686c48
[yaffs-website] / web / core / modules / comment / src / CommentAccessControlHandler.php
1 <?php
2
3 namespace Drupal\comment;
4
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;
11
12 /**
13  * Defines the access control handler for the comment entity type.
14  *
15  * @see \Drupal\comment\Entity\Comment
16  */
17 class CommentAccessControlHandler extends EntityAccessControlHandler {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
23     /** @var \Drupal\comment\CommentInterface|\Drupal\user\EntityOwnerInterface $entity */
24
25     $comment_admin = $account->hasPermission('administer comments');
26     if ($operation == 'approve') {
27       return AccessResult::allowedIf($comment_admin && !$entity->isPublished())
28         ->cachePerPermissions()
29         ->addCacheableDependency($entity);
30     }
31
32     if ($comment_admin) {
33       $access = AccessResult::allowed()->cachePerPermissions();
34       return ($operation != 'view') ? $access : $access->andIf($entity->getCommentedEntity()->access($operation, $account, TRUE));
35     }
36
37     switch ($operation) {
38       case 'view':
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.");
43         }
44
45         return $access_result;
46
47       case 'update':
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.");
52         }
53         return $access_result;
54
55       default:
56         // No opinion.
57         return AccessResult::neutral()->cachePerPermissions();
58     }
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
65     return AccessResult::allowedIfHasPermission($account, 'post comments');
66   }
67
68   /**
69    * {@inheritdoc}
70    */
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 = [
76         'uid',
77         'status',
78         'created',
79         'date',
80       ];
81       if (in_array($field_definition->getName(), $administrative_fields, TRUE)) {
82         return AccessResult::allowedIfHasPermission($account, 'administer comments');
83       }
84
85       // No user can change read-only fields.
86       $read_only_fields = [
87         'hostname',
88         'changed',
89         'cid',
90         'thread',
91       ];
92       // These fields can be edited during comment creation.
93       $create_only_fields = [
94         'comment_type',
95         'uuid',
96         'entity_id',
97         'entity_type',
98         'field_name',
99         'pid',
100       ];
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);
104       }
105       // We are editing an existing comment - create only fields are now read
106       // only.
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();
110       }
111
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)) {
116         if (!$items) {
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
120           // access.
121           return AccessResult::forbidden();
122         }
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);
135       }
136     }
137
138     if ($operation == 'view') {
139       // Nobody has access to the hostname.
140       if ($field_definition->getName() == 'hostname') {
141         return AccessResult::forbidden();
142       }
143       // The mail field is hidden from non-admins.
144       if ($field_definition->getName() == 'mail') {
145         return AccessResult::allowedIfHasPermission($account, 'administer comments');
146       }
147     }
148     return parent::checkFieldAccess($operation, $field_definition, $account, $items);
149   }
150
151 }