You are here

protected function EmailConfirmationAccessControlHandler::checkAccess in Email confirmer 8

Performs access checks.

This method is supposed to be overwritten by extending classes that do their own custom access checking.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to check access.

string $operation: The entity operation. Usually one of 'view', 'view label', 'update' or 'delete'.

\Drupal\Core\Session\AccountInterface $account: The user for which to check access.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Overrides EntityAccessControlHandler::checkAccess

File

src/EmailConfirmationAccessControlHandler.php, line 63

Class

EmailConfirmationAccessControlHandler
Access controller for the email confirmation entity.

Namespace

Drupal\email_confirmer

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {

  /** @var \Drupal\email_confirmer\EmailConfirmationInterface $entity */
  if ($account
    ->hasPermission('administer email confirmations')) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }

  // IP access restriction.
  if ($this->confirmerConfig
    ->get('restrict_same_ip') && !$entity
    ->get('ip')
    ->isEmpty() && $entity
    ->get('ip')
    ->getString() != $this->requestStack
    ->getCurrentRequest()
    ->getClientIp()) {
    return AccessResult::forbidden()
      ->addCacheContexts([
      'ip',
    ])
      ->addCacheTags($entity
      ->getCacheTags());
  }

  // Private confirmation access restriction.
  if ($entity
    ->isPrivate() && !in_array($entity
    ->get('uid')->target_id, [
    0,
    $account
      ->id(),
  ])) {
    return AccessResult::forbidden()
      ->cachePerUser()
      ->addCacheTags($entity
      ->getCacheTags());
  }
  return AccessResult::allowedIfHasPermission($account, 'access email confirmation')
    ->cachePerPermissions()
    ->addCacheTags($entity
    ->getCacheTags());
}