View source
<?php
namespace Drupal\commerce\Plugin\views\argument_validator;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\PermissionHandlerInterface;
use Drupal\user\UserInterface;
use Drupal\views\Plugin\views\argument_validator\ArgumentValidatorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CurrentUser extends ArgumentValidatorPluginBase implements CacheableDependencyInterface {
protected $currentUser;
protected $entityTypeManager;
protected $moduleHandler;
protected $permissionHandler;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, PermissionHandlerInterface $permission_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->permissionHandler = $permission_handler;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_user'), $container
->get('entity_type.manager'), $container
->get('module_handler'), $container
->get('user.permissions'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['admin_permission'] = [
'default' => '',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$permissions = [];
foreach ($this->permissionHandler
->getPermissions() as $permission => $permission_item) {
$provider = $permission_item['provider'];
$display_name = $this->moduleHandler
->getName($provider);
$permissions[$display_name][$permission] = Html::escape($permission_item['title']);
}
$form['admin_permission'] = [
'#type' => 'select',
'#title' => $this
->t('Admin permission'),
'#description' => $this
->t('Allows the current user to access the view even if the argument is a different user.'),
'#options' => $permissions,
'#empty_value' => '',
'#default_value' => $this->options['admin_permission'],
];
}
public function validateArgument($argument) {
if (!is_numeric($argument)) {
return FALSE;
}
$user_storage = $this->entityTypeManager
->getStorage('user');
$user = $user_storage
->load($argument);
if (!$user instanceof UserInterface) {
return FALSE;
}
if ($user
->isAnonymous()) {
return FALSE;
}
if ($user
->id() == $this->currentUser
->id()) {
return TRUE;
}
if (!empty($this->options['admin_permission'])) {
return $this->currentUser
->hasPermission($this->options['admin_permission']);
}
return FALSE;
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return [
'user',
];
}
public function getCacheTags() {
return [];
}
}