You are here

class ReplicationAccessControlHandler in Deploy - Content Staging 8

ReplicationAccessControlHandler class.

Hierarchy

Expanded class hierarchy of ReplicationAccessControlHandler

File

src/ReplicationAccessControlHandler.php, line 24

Namespace

Drupal\deploy
View source
class ReplicationAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
  use MessengerTrait;

  /**
   * The workspace manager service.
   *
   * @var \Drupal\multiversion\Workspace\WorkspaceManagerInterface
   */
  protected $workspaceManager;

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

  /**
   * Constructs a NodeAccessControlHandler object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\multiversion\Workspace\WorkspaceManagerInterface $workspace_manager
   *   The workspace manager service.
   */
  public function __construct(EntityTypeInterface $entity_type, WorkspaceManagerInterface $workspace_manager, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($entity_type);
    $this->workspaceManager = $workspace_manager;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static($entity_type, $container
      ->get('workspace.manager'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
    $restricted_fields = [
      'source',
      'target',
    ];
    if (in_array($field_definition
      ->getName(), $restricted_fields)) {
      return AccessResult::forbidden();
    }
    return parent::checkFieldAccess($operation, $field_definition, $account, $items);
  }

  /**
   * {@inheritdoc}
   */
  protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
    $access = parent::checkCreateAccess($account, $context, $entity_bundle);
    $active_workspace = $this->workspaceManager
      ->getActiveWorkspace();
    $upstream_workspace_pointer = $active_workspace->upstream->entity;

    // When no upstream workspace pointer is set the access is forbidden.
    if (!$upstream_workspace_pointer) {
      return AccessResult::forbidden('No target is set for the active workspace.');
    }
    if (\Drupal::state()
      ->get('workspace.last_replication_failed', FALSE)) {
      return AccessResult::forbidden('Replication is blocked.');
    }
    $replication_in_queue = $this->entityTypeManager
      ->getStorage('replication')
      ->getQuery()
      ->condition('source', WorkspacePointer::loadFromWorkspace($active_workspace)
      ->id())
      ->condition('target', $upstream_workspace_pointer
      ->id())
      ->condition('replication_status', [
      Replication::QUEUED,
      Replication::REPLICATING,
    ], 'IN')
      ->execute();
    if (!empty($replication_in_queue)) {
      $this
        ->messenger()
        ->addWarning(t('Users are only allowed to create one push and one pull deployment between the same source and target workspace. New deployments are only allowed after the currently queued deployment finish.'));
      return AccessResult::forbidden('Replication queued or in progress.');
    }

    // The 'deploy to any workspace' permission will always allow the user to
    // create replication entities and perform deployments.
    if ($account
      ->hasPermission('deploy to any workspace')) {
      return AccessResult::allowed();
    }

    // Load just the ID and workspace separately to allow for remote workspace
    // pointers which won't have the workspace_pointer field set.
    $upstream_workspace_id = $upstream_workspace_pointer->workspace_pointer->target_id;
    $upstream_workspace = Workspace::load($upstream_workspace_id);

    // When the upstream workspace is set, the owner matches the account, and
    // the user has the correct permission then allow access.
    if ($upstream_workspace && $upstream_workspace
      ->getOwnerId() == $account
      ->id() && $account
      ->hasPermission('deploy to own workspace')) {
      return AccessResult::allowed();
    }

    // When the user doesn't have permissions to deploy to the upstream the
    // access is forbidden.
    if (!$account
      ->hasPermission('Deploy to ' . $upstream_workspace_pointer
      ->label())) {
      return AccessResult::forbidden('You do not have permission to deploy to the target.');
    }
    return $access;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityAccessControlHandler::$accessCache protected property Stores calculated access check results.
EntityAccessControlHandler::$entityType protected property Information about the entity type.
EntityAccessControlHandler::$entityTypeId protected property The entity type ID of the access control handler instance.
EntityAccessControlHandler::$viewLabelOperation protected property Allows to grant access to just the labels. 5
EntityAccessControlHandler::access public function Checks access to an operation on a given entity or entity translation. Overrides EntityAccessControlHandlerInterface::access 1
EntityAccessControlHandler::checkAccess protected function Performs access checks. 33
EntityAccessControlHandler::createAccess public function Checks access to create an entity. Overrides EntityAccessControlHandlerInterface::createAccess 1
EntityAccessControlHandler::fieldAccess public function Checks access to an operation on a given entity field. Overrides EntityAccessControlHandlerInterface::fieldAccess
EntityAccessControlHandler::getCache protected function Tries to retrieve a previously cached access value from the static cache.
EntityAccessControlHandler::prepareUser protected function Loads the current account object, if it does not exist yet.
EntityAccessControlHandler::processAccessHookResults protected function We grant access to the entity if both of these conditions are met:
EntityAccessControlHandler::resetCache public function Clears all cached access checks. Overrides EntityAccessControlHandlerInterface::resetCache
EntityAccessControlHandler::setCache protected function Statically caches whether the given user has access.
EntityHandlerBase::$moduleHandler protected property The module handler to invoke hooks on. 2
EntityHandlerBase::moduleHandler protected function Gets the module handler. 2
EntityHandlerBase::setModuleHandler public function Sets the module handler for this handler.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
ReplicationAccessControlHandler::$entityTypeManager protected property The entity type manager service.
ReplicationAccessControlHandler::$workspaceManager protected property The workspace manager service.
ReplicationAccessControlHandler::checkCreateAccess protected function Performs create access checks. Overrides EntityAccessControlHandler::checkCreateAccess
ReplicationAccessControlHandler::checkFieldAccess protected function Default field access as determined by this access control handler. Overrides EntityAccessControlHandler::checkFieldAccess
ReplicationAccessControlHandler::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
ReplicationAccessControlHandler::__construct public function Constructs a NodeAccessControlHandler object. Overrides EntityAccessControlHandler::__construct
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.