ViewAccessCheck.php in Node Accessibility 8
File
src/Access/ViewAccessCheck.php
View source
<?php
namespace Drupal\node_accessibility\Access;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
use Symfony\Component\HttpFoundation\ParameterBag;
use Drupal\node_accessibility\TypeSettingsStorage;
class ViewAccessCheck implements AccessInterface {
public static function check_node_access(AccountInterface $account, $nid, $vid = NULL) {
if (!$account
->hasPermission('perform node accessibility validation')) {
return FALSE;
}
if ($nid == 0) {
return FALSE;
}
$settings = TypeSettingsStorage::loadByNodeAsArray($nid);
if (is_numeric($vid)) {
if (!static::nodeRevisionIsValid($nid, (int) $vid)) {
return FALSE;
}
}
if (empty($settings['node_type'])) {
return FALSE;
}
if (empty($settings['enabled']) || $settings['enabled'] == 'disabled') {
return FALSE;
}
return TRUE;
}
public function access(AccountInterface $account, RouteMatchInterface $route_match) {
if (!$account
->hasPermission('perform node accessibility validation')) {
return AccessResult::forbidden();
}
$bag = $route_match
->getParameters();
$nid = (int) $bag
->get('node');
$vid = $bag
->get('node_revision');
unset($bag);
if ($nid == 0) {
return AccessResult::forbidden();
}
$settings = TypeSettingsStorage::loadByNodeAsArray($nid);
if (is_numeric($vid)) {
if (!$this
->nodeRevisionIsValid($nid, (int) $vid)) {
return AccessResult::forbidden();
}
}
if (empty($settings['node_type'])) {
return AccessResult::forbidden();
}
if (empty($settings['enabled']) || $settings['enabled'] == 'disabled') {
return AccessResult::forbidden();
}
return AccessResult::allowed();
}
private static function nodeRevisionIsValid($nid, $vid) {
$node_type = NULL;
try {
$query = \Drupal::database()
->select('node', 'n');
$query
->innerJoin('node_revision', 'nr', 'n.nid = nr.nid');
$query
->addField('n', 'nid', 'nid');
$query
->addField('nr', 'vid', 'vid');
$query
->condition('n.nid', $nid);
$query
->condition('nr.vid', $vid);
$existing = $query
->execute()
->fetchObject();
if ($existing && $existing->nid == $nid) {
return TRUE;
}
} catch (Exception $e) {
\Drupal::logger('node_accessibility')
->error("Failed to select from {node} table or {node_revision} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
} catch (Error $e) {
\Drupal::logger('node_accessibility')
->error("Failed to select from {node} table or {node_revision} table, exception: @exception.", [
'@exception' => $e
->getMessage(),
]);
}
return FALSE;
}
}