You are here

class EntityAccessByFieldPermissions in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  2. 8 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  3. 8.2 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  4. 8.3 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  5. 8.4 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  6. 8.5 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  7. 8.6 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  8. 8.7 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  9. 8.8 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  10. 10.3.x modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  11. 10.1.x modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  12. 10.2.x modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions

EntityAccessByFieldPermissions.

Hierarchy

Expanded class hierarchy of EntityAccessByFieldPermissions

1 string reference to 'EntityAccessByFieldPermissions'
entity_access_by_field.services.yml in modules/custom/entity_access_by_field/entity_access_by_field.services.yml
modules/custom/entity_access_by_field/entity_access_by_field.services.yml
1 service uses EntityAccessByFieldPermissions
entity_access_by_field.permissions in modules/custom/entity_access_by_field/entity_access_by_field.services.yml
Drupal\entity_access_by_field\EntityAccessByFieldPermissions

File

modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php, line 14

Namespace

Drupal\entity_access_by_field
View source
class EntityAccessByFieldPermissions implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Constructs a new NodeViewPermissionsPermission instance.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'));
  }

  /**
   * Array with values which need to be ignored.
   *
   * @return array
   *   An array containing a list of values to ignore.
   */
  public static function getIgnoredValues() {
    return EntityAccessHelper::getIgnoredValues();
  }

  /**
   * Return all the permissions generated by this user.
   */
  public function permissions() {
    $permissions = [];
    $contentTypes = $this
      ->getContentTypes();
    foreach ($contentTypes as $bundle) {
      $entity_type = 'node';
      $fields = $this
        ->getEntityAccessFields($entity_type, $bundle);

      /** @var \Drupal\field\Entity\FieldConfig $field */
      foreach ($fields as $field) {
        $field_storage = $field
          ->getFieldStorageDefinition();

        // @todo Add support for allowed_values_function.
        $allowed_values = $field_storage
          ->getSetting('allowed_values');
        if (!empty($allowed_values)) {
          foreach ($allowed_values as $field_key => $field_label) {
            if (!in_array($field_key, $this
              ->getIgnoredValues())) {

              // e.g. label = node.article.field_content_visibility:public.
              $permission_label = $field
                ->id() . ':' . $field_key;
              $permission = 'view ' . $permission_label . ' content';
              $permissions[$permission] = [
                'title' => $this
                  ->t('View @label content', [
                  '@label' => $permission_label,
                ]),
              ];
            }
          }
        }
      }
    }
    return $permissions;
  }

  /**
   * Get the realms array with permissions as value.
   */
  public function getRealmWithPermission() {
    $realms = [];
    $contentTypes = $this
      ->getContentTypes();
    foreach ($contentTypes as $bundle) {
      $entity_type = 'node';
      $fields = $this
        ->getEntityAccessFields($entity_type, $bundle);

      /** @var \Drupal\field\Entity\FieldConfig $field */
      foreach ($fields as $field) {
        $field_storage = $field
          ->getFieldStorageDefinition();

        // @todo Add support for allowed_values_function.
        $allowed_values = $field_storage
          ->getSetting('allowed_values');
        if (!empty($allowed_values)) {
          foreach ($allowed_values as $field_key => $field_label) {

            // e.g. label = node.article.field_content_visibility:public.
            $permission_label = $field
              ->id() . ':' . $field_key;
            $op = 'view';
            $permission = $op . ' ' . $permission_label . ' content';
            $bundle_id = $bundle
              ->id();
            $field_name = $field
              ->getName();
            $realm = $this
              ->getRealmForFieldValue($op, $entity_type, $bundle_id, $field_name, $field_key);
            $realms[$realm] = $permission;
          }
        }
      }
    }
    return $realms;
  }

  /**
   * Returns a realm for a field value in order to create access.
   *
   * @return string
   *   The string with the realm created.
   */
  public function getRealmForFieldValue($op, $entity_type, $bundle_id, $field_name, $field_value) {
    $realm = $op . '_' . $entity_type . '_' . $bundle_id . '_' . $field_name . '_' . $field_value;
    return $realm;
  }

  /**
   * Get all the content types.
   *
   * @return \Drupal\Core\Entity\EntityInterface[]
   *   Returns the entity interface containing all the content types.
   */
  protected function getContentTypes() {
    $contentTypes = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();
    return $contentTypes;
  }

  /**
   * Get all fields of type entity_access_field.
   *
   * @return array
   *   Returns all the fields with the entity type entity_acces_field.
   */
  public function getEntityAccessFields($entity, $bundle) {
    $fields = [];
    $field_definitions = $this->entityFieldManager
      ->getFieldDefinitions($entity, $bundle
      ->id());
    foreach ($field_definitions as $field_name => $field_definition) {
      if (!empty($field_definition
        ->getTargetBundle())) {
        if ($field_definition
          ->getType() === 'entity_access_field') {
          $fields[$field_name] = $field_definition;
        }
      }
    }
    return $fields;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityAccessByFieldPermissions::$entityFieldManager protected property The entity field manager.
EntityAccessByFieldPermissions::$entityTypeManager protected property The entity type manager.
EntityAccessByFieldPermissions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityAccessByFieldPermissions::getContentTypes protected function Get all the content types.
EntityAccessByFieldPermissions::getEntityAccessFields public function Get all fields of type entity_access_field.
EntityAccessByFieldPermissions::getIgnoredValues public static function Array with values which need to be ignored.
EntityAccessByFieldPermissions::getRealmForFieldValue public function Returns a realm for a field value in order to create access.
EntityAccessByFieldPermissions::getRealmWithPermission public function Get the realms array with permissions as value.
EntityAccessByFieldPermissions::permissions public function Return all the permissions generated by this user.
EntityAccessByFieldPermissions::__construct public function Constructs a new NodeViewPermissionsPermission instance.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.