Skip to content

Commit

Permalink
New file_can_view_or_download() function
Browse files Browse the repository at this point in the history
file_can_view_bug_attachments() and file_can_download_bug_attachments()
have nearly identical code, the only difference being the names of the
configs.

Adding a new internal File API function to avoid code duplication.

Fixes #27299
  • Loading branch information
dregad committed Sep 23, 2020
1 parent 221cf32 commit 90b8395
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions core/file_api.php
Expand Up @@ -206,30 +206,72 @@ function file_bug_has_attachments( $p_bug_id ) {
}
}

/**
* Check if the current user can view or download attachments.
*
* Generic call used by
* - {@see file_can_view_bug_attachments()}
* - {@see file_can_view_bugnote_attachments}
* - {@see file_can_download_bug_attachments()}
* - {@see file_can_download_bugnote_attachments}
*
* @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
*
* @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 ) {
switch( $p_action ) {
case 'view':
$t_threshold_global = 'view_attachments_threshold';
$t_threshold_own = 'allow_view_own_attachments';
break;
case 'download':
$t_threshold_global = 'download_attachments_threshold';
$t_threshold_own = 'allow_download_own_attachments';
break;
default:
trigger_error( ERROR_GENERIC, ERROR );
}

$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( $t_can_access ) {
return true;
}

$t_uploaded_by_me = auth_get_current_user_id() == $p_uploader_user_id;
$t_view_own = config_get( $t_threshold_own, null, null, $t_project_id );
return $t_uploaded_by_me && $t_view_own;
}

/**
* Check if the current user can view attachments for the specified bug.
*
* @param integer $p_bug_id A bug identifier.
* @param integer $p_uploader_user_id A user identifier.
*
* @return boolean
*/
function file_can_view_bug_attachments( $p_bug_id, $p_uploader_user_id = null ) {
$t_uploaded_by_me = auth_get_current_user_id() === $p_uploader_user_id;
$t_can_view = access_has_bug_level( config_get( 'view_attachments_threshold' ), $p_bug_id );
$t_can_view = $t_can_view || ( $t_uploaded_by_me && config_get( 'allow_view_own_attachments' ) );
return $t_can_view;
return file_can_view_or_download( 'view', $p_bug_id, $p_uploader_user_id );
}

/**
* Check if the current user can download attachments for the specified bug.
*
* @param integer $p_bug_id A bug identifier.
* @param integer $p_uploader_user_id A user identifier.
* @param integer $p_uploader_user_id The user who uploaded the attachment.
*
* @return boolean
*/
function file_can_download_bug_attachments( $p_bug_id, $p_uploader_user_id = null ) {
$t_uploaded_by_me = auth_get_current_user_id() === $p_uploader_user_id;
$t_can_download = access_has_bug_level( config_get( 'download_attachments_threshold', null, null, bug_get_field( $p_bug_id, 'project_id' ) ), $p_bug_id );
$t_can_download = $t_can_download || ( $t_uploaded_by_me && config_get( 'allow_download_own_attachments', null, null, bug_get_field( $p_bug_id, 'project_id' ) ) );
return $t_can_download;
return file_can_view_or_download( 'download', $p_bug_id, $p_uploader_user_id );
}

/**
Expand Down

0 comments on commit 90b8395

Please sign in to comment.