You are here

public function AccessCheck::canUserAccessByNode in Permissions by Term 8.2

2 calls to AccessCheck::canUserAccessByNode()
AccessCheck::dispatchDeniedEventOnRestricedAccess in src/Service/AccessCheck.php
AccessCheck::handleNode in src/Service/AccessCheck.php

File

src/Service/AccessCheck.php, line 48

Class

AccessCheck
AccessCheckService class.

Namespace

Drupal\permissions_by_term\Service

Code

public function canUserAccessByNode(Node $node, $uid = FALSE, $langcode = '') : bool {
  if (!$this
    ->isAnyTaxonomyTermFieldDefinedInNodeType($node
    ->getType())) {
    return TRUE;
  }
  $langcode = $langcode === '' ? \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId() : $langcode;
  if (empty($uid)) {
    $uid = \Drupal::currentUser()
      ->id();
  }
  $user = User::load($uid);
  if ($user instanceof User && $user
    ->hasPermission('view own unpublished content') && (int) $node
    ->getOwnerId() === (int) $uid && !$node
    ->isPublished()) {
    return TRUE;
  }
  if ($user instanceof User && $user
    ->hasPermission('view any unpublished content') && !$node
    ->isPublished()) {
    return TRUE;
  }
  if ($user instanceof User && $user
    ->hasPermission('bypass node access')) {
    return TRUE;
  }
  $configPermissionMode = \Drupal::config('permissions_by_term.settings')
    ->get('permission_mode');
  $requireAllTermsGranted = \Drupal::config('permissions_by_term.settings')
    ->get('require_all_terms_granted');
  if (!$configPermissionMode && !$requireAllTermsGranted) {
    $access_allowed = TRUE;
  }
  else {
    $access_allowed = FALSE;
  }
  $terms = $this->database
    ->query("SELECT tid FROM {taxonomy_index} WHERE nid = :nid", [
    ':nid' => $node
      ->id(),
  ])
    ->fetchAll();
  if (empty($terms) && !$configPermissionMode) {
    return TRUE;
  }
  foreach ($terms as $term) {
    $termInfo = Term::load($term->tid);
    if ($termInfo instanceof Term && $termInfo
      ->get('langcode')
      ->getLangcode() == $langcode) {
      if (!$this
        ->isAnyPermissionSetForTerm($term->tid, $termInfo
        ->get('langcode')
        ->getLangcode())) {
        continue;
      }
      $access_allowed = $this
        ->isAccessAllowedByDatabase($term->tid, $uid, $termInfo
        ->get('langcode')
        ->getLangcode());
      if (!$access_allowed && $requireAllTermsGranted) {
        return $access_allowed;
      }
      if ($access_allowed && !$requireAllTermsGranted) {
        return $access_allowed;
      }
    }
  }
  return $access_allowed;
}