You are here

public function NodeOptionPremiumHelper::hasFullAccess in Node Option Premium 8

Checks if the given user has full access to the given entity.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to check for premium access.

\Drupal\Core\Session\AccountInterface $account: The operating account.

Overrides NodeOptionPremiumHelperInterface::hasFullAccess

File

src/NodeOptionPremiumHelper.php, line 17

Class

NodeOptionPremiumHelper
API and helper methods.

Namespace

Drupal\nopremium

Code

public function hasFullAccess(ContentEntityInterface $entity, AccountInterface $account) {
  if (!$entity
    ->hasField('premium')) {

    // Entity has no premium field, so no restricted access.
    return TRUE;
  }
  if (empty($entity->premium->value)) {

    // This is not a premium entity. Full access granted.
    return TRUE;
  }

  // Check permissions.
  if ($account
    ->hasPermission('administer nodes') || $account
    ->hasPermission('view full premium content of any type') || $account
    ->hasPermission('view full ' . $entity
    ->bundle() . ' premium content') || $entity
    ->access('update', $account)) {
    return TRUE;
  }

  // Check if the account owns the entity.
  if ($entity instanceof EntityOwnerInterface && $account
    ->isAuthenticated() && $account
    ->id() == $entity
    ->getOwnerId()) {
    return TRUE;
  }

  // In all other cases, the user hasn't full access to the entity.
  return FALSE;
}