You are here

class EntityAccessByFieldPermissions in Open Social 8.2

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.3 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  4. 8.4 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  5. 8.5 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  6. 8.6 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  7. 8.7 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  8. 8.8 modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  9. 10.3.x modules/custom/entity_access_by_field/src/EntityAccessByFieldPermissions.php \Drupal\entity_access_by_field\EntityAccessByFieldPermissions
  10. 10.0.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;
  use UrlGeneratorTrait;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Constructs a new NodeViewPermissionsPermission instance.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityManagerInterface $entity_manager) {
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.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->entityManager
      ->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->entityManager
      ->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::$entityManager protected property The entity 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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::redirect Deprecated protected function Returns a redirect response object for the specified route. 3
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.