You are here

function social_file_private_requirements in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  2. 8.2 modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  3. 8.3 modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  4. 8.4 modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  5. 8.5 modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  6. 8.6 modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  7. 8.7 modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  8. 8.8 modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  9. 10.3.x modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  10. 10.0.x modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  11. 10.1.x modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()
  12. 10.2.x modules/custom/social_file_private/social_file_private.install \social_file_private_requirements()

Implements hook_requirements().

File

modules/custom/social_file_private/social_file_private.install, line 13
The social_file_private install file.

Code

function social_file_private_requirements($phase) {
  $requirements = [];

  // Test if the Private File System Path is set.
  if ($phase == 'runtime') {
    $file_private_path_base_path = PrivateStream::basePath();
    if ($file_private_path_base_path === NULL) {
      $requirements['social_file_private_path'] = [
        'title' => 'Social Private Files',
        'value' => t('Private file system path not set'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('Your uploaded files on the Open Social entities are not fully protected because you did not set a Private File Directory. You need to set an existing local file system path for storing private files. It should be writable by Drupal and not accessible over the web. This must be changed in settings.php. More info: https://github.com/goalgorilla/drupal_social/wiki/Private-files'),
      ];
    }
    else {
      $requirements['social_file_private_path'] = [
        'title' => 'Social Private Files',
        'value' => t('Private file system path is set'),
        'severity' => REQUIREMENT_OK,
        'description' => t('Assuming your private file system path configuration is functional and no other warnings are shown: your uploaded files on the Open Social entities are fully protected. More info: https://github.com/goalgorilla/drupal_social/wiki/Private-files'),
      ];
    }
    $image_fields = \Drupal::entityManager()
      ->getStorage('field_storage_config')
      ->loadByProperties([
      'type' => 'image',
    ]);
    $file_fields = \Drupal::entityManager()
      ->getStorage('field_storage_config')
      ->loadByProperties([
      'type' => 'file',
    ]);
    $all_fields = array_merge($image_fields, $file_fields);

    /* @var $plugin \Drupal\social_file_private\SocialFilePrivateFieldsConfigOverride */
    $file_private_fields_config_override = \Drupal::service('social_file_private_fields.overrider');
    $protected_fields = $file_private_fields_config_override
      ->getFileImageFieldsToProtect();

    /** @var \Drupal\field\FieldStorageConfigInterface $field */
    $unprotected_fields = [];
    foreach ($all_fields as $field) {
      $id = $field
        ->getOriginalId();
      if ($id === 'font.field_fonts') {
        continue;
      }
      if (!in_array('field.storage.' . $id, $protected_fields)) {
        if ($field
          ->getSetting('uri_scheme') === 'public') {
          $unprotected_fields[$id] = $id;
        }
      }
    }
    if (!empty($unprotected_fields)) {
      $requirements['social_file_private_path'] = [
        'title' => 'Social Private Files Unprotected',
        'value' => t('Private file system path not set for some fields'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('Some files uploaded on fields are not protected against access by unauthorized users. These fields were found: :fields More info: https://github.com/goalgorilla/drupal_social/wiki/Private-files', [
          ':fields' => implode(',', $unprotected_fields),
        ]),
      ];
    }
  }
  return $requirements;
}