BlockPermissionsAccessControlHandler.php in Block permissions 8
File
src/BlockPermissionsAccessControlHandler.php
View source
<?php
namespace Drupal\block_permissions;
use Drupal\block\Entity\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlockPermissionsAccessControlHandler implements ContainerInjectionInterface {
protected $blockManager;
protected $currentUser;
protected $configFactory;
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.block'), $container
->get('current_user'), $container
->get('config.factory'));
}
public function __construct(BlockManagerInterface $block_manager, AccountInterface $current_user, ConfigFactoryInterface $config_factory) {
$this->blockManager = $block_manager;
$this->currentUser = $current_user;
$this->configFactory = $config_factory;
}
public function blockListAccess() {
$theme = $this->configFactory
->get('system.theme')
->get('default');
$access = AccessResult::allowedIfHasPermission($this->currentUser, 'administer block settings for theme ' . $theme);
return $access;
}
public function blockThemeListAccess($theme) {
$access = AccessResult::allowedIfHasPermission($this->currentUser, 'administer block settings for theme ' . $theme);
return $access;
}
public function blockAddFormAccess($plugin_id, $theme) {
$plugin = $this->blockManager
->getDefinition($plugin_id);
$access = AccessResult::allowedIfHasPermissions($this->currentUser, [
'administer blocks provided by ' . $plugin['provider'],
'administer block settings for theme ' . $theme,
]);
return $access;
}
public function blockFormAccess(Block $block) {
$plugin = $block
->getPlugin();
$configuration = $plugin
->getConfiguration();
$access = AccessResult::allowedIfHasPermission($this->currentUser, 'administer blocks provided by ' . $configuration['provider']);
return $access;
}
}