You are here

public static function EntityAccessHelper::nodeAccessCheck in Open Social 8.4

Same name and namespace in other branches
  1. 8.9 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  2. 8 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  3. 8.2 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  4. 8.3 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  5. 8.5 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  6. 8.6 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  7. 8.7 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  8. 8.8 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  9. 10.3.x modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  10. 10.0.x modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  11. 10.1.x modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  12. 10.2.x modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()

NodeAccessCheck for given operation, node and user account.

5 calls to EntityAccessHelper::nodeAccessCheck()
EntityAccessHelper::getEntityAccessResult in modules/custom/entity_access_by_field/src/EntityAccessHelper.php
Gets the Entity access for the given node.
EntityAccessTest::testAllowedAccess in modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php
Tests the EntityAccessHelper::nodeAccessCheck for Allowed Access.
EntityAccessTest::testAuthorAccessAllowed in modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php
Tests the EntityAccessHelper::nodeAccessCheck for Author Access Allowed.
EntityAccessTest::testForbiddenAccess in modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php
Tests the EntityAccessHelper::nodeAccessCheck for Forbidden Access.
EntityAccessTest::testNeutralAccess in modules/custom/entity_access_by_field/tests/src/Unit/EntityAccessTest.php
Tests the EntityAccessHelper::nodeAccessCheck for Neutral Access.

File

modules/custom/entity_access_by_field/src/EntityAccessHelper.php, line 29

Class

EntityAccessHelper
Helper class for checking entity access.

Namespace

Drupal\entity_access_by_field

Code

public static function nodeAccessCheck(NodeInterface $node, $op, AccountInterface $account) {
  if ($op === 'view') {

    // Check published status.
    if (isset($node->status) && $node->status->value == NODE_NOT_PUBLISHED) {
      $unpublished_own = $account
        ->hasPermission('view own unpublished content');
      if ($node
        ->getOwnerId() !== $account
        ->id() || $node
        ->getOwnerId() === $account
        ->id() && !$unpublished_own) {
        return 1;
      }
    }
    $field_definitions = $node
      ->getFieldDefinitions();

    /* @var \Drupal\Core\Field\FieldConfigInterface $field_definition */
    foreach ($field_definitions as $field_name => $field_definition) {
      if ($field_definition
        ->getType() === 'entity_access_field') {
        $field_values = $node
          ->get($field_name)
          ->getValue();
        if (!empty($field_values)) {
          foreach ($field_values as $field_value) {
            if (isset($field_value['value'])) {
              if (in_array($field_value['value'], EntityAccessHelper::getIgnoredValues())) {
                return 0;
              }
              $permission_label = $field_definition
                ->id() . ':' . $field_value['value'];

              // When content is posted in a group and the account does not
              // have permission we return Access::ignore.
              if ($field_value['value'] === 'group') {
                if (!$account
                  ->hasPermission('view ' . $permission_label . ' content')) {
                  return 0;
                }
              }
              if ($account
                ->hasPermission('view ' . $permission_label . ' content')) {
                return 2;
              }
              if ($account
                ->id() !== 0 && $account
                ->id() === $node
                ->getOwnerId()) {
                return 2;
              }
            }
          }
        }
        $access = FALSE;
      }
    }
    if (isset($access) && $access === FALSE) {
      return 1;
    }
  }
  return 0;
}