There is a permissions "send arbitrary files" that allows sending files/attachments that are not inside public://

We had a need for altering this access-permission on a request basis. Therefore created a hook HOOK_mimemail_file_access_alter() that can be used to change the permission dynamically.

Example usage:

/**
 * Implements HOOK_mimemail_file_access_alter().
 */
function MODULE_mimemail_file_access_alter(&$access, $url) {

  // For security hardening we validate that the file is created within the
  // same request.
  $files = file_load_multiple(array(), array('uri' => $url));
  $file = reset($files);

  if ($file->timestamp == REQUEST_TIME) {
    $access = TRUE;
  }

}

Comments

ndf created an issue. See original summary.

ndf’s picture

ndf’s picture

Status: Active » Needs review
ronino’s picture

I am looking for a way to attach private files to emails without setting the "send arbitrary files" permission.

If I am not mistaken, then with the patch the hook implementation called last will decide about access to the file no matter what other hook implementations set before.

The file module uses a different approach in file.module to determine whether a user can download a file which could also work here:

// Invoke hook and collect grants/denies for download access.
// Default to FALSE and let entities overrule this ruling.
$grants = array('system' => FALSE);
foreach (module_implements('file_download_access') as $module) {
  $grants = array_merge($grants, array($module => module_invoke($module, 'file_download_access', $field_item, $entity_type, $entity)));
}
// Allow other modules to alter the returned grants/denies.
drupal_alter('file_download_access', $grants, $field_item, $entity_type, $entity);

if (in_array(TRUE, $grants)) {
  // If TRUE is returned, access is granted and no further checks are
  // necessary.
  $denied = FALSE;
  break 3;
}

If any hook implementation grants access, then access is allowed.

tr’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Needs work, based on #4. Also needs tests. Tests are especially important when we start messing with permissions - we need some assurance that the code is doing exactly what we expect of it.