You are here

public function AccessCheck::canUserAccessByNodeId in Permissions by Term 8

Parameters

int $nid:

bool $uid:

string $langcode:

Return value

array|bool

1 call to AccessCheck::canUserAccessByNodeId()
AccessCheck::handleNode in src/Service/AccessCheck.php

File

src/Service/AccessCheck.php, line 47

Class

AccessCheck
AccessCheckService class.

Namespace

Drupal\permissions_by_term\Service

Code

public function canUserAccessByNodeId($nid, $uid = FALSE, $langcode = '') {
  $langcode = $langcode === '' ? \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId() : $langcode;
  if (\Drupal::currentUser()
    ->hasPermission('bypass node access')) {
    return TRUE;
  }
  if (!($singleTermRestriction = \Drupal::config('permissions_by_term.settings.single_term_restriction')
    ->get('value'))) {
    $access_allowed = TRUE;
  }
  else {
    $access_allowed = FALSE;
  }
  $terms = $this->database
    ->query("SELECT tid FROM {taxonomy_index} WHERE nid = :nid", [
    ':nid' => $nid,
  ])
    ->fetchAll();
  if (empty($terms)) {
    return TRUE;
  }
  foreach ($terms as $term) {
    $termInfo = Term::load($term->tid);
    if ($termInfo instanceof Term && $termInfo
      ->get('langcode')
      ->getLangcode() == $langcode) {
      $access_allowed = $this
        ->isAccessAllowedByDatabase($term->tid, $uid, $termInfo
        ->get('langcode')
        ->getLangcode());
      if (!$access_allowed) {
        if ($singleTermRestriction) {
          return $access_allowed;
        }
      }
      if ($access_allowed && !$singleTermRestriction) {
        return $access_allowed;
      }
    }
  }
  return $access_allowed;
}