ModerationDashboardAccess.php in Moderation Dashboard 8
File
src/Plugin/Condition/ModerationDashboardAccess.php
View source
<?php
namespace Drupal\moderation_dashboard\Plugin\Condition;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\user\UserStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModerationDashboardAccess extends ConditionPluginBase implements ContainerFactoryPluginInterface {
protected $userStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, UserStorageInterface $user_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->userStorage = $user_storage;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getStorage('user'));
}
public function defaultConfiguration() {
return [
'enable' => FALSE,
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['enable'] = [
'#title' => $this
->t('Enable'),
'#type' => 'checkbox',
'#default_value' => $this->configuration['enable'],
'#description' => $this
->t('Leaving this unchecked will bypass this condition.'),
'#weight' => 0,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['enable'] = $form_state
->getValue('enable', FALSE);
}
public function evaluate() {
if (!$this->configuration['enable']) {
return TRUE;
}
$dashboard_owner = $this
->getContextValue('dashboard_user');
$current_user = $this
->getContextValue('current_user');
if (is_string($dashboard_owner)) {
$dashboard_owner = $this->userStorage
->load($dashboard_owner);
}
if (!$dashboard_owner
->hasPermission('use moderation dashboard')) {
return FALSE;
}
if ($current_user
->id() === $dashboard_owner
->id()) {
return TRUE;
}
return $current_user
->hasPermission('view any moderation dashboard');
}
public function summary() {
if ($this
->isNegated()) {
return $this
->t("User can't access moderation dashboard.");
}
return $this
->t('User can access moderation dashboard.');
}
}