You are here

protected function SubscriberAccessControlHandler::checkAccess in Simplenews 8

Same name and namespace in other branches
  1. 8.2 src/SubscriberAccessControlHandler.php \Drupal\simplenews\SubscriberAccessControlHandler::checkAccess()
  2. 3.x src/SubscriberAccessControlHandler.php \Drupal\simplenews\SubscriberAccessControlHandler::checkAccess()

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/SubscriberAccessControlHandler.php, line 22

Class

SubscriberAccessControlHandler
Defines the access control handler for the simplenews subscriber entity type.

Namespace

Drupal\simplenews

Code

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

  // Administrators can view/update/delete all subscribers.
  if ($account
    ->hasPermission('administer simplenews subscriptions')) {
    return AccessResult::allowed()
      ->cachePerPermissions();
  }
  if ($operation != 'delete' && $entity
    ->getUserId()) {

    // For a subscription that corresponds to a user, access to view/update
    // is allowed for that user if they have permission. Don't allow users to
    // delete the subscription entirely, as we need to keep a record of the
    // subscription history.
    return AccessResult::allowedIf($entity
      ->getUserId() == $account
      ->id())
      ->andIf(AccessResult::allowedIfHasPermission($account, 'subscribe to newsletters'))
      ->addCacheableDependency($entity);
  }

  // Allow access to view subscribers based on the related permission.
  if ($operation == 'view') {
    return AccessResult::allowedIfHasPermission($account, 'view simplenews subscriptions');
  }

  // No opinion.
  return AccessResult::neutral();
}