domain_entity_type-1.0.0-rc2/modules/det_node/src/Access/NodeTypeDomainAccessCheck.php

modules/det_node/src/Access/NodeTypeDomainAccessCheck.php
<?php

namespace Drupal\det_node\Access;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\domain\DomainNegotiatorInterface;
use Drupal\domain_entity_type\Services\DomainEntityTypeManagerInterface;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeInterface;

/**
 * Node type routes domain access check.
 */
class NodeTypeDomainAccessCheck implements AccessInterface {

  /**
   * Domain negotiator service.
   *
   * @var \Drupal\domain\DomainNegotiatorInterface
   */
  protected $domainNegotiator;

  /**
   * Domain entity type manager service.
   *
   * @var \Drupal\domain_entity_type\Services\DomainEntityTypeManagerInterface
   */
  protected $domainEntityTypeManager;

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

  /**
   * Route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * {@inheritDoc}
   */
  public function __construct(
    DomainNegotiatorInterface $domainNegotiator,
    DomainEntityTypeManagerInterface $domainEntityTypeManager,
    EntityTypeManagerInterface $entityTypeManager,
    RouteMatchInterface $routeMatch,
  ) {
    $this->domainNegotiator = $domainNegotiator;
    $this->domainEntityTypeManager = $domainEntityTypeManager;
    $this->entityTypeManager = $entityTypeManager;
    $this->routeMatch = $routeMatch;
  }

  /**
   * {@inheritDoc}
   */
  public function access(AccountInterface $account) {
    // Allow access if there are no domains configured.
    if (!$this->entityTypeManager->getStorage('domain')->getQuery()->accessCheck(FALSE)->execute()) {
      return AccessResult::allowed();
    }

    // Allow access if the access check for node_type is bypassed.
    if ($this->domainEntityTypeManager->bypassAccessCheck('node_type')) {
      return AccessResult::allowed();
    }
    $params = $this->routeMatch->getParameters()->all();
    if (isset($params['node_type']) && $params['node_type'] instanceof NodeTypeInterface) {
      $domains = $params['node_type']->getThirdPartySetting('det_node', 'domains', []);
      $current_domain = $this->domainNegotiator->getActiveId();
      if (!empty($domains) && !in_array($current_domain, $domains)) {
        return AccessResult::forbidden('Node type access denied on current domain');
      }
    }
    if (isset($params['node']) && $params['node'] instanceof NodeInterface) {
      $bundle = $params['node']->bundle();
      if (!empty($bundle)) {
        $node_type = $this->entityTypeManager->getStorage('node_type')
          ->load($bundle);
        if ($node_type instanceof NodeTypeInterface) {
          $domains = $node_type->getThirdPartySetting('det_node', 'domains', []);
          $current_domain = $this->domainNegotiator->getActiveId();
          if (!empty($domains) && !in_array($current_domain, $domains)) {
            return AccessResult::forbidden('Node type access denied on current domain');
          }
        }
      }
    }
    return AccessResult::allowed();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc