Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / Plugin / views / filter / Roles.php
1 <?php
2
3 namespace Drupal\user\Plugin\views\filter;
4
5 use Drupal\user\RoleInterface;
6 use Drupal\user\RoleStorageInterface;
7 use Drupal\views\Plugin\views\filter\ManyToOne;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Filter handler for user roles.
12  *
13  * @ingroup views_filter_handlers
14  *
15  * @ViewsFilter("user_roles")
16  */
17 class Roles extends ManyToOne {
18
19   /**
20    * The role storage.
21    *
22    * @var \Drupal\user\RoleStorageInterface
23    */
24   protected $roleStorage;
25
26   /**
27    * Constructs a Roles object.
28    *
29    * @param array $configuration
30    *   A configuration array containing information about the plugin instance.
31    * @param string $plugin_id
32    *   The plugin_id for the plugin instance.
33    * @param mixed $plugin_definition
34    *   The plugin implementation definition.
35    * @param \Drupal\user\RoleStorageInterface $role_storage
36    *   The role storage.
37    */
38   public function __construct(array $configuration, $plugin_id, $plugin_definition, RoleStorageInterface $role_storage) {
39     parent::__construct($configuration, $plugin_id, $plugin_definition);
40     $this->roleStorage = $role_storage;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
47     return new static(
48       $configuration,
49       $plugin_id,
50       $plugin_definition,
51       $container->get('entity.manager')->getStorage('user_role')
52     );
53   }
54
55   public function getValueOptions() {
56     $this->valueOptions = user_role_names(TRUE);
57     unset($this->valueOptions[RoleInterface::AUTHENTICATED_ID]);
58     return $this->valueOptions;
59
60   }
61
62   /**
63    * Override empty and not empty operator labels to be clearer for user roles.
64    */
65   public function operators() {
66     $operators = parent::operators();
67     $operators['empty']['title'] = $this->t("Only has the 'authenticated user' role");
68     $operators['not empty']['title'] = $this->t("Has roles in addition to 'authenticated user'");
69     return $operators;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public function calculateDependencies() {
76     $dependencies = [];
77     if (in_array($this->operator, ['empty', 'not empty'])) {
78       return $dependencies;
79     }
80     foreach ($this->value as $role_id) {
81       $role = $this->roleStorage->load($role_id);
82       $dependencies[$role->getConfigDependencyKey()][] = $role->getConfigDependencyName();
83     }
84     return $dependencies;
85   }
86
87 }