You are here

class BlockPermissionsAccessControlHandler in Block permissions 8

Controller for the block permissions.

Hierarchy

Expanded class hierarchy of BlockPermissionsAccessControlHandler

File

src/BlockPermissionsAccessControlHandler.php, line 16

Namespace

Drupal\block_permissions
View source
class BlockPermissionsAccessControlHandler implements ContainerInjectionInterface {

  /**
   * The condition plugin manager.
   *
   * @var \Drupal\Core\Block\BlockManagerInterface
   */
  protected $blockManager;

  /**
   * The current user service.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.block'), $container
      ->get('current_user'), $container
      ->get('config.factory'));
  }

  /**
   * Constructs the block access control handler instance.
   *
   * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
   *   Plugin manager.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   Current user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   */
  public function __construct(BlockManagerInterface $block_manager, AccountInterface $current_user, ConfigFactoryInterface $config_factory) {
    $this->blockManager = $block_manager;
    $this->currentUser = $current_user;
    $this->configFactory = $config_factory;
  }

  /**
   * Access check for the default block list manage page.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   AccessResult object.
   */
  public function blockListAccess() {
    $theme = $this->configFactory
      ->get('system.theme')
      ->get('default');

    // Check if the user has the proper permissions.
    $access = AccessResult::allowedIfHasPermission($this->currentUser, 'administer block settings for theme ' . $theme);
    return $access;
  }

  /**
   * Access check for the block list for specific themes.
   *
   * @param string $theme
   *   The theme name.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   An access result.
   */
  public function blockThemeListAccess($theme) {

    // Check if the user has the proper permissions.
    $access = AccessResult::allowedIfHasPermission($this->currentUser, 'administer block settings for theme ' . $theme);
    return $access;
  }

  /**
   * Access check for the add block form.
   *
   * @param string $plugin_id
   *   The plugin name.
   * @param string $theme
   *   The theme name.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   An access result.
   */
  public function blockAddFormAccess($plugin_id, $theme) {
    $plugin = $this->blockManager
      ->getDefinition($plugin_id);

    // Check if the user has the proper permissions.
    $access = AccessResult::allowedIfHasPermissions($this->currentUser, [
      'administer blocks provided by ' . $plugin['provider'],
      'administer block settings for theme ' . $theme,
    ]);
    return $access;
  }

  /**
   * Access check for the block config form.
   *
   * @param \Drupal\block\Entity\Block $block
   *   The theme name.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   An access result.
   */
  public function blockFormAccess(Block $block) {
    $plugin = $block
      ->getPlugin();
    $configuration = $plugin
      ->getConfiguration();

    // Check if the user has the proper permissions.
    $access = AccessResult::allowedIfHasPermission($this->currentUser, 'administer blocks provided by ' . $configuration['provider']);
    return $access;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPermissionsAccessControlHandler::$blockManager protected property The condition plugin manager.
BlockPermissionsAccessControlHandler::$configFactory protected property The config factory service.
BlockPermissionsAccessControlHandler::$currentUser protected property The current user service.
BlockPermissionsAccessControlHandler::blockAddFormAccess public function Access check for the add block form.
BlockPermissionsAccessControlHandler::blockFormAccess public function Access check for the block config form.
BlockPermissionsAccessControlHandler::blockListAccess public function Access check for the default block list manage page.
BlockPermissionsAccessControlHandler::blockThemeListAccess public function Access check for the block list for specific themes.
BlockPermissionsAccessControlHandler::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
BlockPermissionsAccessControlHandler::__construct public function Constructs the block access control handler instance.