CRMCoreAccess.php in CRM Core 8
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;
class CRMCoreAccess implements RoutingAccessInterface, ContainerInjectionInterface {
protected $menuTree;
protected $accessManager;
public function __construct(MenuLinkTreeInterface $menu_tree, AccessManagerInterface $access_manager) {
$this->menuTree = $menu_tree;
$this->accessManager = $access_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('menu.link_tree'), $container
->get('access_manager'));
}
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();
}
}