You are here

class CRMCoreAccess in CRM Core 8

Same name and namespace in other branches
  1. 8.3 src/Access/CRMCoreAccess.php \Drupal\crm_core\Access\CRMCoreAccess
  2. 8.2 src/Access/CRMCoreAccess.php \Drupal\crm_core\Access\CRMCoreAccess

Check access for crm_core.

Hierarchy

Expanded class hierarchy of CRMCoreAccess

File

src/Access/CRMCoreAccess.php, line 19

Namespace

Drupal\crm_core\Access
View source
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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CRMCoreAccess::$accessManager protected property The access manager.
CRMCoreAccess::$menuTree protected property The menu link tree manager.
CRMCoreAccess::access public function Checks access for CRM Core overview.
CRMCoreAccess::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
CRMCoreAccess::__construct public function CRMCoreAccess constructor.