You are here

function tft_file_access in Taxonomy File Tree 8

Same name and namespace in other branches
  1. 3.x tft.module \tft_file_access()

Implements hook_ENTITY_TYPE_access().

File

./tft.module, line 662
Contains tft.module.

Code

function tft_file_access(EntityInterface $entity, $operation, AccountInterface $account) {

  // Check that user has an access to the group.
  $fid = $entity
    ->get('fid')
    ->getValue();
  $query = Drupal::service('entity.query')
    ->get('media')
    ->condition('tft_file', $fid[0]['value']);
  $entity_id = array_values($query
    ->execute());
  if (isset($entity_id[0])) {
    $media = Media::load($entity_id[0]);
  }
  if (!empty($media)) {
    $folder = $media
      ->get('tft_folder')
      ->getValue();
    $tid = reset($folder)['target_id'];
    if (empty($gid = _tft_get_group_gid($tid))) {
      return AccessResult::forbidden();
    }
    $group = Group::load($gid);
    if (!$group
      ->access('view')) {
      return AccessResult::forbidden();
    }
  }
}