You are here

abstract class AccessControlHierarchyBase in Workbench Access 8

Defines a base hierarchy class that others may extend.

Hierarchy

Expanded class hierarchy of AccessControlHierarchyBase

4 files declare their use of AccessControlHierarchyBase
DerivedAccessControlHierarchy.php in tests/modules/workbench_access_test/src/Plugin/AccessControlHierarchy/DerivedAccessControlHierarchy.php
FilterAccess.php in tests/modules/workbench_access_filter_test/src/Plugin/AccessControlHierarchy/FilterAccess.php
Menu.php in src/Plugin/AccessControlHierarchy/Menu.php
Taxonomy.php in src/Plugin/AccessControlHierarchy/Taxonomy.php

File

src/AccessControlHierarchyBase.php, line 24

Namespace

Drupal\workbench_access
View source
abstract class AccessControlHierarchyBase extends PluginBase implements AccessControlHierarchyInterface, ContainerFactoryPluginInterface {
  use PluginWithFormsTrait;
  use StringTranslationTrait;

  /**
   * A configuration factory object to store configuration.
   *
   * @var ConfigFactory
   */
  protected $configFactory;

  /**
   * The access tree array.
   *
   * @var array
   */
  protected $tree;

  /**
   * User section storage.
   *
   * @var \Drupal\workbench_access\UserSectionStorageInterface
   */
  protected $userSectionStorage;

  /**
   * Config for module.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new AccessControlHierarchyBase object.
   *
   * @param array $configuration
   *   Configuration.
   * @param string $plugin_id
   *   Plugin ID.
   * @param mixed $plugin_definition
   *   Plugin definition.
   * @param \Drupal\workbench_access\UserSectionStorageInterface $user_section_storage
   *   User section storage.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   Config factory.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, UserSectionStorageInterface $user_section_storage, ConfigFactoryInterface $configFactory, EntityTypeManagerInterface $entityTypeManager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->config = $configFactory
      ->get('workbench_access.settings');
    $this->userSectionStorage = $user_section_storage;
    $this->entityTypeManager = $entityTypeManager;
    $this
      ->setConfiguration($configuration);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('workbench_access.user_section_storage'), $container
      ->get('config.factory'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration + $this
      ->defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function id() {
    return $this->pluginDefinition['id'];
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->pluginDefinition['label'];
  }

  /**
   * Gets the entity type id controlled by the scheme.
   *
   * Note that this is an API change and not in the Interface.
   * @TODO: add to interface in version 2.0.
   *
   * @return string
   *  The entity type id of entities controlled by the scheme.
   */
  public function entityType() {
    return $this->pluginDefinition['entity'];
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getTree() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function resetTree() {
    unset($this->tree);
  }

  /**
   * {@inheritdoc}
   */
  public function load($id) {

    // This cache is specific to the object being called.
    // e.g. Drupal\workbench_access\Plugin\AccessControlHierarchy\Menu
    if (!isset($this->tree)) {
      $this->tree = $this
        ->getTree();
    }
    foreach ($this->tree as $parent => $data) {
      if (isset($data[$id])) {
        return $data[$id];
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function checkEntityAccess(AccessSchemeInterface $scheme, EntityInterface $entity, $op, AccountInterface $account) {
    if (!$this
      ->applies($entity
      ->getEntityTypeId(), $entity
      ->bundle())) {
      return AccessResult::neutral();
    }

    // Discover the field and check status.
    $entity_sections = $this
      ->getEntityValues($entity);

    // If no value is set on the entity, ignore.
    // @TODO: Is this the correct logic? It is helpful for new installs.
    $deny_on_empty = $this->config
      ->get('deny_on_empty');
    if (!$deny_on_empty && empty($entity_sections)) {
      return AccessResult::neutral();
    }
    $user_sections = $this->userSectionStorage
      ->getUserSections($scheme, $account);
    if (empty($user_sections)) {
      return AccessResult::forbidden();
    }

    // Check the tree status of the $entity against the $user.
    // Return neutral if in tree, forbidden if not.
    if (WorkbenchAccessManager::checkTree($scheme, $entity_sections, $user_sections)) {
      return AccessResult::neutral();
    }
    return AccessResult::forbidden();
  }

  /**
   * {@inheritdoc}
   */
  public function disallowedOptions($field) {
    $options = [];
    if (isset($field['widget']['#default_value']) && isset($field['widget']['#options'])) {

      // Default value may be an array or a string.
      if (is_array($field['widget']['#default_value'])) {
        $default_value_array = array_flip($field['widget']['#default_value']);
      }
      else {
        $default_value_array = [
          $field['widget']['#default_value'] => $field['widget']['#default_value'],
        ];
      }
      $options = array_diff_key($default_value_array, $field['widget']['#options']);
    }
    return array_keys($options);
  }

  /**
   * @inheritdoc
   */
  public function getApplicableFields($entity_type, $bundle) {

    // Extending classes are expected to provide their own implementation.
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public static function submitEntity(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\workbench_access\Entity\AccessSchemeInterface $access_scheme */
    foreach (\Drupal::entityTypeManager()
      ->getStorage('access_scheme')
      ->loadMultiple() as $access_scheme) {
      $scheme = $access_scheme
        ->getAccessScheme();
      $hidden_values = $form_state
        ->getValue('workbench_access_disallowed');
      if (!empty($hidden_values)) {
        $entity = $form_state
          ->getFormObject()
          ->getEntity();
        $scheme
          ->massageFormValues($entity, $form_state, $hidden_values);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getViewsJoin($entity_type, $key, $alias = NULL) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function addWhere(Section $filter, array $values) {

    // The JOIN data tells us if we have multiple tables to deal with.
    $join_data = $this
      ->getViewsJoin($filter
      ->getEntityType(), $filter->realField);
    if (count($join_data) == 1) {
      $filter->query
        ->addWhere($filter->options['group'], "{$filter->tableAlias}.{$filter->realField}", array_values($values), $filter->operator);
    }
    else {
      $or = new Condition('OR');
      foreach ($join_data as $field => $data) {
        $alias = $data['table_alias'] . '.' . $data['real_field'];
        $or
          ->condition($alias, array_values($values), $filter->operator);
      }
      $filter->query
        ->addWhere($filter->options['group'], $or);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function viewsData(array &$data, AccessSchemeInterface $scheme) {

    // Null op.
  }

  /**
   * {@inheritdoc}
   */
  public function massageFormValues(ContentEntityInterface $entity, FormStateInterface $form_state, array $hidden_values) {

    // Null op.
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Default implementation is empty.
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Default implementation is empty.
  }

  /**
   * {@inheritdoc}
   */
  public function onDependencyRemoval(array $dependencies) {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function alterForm(AccessSchemeInterface $scheme, array &$form, FormStateInterface &$form_state, ContentEntityInterface $entity) {

    // Default implementation is empty.
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessControlHierarchyBase::$config protected property Config for module.
AccessControlHierarchyBase::$configFactory protected property A configuration factory object to store configuration.
AccessControlHierarchyBase::$entityTypeManager protected property Entity type manager.
AccessControlHierarchyBase::$tree protected property The access tree array.
AccessControlHierarchyBase::$userSectionStorage protected property User section storage.
AccessControlHierarchyBase::addWhere public function Adds a where clause to a view when using a section filter. Overrides AccessControlHierarchyInterface::addWhere
AccessControlHierarchyBase::alterForm public function Alters the selection options provided for an access control field. Overrides AccessControlHierarchyInterface::alterForm 2
AccessControlHierarchyBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 2
AccessControlHierarchyBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 2
AccessControlHierarchyBase::checkEntityAccess public function Responds to request for node access. Overrides AccessControlHierarchyInterface::checkEntityAccess
AccessControlHierarchyBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 3
AccessControlHierarchyBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 2
AccessControlHierarchyBase::disallowedOptions public function Gets any options that are set but cannot be changed by the editor. Overrides AccessControlHierarchyInterface::disallowedOptions 1
AccessControlHierarchyBase::entityType public function Gets the entity type id controlled by the scheme.
AccessControlHierarchyBase::getApplicableFields public function @inheritdoc Overrides AccessControlHierarchyInterface::getApplicableFields 1
AccessControlHierarchyBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
AccessControlHierarchyBase::getTree public function Gets the entire hierarchy tree. Overrides AccessControlHierarchyInterface::getTree 3
AccessControlHierarchyBase::getViewsJoin public function Returns information on how to join this section data to a base view table. Overrides AccessControlHierarchyInterface::getViewsJoin 2
AccessControlHierarchyBase::id public function Returns the id for a hierarchy. Overrides AccessControlHierarchyInterface::id
AccessControlHierarchyBase::label public function Returns the label for a hierarchy. Overrides AccessControlHierarchyInterface::label
AccessControlHierarchyBase::load public function Loads a hierarchy definition for a single item in the tree. Overrides AccessControlHierarchyInterface::load
AccessControlHierarchyBase::massageFormValues public function Massage form values as appropriate during entity submit. Overrides AccessControlHierarchyInterface::massageFormValues 1
AccessControlHierarchyBase::onDependencyRemoval public function Informs the plugin that a dependency of the scheme will be deleted. Overrides AccessControlHierarchyInterface::onDependencyRemoval 2
AccessControlHierarchyBase::resetTree public function Resets the internal cache of the tree. Overrides AccessControlHierarchyInterface::resetTree
AccessControlHierarchyBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
AccessControlHierarchyBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 2
AccessControlHierarchyBase::submitEntity public static function Responds to the submission of an entity form. Overrides AccessControlHierarchyInterface::submitEntity
AccessControlHierarchyBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
AccessControlHierarchyBase::viewsData public function Adds views data for the plugin. Overrides AccessControlHierarchyInterface::viewsData 2
AccessControlHierarchyBase::__construct public function Constructs a new AccessControlHierarchyBase object. Overrides PluginBase::__construct 1
AccessControlHierarchyInterface::applies public function Check if this access scheme applies to the given entity. 4
AccessControlHierarchyInterface::getEntityValues public function Retrieves the access control values from an entity. 4
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.