Version 1
[yaffs-website] / web / core / modules / user / src / Plugin / views / filter / Permissions.php
1 <?php
2
3 namespace Drupal\user\Plugin\views\filter;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Extension\ModuleHandlerInterface;
7 use Drupal\user\PermissionHandlerInterface;
8 use Drupal\views\Plugin\views\filter\ManyToOne;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Filter handler for user roles.
13  *
14  * @ingroup views_filter_handlers
15  *
16  * @ViewsFilter("user_permissions")
17  */
18 class Permissions extends ManyToOne {
19
20   /**
21    * The permission handler.
22    *
23    * @var \Drupal\user\PermissionHandlerInterface
24    */
25   protected $permissionHandler;
26
27   /**
28    * The module handler.
29    *
30    * @var \Drupal\Core\Extension\ModuleHandlerInterface
31    */
32   protected $moduleHandler;
33
34   /**
35    * Constructs a Permissions object.
36    *
37    * @param array $configuration
38    *   A configuration array containing information about the plugin instance.
39    * @param string $plugin_id
40    *   The plugin_id for the plugin instance.
41    * @param mixed $plugin_definition
42    *   The plugin implementation definition.
43    * @param \Drupal\user\PermissionHandlerInterface $permission_handler
44    *   The permission handler.
45    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
46    *   The module handler.
47    */
48   public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) {
49     parent::__construct($configuration, $plugin_id, $plugin_definition);
50
51     $this->permissionHandler = $permission_handler;
52     $this->moduleHandler = $module_handler;
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
59     return new static(
60       $configuration,
61       $plugin_id,
62       $plugin_definition,
63       $container->get('user.permissions'),
64       $container->get('module_handler')
65     );
66   }
67
68   public function getValueOptions() {
69     if (!isset($this->valueOptions)) {
70       $permissions = $this->permissionHandler->getPermissions();
71       foreach ($permissions as $perm => $perm_item) {
72         $provider = $perm_item['provider'];
73         $display_name = $this->moduleHandler->getName($provider);
74         $this->valueOptions[$display_name][$perm] = Html::escape(strip_tags($perm_item['title']));
75       }
76       return $this->valueOptions;
77     }
78     else {
79       return $this->valueOptions;
80     }
81   }
82
83   /**
84    * {@inheritdoc}
85    *
86    * Replace the configured permission with a filter by all roles that have this
87    * permission.
88    */
89   public function query() {
90     // @todo user_role_names() should maybe support multiple permissions.
91     $rids = [];
92     // Get all role IDs that have the configured permissions.
93     foreach ($this->value as $permission) {
94       $roles = user_role_names(FALSE, $permission);
95       // user_role_names() returns an array with the role IDs as keys, so take
96       // the array keys and merge them with previously found role IDs.
97       $rids = array_merge($rids, array_keys($roles));
98     }
99     // Remove any duplicate role IDs.
100     $rids = array_unique($rids);
101     $this->value = $rids;
102
103     // $this->value contains the role IDs that have the configured permission.
104     parent::query();
105   }
106
107 }