Skip to content

Commit

Permalink
Functions to check view/download ability at bugnote level
Browse files Browse the repository at this point in the history
2 new File API functions:
- file_can_view_bugnote_attachments()
- file_can_download_bugnote_attachments

Prerequisite to fix issue #27039
  • Loading branch information
dregad committed Sep 23, 2020
1 parent 90b8395 commit 5595c90
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions core/file_api.php
Expand Up @@ -218,12 +218,13 @@ function file_bug_has_attachments( $p_bug_id ) {
* @param string $p_action 'view' or 'download'
* @param int $p_bug_id A bug identifier
* @param int $p_uploader_user_id The user who uploaded the attachment
* @param int|null $p_bugnote_id If specified, will check at bugnote level
*
* @return bool
*
* @internal Should not be used outside of File API.
*/
function file_can_view_or_download( $p_action, $p_bug_id, $p_uploader_user_id ) {
function file_can_view_or_download( $p_action, $p_bug_id, $p_uploader_user_id, $p_bugnote_id = null ) {
switch( $p_action ) {
case 'view':
$t_threshold_global = 'view_attachments_threshold';
Expand All @@ -240,7 +241,11 @@ function file_can_view_or_download( $p_action, $p_bug_id, $p_uploader_user_id )
$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
$t_access_global = config_get( $t_threshold_global,null, null, $t_project_id );

$t_can_access = access_has_bug_level( $t_access_global, $p_bug_id );
if( $p_bugnote_id === null ) {
$t_can_access = access_has_bug_level( $t_access_global, $p_bug_id );
} else {
$t_can_access = access_has_bugnote_level( $t_access_global, $p_bugnote_id );
}
if( $t_can_access ) {
return true;
}
Expand All @@ -262,6 +267,22 @@ function file_can_view_bug_attachments( $p_bug_id, $p_uploader_user_id = null )
return file_can_view_or_download( 'view', $p_bug_id, $p_uploader_user_id );
}

/**
* Check if the current user can view attachments for the specified bug note.
*
* @param integer $p_bugnote_id A bugnote identifier.
* @param integer $p_uploader_user_id The user who uploaded the attachment.
*
* @return boolean
*/
function file_can_view_bugnote_attachments( $p_bugnote_id, $p_uploader_user_id = null ) {
if( $p_bugnote_id == 0 ) {
return true;
}
$t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
return file_can_view_or_download( 'view', $t_bug_id, $p_uploader_user_id );
}

/**
* Check if the current user can download attachments for the specified bug.
*
Expand All @@ -274,6 +295,22 @@ function file_can_download_bug_attachments( $p_bug_id, $p_uploader_user_id = nul
return file_can_view_or_download( 'download', $p_bug_id, $p_uploader_user_id );
}

/**
* Check if the current user can download attachments for the specified bug note.
*
* @param integer $p_bugnote_id A bugnote identifier.
* @param integer $p_uploader_user_id The user who uploaded the attachment.
*
* @return boolean
*/
function file_can_download_bugnote_attachments( $p_bugnote_id, $p_uploader_user_id = null ) {
if( $p_bugnote_id == 0 ) {
return true;
}
$t_bug_id = bugnote_get_field( $p_bugnote_id, 'bug_id' );
return file_can_view_or_download( 'download', $t_bug_id, $p_uploader_user_id, $p_bugnote_id );
}

/**
* Check if the current user can delete attachments from the specified bug.
* @param integer $p_bug_id A bug identifier.
Expand Down

0 comments on commit 5595c90

Please sign in to comment.