CommentQueryAccessHandler.php in Open Social 10.2.x
File
modules/social_features/social_comment/src/Entity/Access/CommentQueryAccessHandler.php
View source
<?php
namespace Drupal\social_comment\Entity\Access;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity\QueryAccess\ConditionGroup;
use Drupal\entity\QueryAccess\QueryAccessHandlerBase;
use Drupal\user\EntityOwnerInterface;
class CommentQueryAccessHandler extends QueryAccessHandlerBase {
public function buildConditions($operation, AccountInterface $account) {
$entity_type_id = $this->entityType
->id();
$has_owner = $this->entityType
->entityClassImplements(EntityOwnerInterface::class);
$has_published = $this->entityType
->entityClassImplements(EntityPublishedInterface::class);
if ($has_owner && !$this->entityType
->hasKey('owner')) {
throw new \RuntimeException(sprintf('The "%s" entity type did not define a "owner" key.', $entity_type_id));
}
if ($has_published && !$this->entityType
->hasKey('published')) {
throw new \RuntimeException(sprintf('The "%s" entity type did not define a "published" key', $entity_type_id));
}
if ($account
->hasPermission("administer comments")) {
$conditions = new ConditionGroup('OR');
$conditions
->addCacheContexts([
'user.permissions',
]);
return $conditions;
}
if ($has_owner) {
$entity_conditions = $this
->buildEntityOwnerConditions($operation, $account);
}
else {
$entity_conditions = $this
->buildEntityConditions($operation, $account);
}
$conditions = NULL;
if ($operation == 'view' && $has_published) {
$published_key = $this->entityType
->getKey('published');
$published_conditions = NULL;
if ($entity_conditions) {
$published_conditions = new ConditionGroup('AND');
$published_conditions
->addCacheContexts([
'user.permissions',
]);
$published_conditions
->addCondition($entity_conditions);
$published_conditions
->addCondition($published_key, '1');
}
if ($published_conditions) {
$conditions = $published_conditions;
}
}
else {
$conditions = $entity_conditions;
}
if (!$conditions) {
$conditions = new ConditionGroup('OR');
$conditions
->addCacheContexts([
'user.permissions',
]);
$conditions
->alwaysFalse();
}
return $conditions;
}
protected function buildEntityOwnerConditions($operation, AccountInterface $account) {
$conditions = new ConditionGroup('OR');
$conditions
->addCacheContexts([
'user.permissions',
]);
if ($account
->hasPermission("access comments")) {
return $conditions;
}
return $conditions
->count() ? $conditions : NULL;
}
}