class CommentTypeAccessControlHandler in Comment Permissions 8
Override comment access control handler for the comment entity type.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityAccessControlHandler implements EntityAccessControlHandlerInterface
- class \Drupal\comment\CommentAccessControlHandler
- class \Drupal\comment_perm\CommentTypeAccessControlHandler uses CommentAccessTrait
- class \Drupal\comment\CommentAccessControlHandler
- class \Drupal\Core\Entity\EntityAccessControlHandler implements EntityAccessControlHandlerInterface
Expanded class hierarchy of CommentTypeAccessControlHandler
See also
\Drupal\comment\Entity\Comment
File
- src/
CommentTypeAccessControlHandler.php, line 18
Namespace
Drupal\comment_permView source
class CommentTypeAccessControlHandler extends CommentAccessControlHandler {
use CommentAccessTrait;
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\comment\CommentInterface|\Drupal\user\EntityOwnerInterface $entity */
$comment_type = $entity
->bundle();
$comment_admin = $this
->accessAdministerComment($account, $comment_type);
if ($operation == 'approve') {
return AccessResult::allowedIf($comment_admin && !$entity
->isPublished())
->cachePerPermissions()
->addCacheableDependency($entity);
}
if ($comment_admin) {
$access = AccessResult::allowed()
->cachePerPermissions();
return $operation != 'view' ? $access : $access
->andIf($entity
->getCommentedEntity()
->access($operation, $account, TRUE));
}
switch ($operation) {
case 'view':
$access_result = AccessResult::allowedIf($this
->accessComment($account, $comment_type) && $entity
->isPublished())
->cachePerPermissions()
->addCacheableDependency($entity)
->andIf($entity
->getCommentedEntity()
->access($operation, $account, TRUE));
if (!$access_result
->isAllowed()) {
$access_result
->setReason("The 'access comments' permission is required and the comment must be published.");
}
return $access_result;
case 'update':
$access_result = AccessResult::allowedIf($account
->id() && $account
->id() == $entity
->getOwnerId() && $entity
->isPublished() && $this
->accessEditOwnComment($account, $comment_type))
->cachePerPermissions()
->cachePerUser()
->addCacheableDependency($entity);
if (!$access_result
->isAllowed()) {
$access_result
->setReason("The 'edit own comments' permission is required, the user must be the comment author, and the comment must be published.");
}
return $access_result;
default:
// No opinion.
return AccessResult::neutral()
->cachePerPermissions();
}
}
/**
* {@inheritdoc}
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIf($this
->accessPostComment($account, $entity_bundle));
}
/**
* {@inheritdoc}
*/
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
$comment_type = $items
->getEntity()
->bundle();
if ($operation == 'edit') {
// Only users with the "administer comments" permission can edit
// administrative fields.
$administrative_fields = [
'uid',
'status',
'created',
'date',
];
if (in_array($field_definition
->getName(), $administrative_fields, TRUE)) {
return AccessResult::allowedIf($this
->accessAdministerComment($account, $comment_type));
}
// No user can change read-only fields.
$read_only_fields = [
'hostname',
'changed',
'cid',
'thread',
];
// These fields can be edited during comment creation.
$create_only_fields = [
'comment_type',
'uuid',
'entity_id',
'entity_type',
'field_name',
'pid',
];
if ($items && ($entity = $items
->getEntity()) && $entity
->isNew() && in_array($field_definition
->getName(), $create_only_fields, TRUE)) {
// We are creating a new comment, user can edit create only fields.
return AccessResult::allowedIf($this
->accessPostComment($account, $comment_type))
->addCacheableDependency($entity);
}
// We are editing an existing comment - create only fields are now read
// only.
$read_only_fields = array_merge($read_only_fields, $create_only_fields);
if (in_array($field_definition
->getName(), $read_only_fields, TRUE)) {
return AccessResult::forbidden();
}
// If the field is configured to accept anonymous contact details - admins
// can edit name, homepage and mail. Anonymous users can also fill in the
// fields on comment creation.
if (in_array($field_definition
->getName(), [
'name',
'mail',
'homepage',
], TRUE)) {
if (!$items) {
// We cannot make a decision about access to edit these fields if we
// don't have any items and therefore cannot determine the Comment
// entity. In this case we err on the side of caution and prevent edit
// access.
return AccessResult::forbidden();
}
$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');
$admin_access = AccessResult::allowedIf($this
->accessAdministerComment($account, $comment_type));
$anonymous_access = AccessResult::allowedIf($entity
->isNew() && $account
->isAnonymous() && ($anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT || $is_name) && $this
->accessPostComment($account, $comment_type))
->cachePerPermissions()
->addCacheableDependency($entity)
->addCacheableDependency($field_definition
->getConfig($commented_entity
->bundle()))
->addCacheableDependency($commented_entity);
return $admin_access
->orIf($anonymous_access);
}
}
if ($operation == 'view') {
// Nobody has access to the hostname.
if ($field_definition
->getName() == 'hostname') {
return AccessResult::forbidden();
}
// The mail field is hidden from non-admins.
if ($field_definition
->getName() == 'mail') {
return AccessResult::allowedIf($this
->accessAdministerComment($account, $comment_type));
}
}
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommentAccessTrait:: |
public | function | Check if user has access to administer comments. | |
CommentAccessTrait:: |
public | function | Check if user has access to administer comment type. | |
CommentAccessTrait:: |
public | function | Check if user has access to comments. | |
CommentAccessTrait:: |
public | function | Check if user has access to edit own comment. | |
CommentAccessTrait:: |
public | function | Check if user has access to comments. | |
CommentAccessTrait:: |
public | function | Check if user has access to skip comment approval. | |
CommentAccessTrait:: |
public | function | Check if user doesn't have access to administer comments. | |
CommentTypeAccessControlHandler:: |
protected | function |
Performs access checks. Overrides CommentAccessControlHandler:: |
|
CommentTypeAccessControlHandler:: |
protected | function |
Performs create access checks. Overrides CommentAccessControlHandler:: |
|
CommentTypeAccessControlHandler:: |
protected | function |
Default field access as determined by this access control handler. Overrides CommentAccessControlHandler:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityAccessControlHandler:: |
protected | property | Stores calculated access check results. | |
EntityAccessControlHandler:: |
protected | property | Information about the entity type. | |
EntityAccessControlHandler:: |
protected | property | The entity type ID of the access control handler instance. | |
EntityAccessControlHandler:: |
protected | property | Allows to grant access to just the labels. | 5 |
EntityAccessControlHandler:: |
public | function |
Checks access to an operation on a given entity or entity translation. Overrides EntityAccessControlHandlerInterface:: |
1 |
EntityAccessControlHandler:: |
public | function |
Checks access to create an entity. Overrides EntityAccessControlHandlerInterface:: |
1 |
EntityAccessControlHandler:: |
public | function |
Checks access to an operation on a given entity field. Overrides EntityAccessControlHandlerInterface:: |
|
EntityAccessControlHandler:: |
protected | function | Tries to retrieve a previously cached access value from the static cache. | |
EntityAccessControlHandler:: |
protected | function | Loads the current account object, if it does not exist yet. | |
EntityAccessControlHandler:: |
protected | function | We grant access to the entity if both of these conditions are met: | |
EntityAccessControlHandler:: |
public | function |
Clears all cached access checks. Overrides EntityAccessControlHandlerInterface:: |
|
EntityAccessControlHandler:: |
protected | function | Statically caches whether the given user has access. | |
EntityAccessControlHandler:: |
public | function | Constructs an access control handler instance. | 5 |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 2 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 2 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |