FileAccessControlHandler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Drupal\file;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Entity\EntityAccessControlHandler;
  5. use Drupal\Core\Entity\EntityInterface;
  6. use Drupal\Core\Entity\EntityStorageInterface;
  7. use Drupal\Core\Field\FieldDefinitionInterface;
  8. use Drupal\Core\Field\FieldItemListInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. /**
  11. * Provides a File access control handler.
  12. */
  13. class FileAccessControlHandler extends EntityAccessControlHandler {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  18. /** @var \Drupal\file\FileInterface $entity */
  19. if ($operation == 'download' || $operation == 'view') {
  20. if (\Drupal::service('file_system')->uriScheme($entity->getFileUri()) === 'public') {
  21. // Always allow access to file in public file system.
  22. return AccessResult::allowed();
  23. }
  24. elseif ($references = $this->getFileReferences($entity)) {
  25. foreach ($references as $field_name => $entity_map) {
  26. foreach ($entity_map as $referencing_entity_type => $referencing_entities) {
  27. /** @var \Drupal\Core\Entity\EntityInterface $referencing_entity */
  28. foreach ($referencing_entities as $referencing_entity) {
  29. $entity_and_field_access = $referencing_entity->access('view', $account, TRUE)->andIf($referencing_entity->$field_name->access('view', $account, TRUE));
  30. if ($entity_and_field_access->isAllowed()) {
  31. return $entity_and_field_access;
  32. }
  33. }
  34. }
  35. }
  36. }
  37. elseif ($entity->getOwnerId() == $account->id()) {
  38. // This case handles new nodes, or detached files. The user who uploaded
  39. // the file can access it even if it's not yet used.
  40. if ($account->isAnonymous()) {
  41. // For anonymous users, only the browser session that uploaded the
  42. // file is positively allowed access to it. See file_save_upload().
  43. // @todo Implement \Drupal\Core\Entity\EntityHandlerInterface so that
  44. // services can be more properly injected.
  45. $allowed_fids = \Drupal::service('session')->get('anonymous_allowed_file_ids', []);
  46. if (!empty($allowed_fids[$entity->id()])) {
  47. return AccessResult::allowed();
  48. }
  49. }
  50. else {
  51. return AccessResult::allowed();
  52. }
  53. }
  54. }
  55. if ($operation == 'delete' || $operation == 'update') {
  56. $account = $this->prepareUser($account);
  57. $file_uid = $entity->get('uid')->getValue();
  58. // Only the file owner can delete and update the file entity.
  59. if ($account->id() == $file_uid[0]['target_id']) {
  60. return AccessResult::allowed();
  61. }
  62. return AccessResult::forbidden();
  63. }
  64. // No opinion.
  65. return AccessResult::neutral();
  66. }
  67. /**
  68. * Wrapper for file_get_file_references().
  69. *
  70. * @param \Drupal\file\FileInterface $file
  71. * The file object for which to get references.
  72. *
  73. * @return array
  74. * A multidimensional array. The keys are field_name, entity_type,
  75. * entity_id and the value is an entity referencing this file.
  76. *
  77. * @see file_get_file_references()
  78. */
  79. protected function getFileReferences(FileInterface $file) {
  80. return file_get_file_references($file, NULL, EntityStorageInterface::FIELD_LOAD_REVISION, NULL);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  86. // Deny access to fields that should only be set on file creation, and
  87. // "status" which should only be changed based on a file's usage.
  88. $create_only_fields = [
  89. 'uri',
  90. 'filemime',
  91. 'filesize',
  92. ];
  93. // The operation is 'edit' when the entity is being created or updated.
  94. // Determine if the entity is being updated by checking if it is new.
  95. $field_name = $field_definition->getName();
  96. if ($operation === 'edit' && $items && ($entity = $items->getEntity()) && !$entity->isNew() && in_array($field_name, $create_only_fields, TRUE)) {
  97. return AccessResult::forbidden();
  98. }
  99. // Regardless of whether the entity exists access should be denied to the
  100. // status field as this is managed via other APIs, for example:
  101. // - \Drupal\file\FileUsage\FileUsageBase::add()
  102. // - \Drupal\file\Plugin\EntityReferenceSelection\FileSelection::createNewEntity()
  103. if ($operation === 'edit' && $field_name === 'status') {
  104. return AccessResult::forbidden();
  105. }
  106. return parent::checkFieldAccess($operation, $field_definition, $account, $items);
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
  112. // The file entity has no "create" permission because by default Drupal core
  113. // does not allow creating file entities independently. It allows you to
  114. // create file entities that are referenced from another entity
  115. // (e.g. an image for a article). A contributed module is free to alter
  116. // this to allow file entities to be created directly.
  117. // @todo Update comment to mention REST module when
  118. // https://www.drupal.org/node/1927648 is fixed.
  119. return AccessResult::neutral();
  120. }
  121. }