You are here

CRMCoreAccess.php in CRM Core 8

Same filename and directory in other branches
  1. 8.3 src/Access/CRMCoreAccess.php
  2. 8.2 src/Access/CRMCoreAccess.php

File

src/Access/CRMCoreAccess.php
View source
<?php

namespace Drupal\crm_core\Access;

use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Routing\Access\AccessInterface as RoutingAccessInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;

/**
 * Check access for crm_core.
 */
class CRMCoreAccess implements RoutingAccessInterface, ContainerInjectionInterface {

  /**
   * The menu link tree manager.
   *
   * @var \Drupal\Core\Menu\MenuLinkTreeInterface
   */
  protected $menuTree;

  /**
   * The access manager.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface
   */
  protected $accessManager;

  /**
   * CRMCoreAccess constructor.
   *
   * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
   *   The menu link tree manager.
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
   *   The access manager.
   */
  public function __construct(MenuLinkTreeInterface $menu_tree, AccessManagerInterface $access_manager) {
    $this->menuTree = $menu_tree;
    $this->accessManager = $access_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('menu.link_tree'), $container
      ->get('access_manager'));
  }

  /**
   * Checks access for CRM Core overview.
   *
   * @param \Symfony\Component\Routing\Route $route
   *   The route to check against.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account being checked.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   The access result.
   */
  public function access(Route $route, AccountInterface $account) {
    $path = $route
      ->getPath();
    $route = Url::fromUri('internal:' . $path);
    $parameters = new MenuTreeParameters();
    $parameters
      ->setRoot($route
      ->getRouteName())
      ->excludeRoot()
      ->setTopLevelOnly()
      ->onlyEnabledLinks();
    $tree = $this->menuTree
      ->load(NULL, $parameters);
    $manipulators = [
      [
        'callable' => 'menu.default_tree_manipulators:checkAccess',
      ],
      [
        'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
      ],
    ];
    $tree = $this->menuTree
      ->transform($tree, $manipulators);
    foreach ($tree as $element) {
      $route_name = $element->link
        ->getPluginId();
      if ($this->accessManager
        ->checkNamedRoute($route_name, [], $account)) {
        return AccessResult::allowed();
      }
    }
    return AccessResult::forbidden();
  }

}

Classes

Namesort descending Description
CRMCoreAccess Check access for crm_core.