class Permission

Same name and namespace in other branches
  1. 11.x core/modules/user/src/Plugin/views/access/Permission.php \Drupal\user\Plugin\views\access\Permission

Access plugin that provides permission-based access control.

Plugin annotation


@ViewsAccess(
  id = "perm",
  title = @Translation("Permission"),
  help = @Translation("Access will be granted to users with the specified permission string.")
)

Hierarchy

Expanded class hierarchy of Permission

Related topics

1 file declares its use of Permission
AccessPermissionTest.php in core/modules/user/tests/src/Kernel/Views/AccessPermissionTest.php
21 string references to 'Permission'
AccessPermissionTest::testAccessPerm in core/modules/user/tests/src/Kernel/Views/AccessPermissionTest.php
Tests perm access plugin.
drupal6.php in core/modules/migrate_drupal/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal6.php in core/modules/aggregator/tests/fixtures/drupal6.php
A database agnostic dump for testing purposes.
drupal7.php in core/modules/migrate_drupal/tests/fixtures/drupal7.php
A database agnostic dump for testing purposes.
drupal7.php in core/modules/tracker/tests/fixtures/drupal7.php
A database agnostic dump for testing purposes.

... See full list

File

core/modules/user/src/Plugin/views/access/Permission.php, line 26

Namespace

Drupal\user\Plugin\views\access
View source
class Permission extends AccessPluginBase implements CacheableDependencyInterface {
  
  /**
   * {@inheritdoc}
   */
  protected $usesOptions = TRUE;
  
  /**
   * The permission handler.
   *
   * @var \Drupal\user\PermissionHandlerInterface
   */
  protected $permissionHandler;
  
  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;
  
  /**
   * Constructs a Permission object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\user\PermissionHandlerInterface $permission_handler
   *   The permission handler.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->permissionHandler = $permission_handler;
    $this->moduleHandler = $module_handler;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('user.permissions'), $container->get('module_handler'));
  }
  
  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account) {
    return $account->hasPermission($this->options['perm']);
  }
  
  /**
   * {@inheritdoc}
   */
  public function alterRouteDefinition(Route $route) {
    $route->setRequirement('_permission', $this->options['perm']);
  }
  public function summaryTitle() {
    $permissions = $this->permissionHandler
      ->getPermissions();
    if (isset($permissions[$this->options['perm']])) {
      return $permissions[$this->options['perm']]['title'];
    }
    return $this->t($this->options['perm']);
  }
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['perm'] = [
      'default' => 'access content',
    ];
    return $options;
  }
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    // Get list of permissions
    $perms = [];
    $permissions = $this->permissionHandler
      ->getPermissions();
    foreach ($permissions as $perm => $perm_item) {
      $provider = $perm_item['provider'];
      $display_name = $this->moduleHandler
        ->getName($provider);
      $perms[$display_name][$perm] = strip_tags($perm_item['title']);
    }
    $form['perm'] = [
      '#type' => 'select',
      '#options' => $perms,
      '#title' => $this->t('Permission'),
      '#default_value' => $this->options['perm'],
      '#description' => $this->t('Only users with the selected permission flag will be able to access this display.'),
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return [
      'user.permissions',
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return [];
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.