WorkbenchAccessManager.php in Workbench Access 8
File
src/WorkbenchAccessManager.php
View source
<?php
namespace Drupal\workbench_access;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
class WorkbenchAccessManager extends DefaultPluginManager implements WorkbenchAccessManagerInterface {
use StringTranslationTrait;
public static $tree;
protected $entityTypeManager;
protected $userSectionStorage;
protected $configFactory;
protected $currentUser;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entityTypeManager, UserSectionStorageInterface $user_section_storage, ConfigFactoryInterface $configFactory, AccountInterface $currentUser) {
parent::__construct('Plugin/AccessControlHierarchy', $namespaces, $module_handler, 'Drupal\\workbench_access\\AccessControlHierarchyInterface', 'Drupal\\workbench_access\\Annotation\\AccessControlHierarchy');
$this
->alterInfo('workbench_access_info');
$this
->setCacheBackend($cache_backend, 'workbench_access_plugins');
$this->moduleHandler = $module_handler;
$this->namespaces = $namespaces;
$this->userSectionStorage = $user_section_storage;
$this->configFactory = $configFactory;
$this->currentUser = $currentUser;
$this->entityTypeManager = $entityTypeManager;
}
public static function checkTree(AccessSchemeInterface $scheme, array $entity_sections, array $user_sections) {
$list = array_flip($user_sections);
foreach ($entity_sections as $section) {
if (isset($list[$section])) {
return TRUE;
}
if (!isset(self::$tree[$scheme
->id()])) {
self::$tree[$scheme
->id()] = $scheme
->getAccessScheme()
->getTree();
}
foreach (self::$tree[$scheme
->id()] as $id => $info) {
if (isset($list[$section]) && isset($info[$section])) {
return TRUE;
}
if (!empty($info[$section]['parents'])) {
$parents = array_flip($info[$section]['parents']);
foreach ($list as $uid => $data) {
if (isset($parents[$uid])) {
return TRUE;
}
}
}
}
}
return FALSE;
}
public static function getAllSections(AccessSchemeInterface $scheme, $root_only = FALSE) {
$sections = [];
foreach ($scheme
->getAccessScheme()
->getTree() as $root => $item) {
if ($root_only) {
$sections[] = $root;
}
else {
foreach ($item as $id => $data) {
$sections[] = $id;
}
}
}
return $sections;
}
public function userInAll(AccessSchemeInterface $scheme, AccountInterface $account = NULL) {
if (!$account) {
$account = $this->currentUser;
}
if ($account
->hasPermission('bypass workbench access')) {
return TRUE;
}
else {
$user_sections = $this->userSectionStorage
->getUserSections($scheme, $account);
foreach (array_keys($scheme
->getAccessScheme()
->getTree()) as $root) {
if (!in_array($root, $user_sections)) {
return FALSE;
}
}
}
return TRUE;
}
}