3 namespace Drupal\user\Plugin\views\access;
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;
16 * Access plugin that provides permission-based access control.
18 * @ingroup views_access_plugins
22 * title = @Translation("Permission"),
23 * help = @Translation("Access will be granted to users with the specified permission string.")
26 class Permission extends AccessPluginBase implements CacheableDependencyInterface {
31 protected $usesOptions = TRUE;
34 * The permission handler.
36 * @var \Drupal\user\PermissionHandlerInterface
38 protected $permissionHandler;
43 * @var \Drupal\Core\Extension\ModuleHandlerInterface
45 protected $moduleHandler;
48 * Constructs a Permission object.
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
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;
70 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
75 $container->get('user.permissions'),
76 $container->get('module_handler')
83 public function access(AccountInterface $account) {
84 return $account->hasPermission($this->options['perm']);
90 public function alterRouteDefinition(Route $route) {
91 $route->setRequirement('_permission', $this->options['perm']);
94 public function summaryTitle() {
95 $permissions = $this->permissionHandler->getPermissions();
96 if (isset($permissions[$this->options['perm']])) {
97 return $permissions[$this->options['perm']]['title'];
100 return $this->t($this->options['perm']);
104 protected function defineOptions() {
105 $options = parent::defineOptions();
106 $options['perm'] = ['default' => 'access content'];
111 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
112 parent::buildOptionsForm($form, $form_state);
113 // Get list of permissions
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']);
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.'),
134 public function getCacheMaxAge() {
135 return Cache::PERMANENT;
141 public function getCacheContexts() {
142 return ['user.permissions'];
148 public function getCacheTags() {