Role.php

Same filename in this branch
  1. 11.x core/modules/user/src/Entity/Role.php
  2. 11.x core/modules/user/src/Plugin/migrate/source/d6/Role.php
  3. 11.x core/modules/user/src/Plugin/migrate/source/d7/Role.php
Same filename and directory in other branches
  1. 9 core/modules/user/src/Entity/Role.php
  2. 9 core/modules/user/src/Plugin/views/access/Role.php
  3. 9 core/modules/user/src/Plugin/migrate/source/d6/Role.php
  4. 9 core/modules/user/src/Plugin/migrate/source/d7/Role.php
  5. 8.9.x core/modules/user/src/Entity/Role.php
  6. 8.9.x core/modules/user/src/Plugin/views/access/Role.php
  7. 8.9.x core/modules/user/src/Plugin/migrate/source/d6/Role.php
  8. 8.9.x core/modules/user/src/Plugin/migrate/source/d7/Role.php
  9. 10 core/modules/user/src/Entity/Role.php
  10. 10 core/modules/user/src/Plugin/views/access/Role.php
  11. 10 core/modules/user/src/Plugin/migrate/source/d6/Role.php
  12. 10 core/modules/user/src/Plugin/migrate/source/d7/Role.php

Namespace

Drupal\user\Plugin\views\access

File

core/modules/user/src/Plugin/views/access/Role.php

View source
<?php

namespace Drupal\user\Plugin\views\access;

use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\user\RoleInterface;
use Drupal\user\RoleStorageInterface;
use Drupal\views\Attribute\ViewsAccess;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;
use Drupal\Core\Session\AccountInterface;

/**
 * Access plugin that provides role-based access control.
 *
 * @ingroup views_access_plugins
 */
class Role extends AccessPluginBase implements CacheableDependencyInterface {
  
  /**
   * {@inheritdoc}
   */
  protected $usesOptions = TRUE;
  
  /**
   * The role storage.
   *
   * @var \Drupal\user\RoleStorageInterface
   */
  protected $roleStorage;
  
  /**
   * Constructs a Role 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\RoleStorageInterface $role_storage
   *   The role storage.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, RoleStorageInterface $role_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->roleStorage = $role_storage;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager')
      ->getStorage('user_role'));
  }
  
  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account) {
    return !empty(array_intersect(array_filter($this->options['role']), $account->getRoles()));
  }
  
  /**
   * {@inheritdoc}
   */
  public function alterRouteDefinition(Route $route) {
    if ($this->options['role']) {
      $route->setRequirement('_role', (string) implode('+', $this->options['role']));
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function summaryTitle() {
    $count = count($this->options['role']);
    if ($count < 1) {
      return $this->t('No role(s) selected');
    }
    elseif ($count > 1) {
      return $this->t('Multiple roles');
    }
    else {
      $rid = reset($this->options['role']);
      return $this->roleStorage
        ->load($rid)
        ->label();
    }
  }
  
  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['role'] = [
      'default' => [],
    ];
    return $options;
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['role'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Role'),
      '#default_value' => $this->options['role'],
      '#options' => array_map(fn(RoleInterface $role) => Html::escape($role->label()), $this->roleStorage
        ->loadMultiple()),
      '#description' => $this->t('Only the checked roles will be able to access this display.'),
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function validateOptionsForm(&$form, FormStateInterface $form_state) {
    $role = $form_state->getValue([
      'access_options',
      'role',
    ]);
    $role = array_filter($role);
    if (!$role) {
      $form_state->setError($form['role'], $this->t('You must select at least one role if type is "by role"'));
    }
    $form_state->setValue([
      'access_options',
      'role',
    ], $role);
  }
  
  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();
    foreach (array_keys($this->options['role']) as $rid) {
      if ($role = $this->roleStorage
        ->load($rid)) {
        $dependencies[$role->getConfigDependencyKey()][] = $role->getConfigDependencyName();
      }
    }
    return $dependencies;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheMaxAge() {
    return Cache::PERMANENT;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheContexts() {
    return [
      'user.roles',
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    return [];
  }

}

Classes

Title Deprecated Summary
Role Access plugin that provides role-based access control.

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