Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Support standard filters in REST API
The standard filters supported are:

- `assigned` - issues assigned to me.
- `unassigned` - unassigned issues.
- `reported` - issues reported by me.
- `monitored` - issues monitored by me.

Fixes #22790
  • Loading branch information
vboctor committed Dec 5, 2017
1 parent 03f76e1 commit 6633280
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
7 changes: 2 additions & 5 deletions api/rest/restcore/issues_rest.php
Expand Up @@ -79,12 +79,9 @@ function rest_issue_get( \Slim\Http\Request $p_request, \Slim\Http\Response $p_r
$t_message = "Project '$t_project_id' doesn't exist";
$p_response = $p_response->withStatus( HTTP_STATUS_NOT_FOUND, $t_message );
} else {
$t_filter_id = (int)$p_request->getParam( 'filter_id', 0 );
if( $t_filter_id !== 0 ) {
# TODO: we should have a better way to do this.
global $g_project_override;
$g_project_override = $t_project_id;
$t_filter_id = trim( $p_request->getParam( 'filter_id', '' ) );

if( !empty( $t_filter_id ) ) {
$t_issues = mc_filter_get_issues(
'', '', $t_project_id, $t_filter_id, $t_page_number, $t_page_size );
} else {
Expand Down
41 changes: 29 additions & 12 deletions api/soap/mc_filter_api.php
Expand Up @@ -103,7 +103,8 @@ function mc_filter_get( $p_username, $p_password, $p_project_id ) {
* @param string $p_username The name of the user trying to access the filters.
* @param string $p_password The password of the user.
* @param integer $p_project_id The id of the project to retrieve filters for.
* @param integer $p_filter_id The id of the filter to apply.
* @param integer|string $p_filter_id The id of the filter to apply,
* or standard filter (see FILTER_STANDARD_* constants).
* @param integer $p_page_number Start with the given page number (zero-based).
* @param integer $p_per_page Number of issues to display per page.
* @return array that represents an IssueDataArray structure
Expand All @@ -113,31 +114,47 @@ function mc_filter_get_issues( $p_username, $p_password, $p_project_id, $p_filte
if( $t_user_id === false ) {
return mci_fault_login_failed();
}

$t_lang = mci_get_user_lang( $t_user_id );

if( !mci_has_readonly_access( $t_user_id, $p_project_id ) ) {
return mci_fault_access_denied( $t_user_id );
}

$t_filter = filter_load( $p_filter_id, $t_user_id );
if( $t_filter === null ) {
return ApiObjectFactory::faultNotFound( "Unknown filter '$p_filter_id'" );
}

if( $t_filter === false ) {
return ApiObjectFactory::faultServerError( "Invalid Filter '$p_filter_id'" );
}

# TODO: we should have a better way to do this.
global $g_project_override;
$g_project_override = $p_project_id;

$t_orig_page_number = $p_page_number < 1 ? 1 : $p_page_number;
$t_page_count = 0;
$t_bug_count = 0;
$t_filter = filter_db_get_filter( $p_filter_id );
$t_filter_detail = explode( '#', $t_filter, 2 );
if( !isset( $t_filter_detail[1] ) ) {
return ApiObjectFactory::faultServerError( 'Invalid Filter' );
}
$t_filter = json_decode( $t_filter_detail[1], true );
$t_filter = filter_ensure_valid_filter( $t_filter );

$t_result = array();
$t_rows = filter_get_bug_rows( $p_page_number, $p_per_page, $t_page_count, $t_bug_count, $t_filter, $p_project_id );
$t_show_sticky = false;

$t_rows = filter_get_bug_rows(
$p_page_number,
$p_per_page,
$t_page_count,
$t_bug_count,
$t_filter,
$p_project_id,
$t_user_id,
$t_show_sticky );

# the page number was moved back, so we have exceeded the actual page number, see bug #12991
if( $t_orig_page_number > $p_page_number ) {
return $t_result;
return array();
}

$t_result = array();
foreach( $t_rows as $t_issue_data ) {
$t_result[] = mci_issue_data_as_array( $t_issue_data, $t_user_id, $t_lang );
}
Expand Down
6 changes: 6 additions & 0 deletions core/constant_inc.php
Expand Up @@ -478,6 +478,12 @@
define( 'FILTER_MATCH_ALL', 0 );
define( 'FILTER_MATCH_ANY', 1 );

# Standard Filters
define( 'FILTER_STANDARD_ASSIGNED', 'assigned' );
define( 'FILTER_STANDARD_UNASSIGNED', 'unassigned' );
define( 'FILTER_STANDARD_REPORTED', 'reported' );
define( 'FILTER_STANDARD_MONITORED', 'monitored' );

# Versions
define( 'VERSION_ALL', null );
define( 'VERSION_FUTURE', false );
Expand Down
47 changes: 47 additions & 0 deletions core/filter_api.php
Expand Up @@ -2849,6 +2849,53 @@ function filter_db_get_filter( $p_filter_id, $p_user_id = null ) {
return $g_cache_filter_db_filters[$c_filter_id];
}

/**
* Load a filter from db or a standard filter.
*
* @param string|integer $p_filter_id The filter id or standard filter.
* @param integer|null $p_user_id The user id or null for logged in user.
* @return null filter not found, false invalid filter, otherwise the filter.
*/
function filter_load( $p_filter_id, $p_user_id = null ) {
if( is_numeric( $p_filter_id ) ) {
$p_filter_id = (int)$p_filter_id;
$t_filter = filter_db_get_filter( $p_filter_id );
if( $t_filter === null ) {
return null;
}

$t_filter_detail = explode( '#', $t_filter, 2 );
if( !isset( $t_filter_detail[1] ) ) {
return false;
}

$t_filter = json_decode( $t_filter_detail[1], true );
} else {
$p_filter_id = strtolower( $p_filter_id );
$t_project_id = helper_get_current_project();
$t_user_id = auth_get_current_user_id();

switch( $p_filter_id ) {
case FILTER_STANDARD_ASSIGNED:
$t_filter = filter_create_assigned_to_unresolved( $t_project_id, $t_user_id );
break;
case FILTER_STANDARD_UNASSIGNED:
$t_filter = filter_create_assigned_to_unresolved( $t_project_id, NO_USER );
break;
case FILTER_STANDARD_REPORTED:
$t_filter = filter_create_reported_by( $t_project_id, $t_user_id );
break;
case FILTER_STANDARD_MONITORED:
$t_filter = filter_create_monitored_by( $t_project_id, $t_user_id );
break;
default:
return null;
}
}

return filter_ensure_valid_filter( $t_filter );
}

/**
* get current filter for given project and user
* @param integer $p_project_id A project identifier.
Expand Down

0 comments on commit 6633280

Please sign in to comment.