MasqueradeBlock.php in Masquerade 8.2
File
src/Plugin/Block/MasqueradeBlock.php
View source
<?php
namespace Drupal\masquerade\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\masquerade\Form\MasqueradeForm;
use Drupal\masquerade\Masquerade;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MasqueradeBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $formBuilder;
protected $masquerade;
public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder, Masquerade $masquerade) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->formBuilder = $form_builder;
$this->masquerade = $masquerade;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('form_builder'), $container
->get('masquerade'));
}
protected function blockAccess(AccountInterface $account) {
if ($account
->isAnonymous()) {
return AccessResult::forbidden();
}
if ($this->masquerade
->isMasquerading()) {
return AccessResult::forbidden()
->addCacheContexts([
'session.is_masquerading',
]);
}
return AccessResult::allowedIfHasPermissions($account, $this->masquerade
->getPermissions(), 'OR')
->addCacheContexts([
'session.is_masquerading',
]);
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'session.is_masquerading',
]);
}
public function build() {
return $this->formBuilder
->getForm(MasqueradeForm::class);
}
}