cms/drupal/modules/file/file.api.php
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Hooks for file module.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Control download access to files.
       
    10  *
       
    11  * The hook is typically implemented to limit access based on the entity the
       
    12  * file is referenced, e.g., only users with access to a node should be allowed
       
    13  * to download files attached to that node.
       
    14  *
       
    15  * @param array $file_item
       
    16  *   The array of information about the file to check access for.
       
    17  * @param $entity_type
       
    18  *   The type of $entity; for example, 'node' or 'user'.
       
    19  * @param $entity
       
    20  *   The $entity to which $file is referenced.
       
    21  *
       
    22  * @return
       
    23  *   TRUE is access should be allowed by this entity or FALSE if denied. Note
       
    24  *   that denial may be overridden by another entity controller, making this
       
    25  *   grant permissive rather than restrictive.
       
    26  *
       
    27  * @see hook_field_access().
       
    28  */
       
    29 function hook_file_download_access($file_item, $entity_type, $entity) {
       
    30   if ($entity_type == 'node') {
       
    31     return node_access('view', $entity);
       
    32   }
       
    33 }
       
    34 
       
    35 /**
       
    36  * Alter the access rules applied to a file download.
       
    37  *
       
    38  * Entities that implement file management set the access rules for their
       
    39  * individual files. Module may use this hook to create custom access rules
       
    40  * for file downloads.
       
    41  *
       
    42  * @see hook_file_download_access().
       
    43  *
       
    44  * @param $grants
       
    45  *   An array of grants gathered by hook_file_download_access(). The array is
       
    46  *   keyed by the module that defines the entity type's access control; the
       
    47  *   values are Boolean grant responses for each module.
       
    48  * @param array $file_item
       
    49  *   The array of information about the file to alter access for.
       
    50  * @param $entity_type
       
    51  *   The type of $entity; for example, 'node' or 'user'.
       
    52  * @param $entity
       
    53  *   The $entity to which $file is referenced.
       
    54  */
       
    55 function hook_file_download_access_alter(&$grants, $file_item, $entity_type, $entity) {
       
    56   // For our example module, we always enforce the rules set by node module.
       
    57   if (isset($grants['node'])) {
       
    58     $grants = array('node' => $grants['node']);
       
    59   }
       
    60 }