You are here

class NodeOptionPremiumHelper in Node Option Premium 8

API and helper methods.

Hierarchy

Expanded class hierarchy of NodeOptionPremiumHelper

1 file declares its use of NodeOptionPremiumHelper
NodeOptionPremiumHelperTest.php in tests/src/Unit/NodeOptionPremiumHelperTest.php
1 string reference to 'NodeOptionPremiumHelper'
nopremium.services.yml in ./nopremium.services.yml
nopremium.services.yml
1 service uses NodeOptionPremiumHelper
nopremium.helper in ./nopremium.services.yml
Drupal\nopremium\NodeOptionPremiumHelper

File

src/NodeOptionPremiumHelper.php, line 12

Namespace

Drupal\nopremium
View source
class NodeOptionPremiumHelper implements NodeOptionPremiumHelperInterface {

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NodeOptionPremiumHelper::hasFullAccess public function Checks if the given user has full access to the given entity. Overrides NodeOptionPremiumHelperInterface::hasFullAccess