You are here

public static function EntityAccessHelper::nodeAccessCheck in Open Social 8.9

Same name and namespace in other branches
  1. 8 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  2. 8.2 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  3. 8.3 modules/custom/entity_access_by_field/src/EntityAccessHelper.php \Drupal\entity_access_by_field\EntityAccessHelper::nodeAccessCheck()
  4. 8.4 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 32

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') {

                // Don't look no further.
                if ($account
                  ->hasPermission('manage all groups')) {
                  return 0;
                }
                if (!$account
                  ->hasPermission('view ' . $permission_label . ' content')) {

                  // Lets verify if we are a member for flexible groups.
                  $groups = GroupContent::loadByEntity($node);
                  if (!empty($groups)) {
                    $group = reset($groups)
                      ->getGroup();
                    if ($group instanceof Group && !$group
                      ->getMember($account) && $group
                      ->getGroupType()
                      ->id() === 'flexible_group') {
                      return 1;
                    }
                  }
                  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;
}