TcaAccessCheck.php in Token Content Access 8
File
src/Access/TcaAccessCheck.php
View source
<?php
namespace Drupal\tca\Access;
use Drupal\tca\Plugin\TcaPluginManager;
use Drupal\tca\TcaSettingsManager;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
class TcaAccessCheck implements AccessInterface {
protected $entityTypeManager = NULL;
protected $tcaPluginManager = NULL;
protected $tcaSettingsManager = NULL;
public function __construct(EntityTypeManagerInterface $entity_type_manager, TcaPluginManager $tca_plugin_manager, TcaSettingsManager $tca_settings_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->tcaPluginManager = $tca_plugin_manager;
$this->tcaSettingsManager = $tca_settings_manager;
}
public function access(EntityInterface $entity, $user_token, AccountInterface $account = NULL) {
$neutral = AccessResult::neutral()
->addCacheableDependency($entity)
->addCacheContexts([
'url.path',
]);
$entity_type_id = $entity
->getEntityTypeId();
$entity_id = $entity
->id();
$affected_types = $this->tcaPluginManager
->loadSupportedEntityTypes();
$affected_bundle_types = $this->tcaPluginManager
->loadSupportedBundleEntityTypes();
if (!$account) {
$account = \Drupal::currentUser();
}
$bypass_permitted = $account
->hasPermission('tca bypass ' . $entity_type_id);
if ($bypass_permitted || !in_array($entity_type_id, $affected_types) && !in_array($entity_type_id, $affected_bundle_types)) {
return $neutral;
}
$entity_type = $this->entityTypeManager
->getStorage($entity_type_id)
->getEntityType();
$is_entity_bundle = $this
->isEntityBundle($entity);
$bundle = $entity
->bundle();
$tca_bundle_settings = NULL;
$tca_settings = NULL;
$active = NULL;
$token = NULL;
if ($is_entity_bundle) {
$tca_settings = $this->tcaSettingsManager
->loadSettingsAsConfig($entity_type_id, $entity_id);
$active = $tca_settings
->get('active');
$token = $tca_settings
->get('token');
$public = $tca_settings
->get('public');
}
else {
$bundle_entity_type_id = $entity_type
->getBundleEntityType() ?: $entity_type_id;
$bundle_entity_id = $entity
->getEntityType()
->getBundleEntityType() ? $entity
->bundle() : NULL;
$tca_bundle_settings = $this->tcaSettingsManager
->loadSettingsAsConfig($bundle_entity_type_id, $bundle_entity_id);
if (!$tca_bundle_settings
->get('active')) {
return $neutral;
}
$tca_settings = $this->tcaSettingsManager
->loadSettingsAsConfig($entity_type_id, $entity_id);
$active = $tca_settings
->get('active');
$token = $tca_settings
->get('token');
$public = $tca_settings
->get('public');
}
if (!$active) {
return $neutral;
}
if (!$user_token || $token != $user_token) {
return AccessResult::forbidden()
->addCacheableDependency($entity)
->addCacheContexts([
'url.path',
]);
}
elseif ($public && $token == $user_token) {
return AccessResult::allowed()
->addCacheableDependency($entity)
->addCacheContexts([
'url.path',
]);
}
return $neutral;
}
protected function isEntityBundle($entity) {
return is_subclass_of($entity, 'Drupal\\Core\\Config\\Entity\\ConfigEntityBundleBase');
}
}