8d0c43dfcef7fc05ce4376c4ce6dd4a6e2e49637
[yaffs-website] / web / core / modules / user / src / Plugin / views / access / Permission.php
1 <?php
2
3 namespace Drupal\user\Plugin\views\access;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\user\PermissionHandlerInterface;
11 use Drupal\views\Plugin\views\access\AccessPluginBase;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Symfony\Component\Routing\Route;
14
15 /**
16  * Access plugin that provides permission-based access control.
17  *
18  * @ingroup views_access_plugins
19  *
20  * @ViewsAccess(
21  *   id = "perm",
22  *   title = @Translation("Permission"),
23  *   help = @Translation("Access will be granted to users with the specified permission string.")
24  * )
25  */
26 class Permission extends AccessPluginBase implements CacheableDependencyInterface {
27
28   /**
29    * {@inheritdoc}
30    */
31   protected $usesOptions = TRUE;
32
33   /**
34    * The permission handler.
35    *
36    * @var \Drupal\user\PermissionHandlerInterface
37    */
38   protected $permissionHandler;
39
40   /**
41    * The module handler.
42    *
43    * @var \Drupal\Core\Extension\ModuleHandlerInterface
44    */
45   protected $moduleHandler;
46
47   /**
48    * Constructs a Permission object.
49    *
50    * @param array $configuration
51    *   A configuration array containing information about the plugin instance.
52    * @param string $plugin_id
53    *   The plugin_id for the plugin instance.
54    * @param mixed $plugin_definition
55    *   The plugin implementation definition.
56    * @param \Drupal\user\PermissionHandlerInterface $permission_handler
57    *   The permission handler.
58    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
59    *   The module handler.
60    */
61   public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) {
62     parent::__construct($configuration, $plugin_id, $plugin_definition);
63     $this->permissionHandler = $permission_handler;
64     $this->moduleHandler = $module_handler;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
71     return new static(
72       $configuration,
73       $plugin_id,
74       $plugin_definition,
75       $container->get('user.permissions'),
76       $container->get('module_handler')
77     );
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function access(AccountInterface $account) {
84     return $account->hasPermission($this->options['perm']);
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function alterRouteDefinition(Route $route) {
91     $route->setRequirement('_permission', $this->options['perm']);
92   }
93
94   public function summaryTitle() {
95     $permissions = $this->permissionHandler->getPermissions();
96     if (isset($permissions[$this->options['perm']])) {
97       return $permissions[$this->options['perm']]['title'];
98     }
99
100     return $this->t($this->options['perm']);
101   }
102
103
104   protected function defineOptions() {
105     $options = parent::defineOptions();
106     $options['perm'] = ['default' => 'access content'];
107
108     return $options;
109   }
110
111   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
112     parent::buildOptionsForm($form, $form_state);
113     // Get list of permissions
114     $perms = [];
115     $permissions = $this->permissionHandler->getPermissions();
116     foreach ($permissions as $perm => $perm_item) {
117       $provider = $perm_item['provider'];
118       $display_name = $this->moduleHandler->getName($provider);
119       $perms[$display_name][$perm] = strip_tags($perm_item['title']);
120     }
121
122     $form['perm'] = [
123       '#type' => 'select',
124       '#options' => $perms,
125       '#title' => $this->t('Permission'),
126       '#default_value' => $this->options['perm'],
127       '#description' => $this->t('Only users with the selected permission flag will be able to access this display.'),
128     ];
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   public function getCacheMaxAge() {
135     return Cache::PERMANENT;
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function getCacheContexts() {
142     return ['user.permissions'];
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function getCacheTags() {
149     return [];
150   }
151
152 }