You are here

function comment_delete_comment_access in Comment Delete 8

Implements hook_ENTITY_TYPE_access().

Give access to the comment delete operation.

File

./comment_delete.module, line 20
comment_delete.module

Code

function comment_delete_comment_access(EntityInterface $entity, $operation, AccountInterface $account) {

  /** @var \Drupal\comment\Entity\Comment $entity */
  if ($operation === 'delete') {
    $config = \Drupal::config('comment_delete.config');
    $time = \Drupal::time();
    $threshold = $config
      ->get('threshold');
    if ($threshold && $time
      ->getRequestTime() - $entity
      ->get('created')
      ->getString() >= $threshold) {
      $expired = TRUE;
    }
    else {
      $expired = FALSE;
    }

    // Is able to delete any comment?
    if ($account
      ->hasPermission('delete any comment anytime')) {
      return AccessResult::allowed();
    }
    elseif (!$expired && $account
      ->hasPermission('delete any comment')) {
      return AccessResult::allowed();
    }

    // Is able to delete own comment?
    if ($entity
      ->getOwnerId() === $account
      ->id()) {
      if ($account
        ->hasPermission('delete own comment anytime')) {
        return AccessResult::allowed();
      }
      elseif (!$expired && $account
        ->hasPermission('delete own comment')) {
        return AccessResult::allowed();
      }
    }
  }
  return AccessResult::neutral();
}