Skip to content

Commit

Permalink
Add optional user_id/project_id parameters to some functions to fix t…
Browse files Browse the repository at this point in the history
…he filter migration in the installer
  • Loading branch information
mantis committed Apr 7, 2013
1 parent eae2a50 commit 17298ed
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 87 deletions.
12 changes: 9 additions & 3 deletions core/columns_api.php
Expand Up @@ -246,7 +246,7 @@ function columns_plugin_cache_issue_data( $p_bugs ) {
* @return array array of columns
* @access public
*/
function columns_get_all( $p_project_id = null ) {
function columns_get_all( $p_project_id = null, $p_user_id = null ) {
$t_columns = columns_get_standard();

# add plugin columns
Expand All @@ -259,9 +259,15 @@ function columns_get_all( $p_project_id = null ) {
$t_project_id = $p_project_id;
}

$t_related_custom_field_ids = custom_field_get_linked_ids( $t_project_id );
if( $p_user_id === null ) {
$t_user_id = auth_get_current_user_id();
} else {
$t_user_id = $p_user_id;
}

$t_related_custom_field_ids = custom_field_get_linked_ids( $t_project_id, $t_user_id );
foreach( $t_related_custom_field_ids as $t_id ) {
if( !custom_field_has_read_access_by_project_id( $t_id, $t_project_id ) ) {
if( !custom_field_has_read_access_by_project_id( $t_id, $t_project_id, $t_user_id ) ) {
continue;
}

Expand Down
21 changes: 8 additions & 13 deletions core/custom_field_api.php
Expand Up @@ -50,15 +50,6 @@
require_api( 'string_api.php' );
require_api( 'utility_api.php' );

# ## Custom Fields API ###
# *******************************************
# TODO
# - add an object to store field data like BugData and UserPrefs ?
# - add caching functions like user, bug, etc
# - make existing api functions use caching functions
# - add functions to return individual db columns for a field definition
# *******************************************

$g_custom_field_types[CUSTOM_FIELD_TYPE_STRING] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_NUMERIC] = 'standard';
$g_custom_field_types[CUSTOM_FIELD_TYPE_FLOAT] = 'standard';
Expand Down Expand Up @@ -202,8 +193,7 @@ function custom_field_is_linked( $p_field_id, $p_project_id ) {
}

# figure out if this bug_id/field_id combination exists
$t_query = "SELECT COUNT(*) FROM {custom_field_project}
WHERE field_id=%d AND project_id=%d";
$t_query = "SELECT COUNT(*) FROM {custom_field_project} WHERE field_id=%d AND project_id=%d";
$t_result = db_query( $t_query, array( $p_field_id, $p_project_id ) );
$t_count = db_result( $t_result );

Expand Down Expand Up @@ -781,18 +771,23 @@ function custom_field_get_id_from_name( $p_field_name, $p_truncated_length = nul
*
* The ids will be sorted based on the sequence number associated with the binding
* @param int $p_project_id project id
* @param int $p_user_id user id
* @return array
* @access public
*/
function custom_field_get_linked_ids( $p_project_id = ALL_PROJECTS ) {
function custom_field_get_linked_ids( $p_project_id = ALL_PROJECTS, $p_user_id = null ) {
global $g_cache_cf_linked;

if( !isset( $g_cache_cf_linked[$p_project_id] ) ) {

$p_project_id = (int) $p_project_id;

if( ALL_PROJECTS == $p_project_id ) {
$t_user_id = auth_get_current_user_id();
if( $p_user_id === null ) {
$t_user_id = auth_get_current_user_id();
} else {
$t_user_id = (int) $p_user_id;
}
$t_pub = VS_PUBLIC;
$t_priv = VS_PRIVATE;

Expand Down
2 changes: 1 addition & 1 deletion core/custom_function_api.php
Expand Up @@ -287,7 +287,7 @@ function custom_function_default_get_columns_to_view( $p_columns_target = COLUMN
$t_columns = config_get( 'print_issues_page_columns', columns_get_default( 'print_issues_page' ), $p_user_id, $t_project_id );
}

$t_columns = columns_remove_invalid( $t_columns, columns_get_all( $t_project_id ) );
$t_columns = columns_remove_invalid( $t_columns, columns_get_all( $t_project_id, $p_user_id ) );

return $t_columns;
}
Expand Down
97 changes: 30 additions & 67 deletions core/filter_api.php
Expand Up @@ -457,10 +457,12 @@ function filter_offset( $p_page_number, $p_per_page ) {
* Make sure that our filters are entirely correct and complete (it is possible that they are not).
* We need to do this to cover cases where we don't have complete control over the filters given.s
* @param array $p_filter_arr
* @param int $p_user_id Optional user id to verify current filter against (normally current user)
* @param int $p_project_id Optional project id to verify current filter against (normally current project)
* @return mixed
* @todo function needs to be abstracted
*/
function filter_ensure_valid_filter( $p_filter_arr ) {
function filter_ensure_valid_filter( $p_filter_arr, $p_user_id = null, $p_project_id = null ) {
# extend current filter to add information passed via POST
if( !isset( $p_filter_arr['_version'] ) ) {
$p_filter_arr['_version'] = config_get_global( 'cookie_version' );
Expand All @@ -474,45 +476,32 @@ function filter_ensure_valid_filter( $p_filter_arr ) {
$p_filter_arr['_view_type'] = gpc_get_string( 'view_type', 'simple' );
}
if( !isset( $p_filter_arr[FILTER_PROPERTY_ISSUES_PER_PAGE] ) ) {
$p_filter_arr[FILTER_PROPERTY_ISSUES_PER_PAGE] = gpc_get_int( FILTER_PROPERTY_ISSUES_PER_PAGE, config_get( 'default_limit_view' ) );
$p_filter_arr[FILTER_PROPERTY_ISSUES_PER_PAGE] = gpc_get_int( FILTER_PROPERTY_ISSUES_PER_PAGE, config_get( 'default_limit_view', null, $p_user_id, $p_project_id ) );
}
if( !isset( $p_filter_arr[FILTER_PROPERTY_HIGHLIGHT_CHANGED] ) ) {
$p_filter_arr[FILTER_PROPERTY_HIGHLIGHT_CHANGED] = config_get( 'default_show_changed' );
$p_filter_arr[FILTER_PROPERTY_HIGHLIGHT_CHANGED] = config_get( 'default_show_changed', null, $p_user_id, $p_project_id );
}
if( !isset( $p_filter_arr[FILTER_PROPERTY_STICKY] ) ) {
$p_filter_arr[FILTER_PROPERTY_STICKY] = gpc_string_to_bool( config_get( 'show_sticky_issues' ) );
$p_filter_arr[FILTER_PROPERTY_STICKY] = gpc_string_to_bool( config_get( 'show_sticky_issues', null, $p_user_id, $p_project_id ) );
}
if( !isset( $p_filter_arr[FILTER_PROPERTY_SORT_FIELD_NAME] ) ) {
$p_filter_arr[FILTER_PROPERTY_SORT_FIELD_NAME] = "last_updated";
}
if( !isset( $p_filter_arr[FILTER_PROPERTY_SORT_DIRECTION] ) ) {
$p_filter_arr[FILTER_PROPERTY_SORT_DIRECTION] = "DESC";
}

if( !isset( $p_filter_arr[FILTER_PROPERTY_PLATFORM] ) ) {
$p_filter_arr[FILTER_PROPERTY_PLATFORM] = array(
0 => META_FILTER_ANY,
);
$p_filter_arr[FILTER_PROPERTY_PLATFORM] = array( 0 => META_FILTER_ANY, );
}

if( !isset( $p_filter_arr[FILTER_PROPERTY_OS] ) ) {
$p_filter_arr[FILTER_PROPERTY_OS] = array(
0 => META_FILTER_ANY,
);
$p_filter_arr[FILTER_PROPERTY_OS] = array( 0 => META_FILTER_ANY, );
}

if( !isset( $p_filter_arr[FILTER_PROPERTY_OS_BUILD] ) ) {
$p_filter_arr[FILTER_PROPERTY_OS_BUILD] = array(
0 => META_FILTER_ANY,
);
$p_filter_arr[FILTER_PROPERTY_OS_BUILD] = array( 0 => META_FILTER_ANY, );
}

if( !isset( $p_filter_arr[FILTER_PROPERTY_PROJECT_ID] ) ) {
$p_filter_arr[FILTER_PROPERTY_PROJECT_ID] = array(
0 => META_FILTER_CURRENT,
);
$p_filter_arr[FILTER_PROPERTY_PROJECT_ID] = array( 0 => META_FILTER_CURRENT, );
}

if( !isset( $p_filter_arr[FILTER_PROPERTY_START_MONTH] ) ) {
$p_filter_arr[FILTER_PROPERTY_START_MONTH] = gpc_get_string( FILTER_PROPERTY_START_MONTH, date( 'm' ) );
}
Expand Down Expand Up @@ -612,7 +601,7 @@ function filter_ensure_valid_filter( $p_filter_arr ) {
}

# validate sorting
$t_fields = helper_get_columns_to_view();
$t_fields = helper_get_columns_to_view( COLUMNS_TARGET_VIEW_PAGE, true, $p_user_id, $p_project_id);
$t_n_fields = count( $t_fields );
for( $i = 0;$i < $t_n_fields;$i++ ) {
if( isset( $t_fields[$i] ) && in_array( $t_fields[$i], array( 'selection', 'edit', 'attachment' ) ) ) {
Expand Down Expand Up @@ -673,23 +662,17 @@ function filter_ensure_valid_filter( $p_filter_arr ) {
if( !isset( $p_filter_arr[$t_multi_field_name] ) ) {
if( FILTER_PROPERTY_HIDE_STATUS == $t_multi_field_name ) {
$p_filter_arr[$t_multi_field_name] = array(
config_get( 'hide_status_default' ),
config_get( 'hide_status_default', null, $p_user_id, $p_project_id ),
);
}
else if( 'custom_fields' == $t_multi_field_name ) {
$p_filter_arr[$t_multi_field_name] = array(
$f_custom_fields_data,
);
$p_filter_arr[$t_multi_field_name] = array( $f_custom_fields_data, );
} else {
$p_filter_arr[$t_multi_field_name] = array(
META_FILTER_ANY,
);
$p_filter_arr[$t_multi_field_name] = array( META_FILTER_ANY, );
}
} else {
if( !is_array( $p_filter_arr[$t_multi_field_name] ) ) {
$p_filter_arr[$t_multi_field_name] = array(
$p_filter_arr[$t_multi_field_name],
);
$p_filter_arr[$t_multi_field_name] = array( $p_filter_arr[$t_multi_field_name], );
}
$t_checked_array = array();
foreach( $p_filter_arr[$t_multi_field_name] as $t_filter_value ) {
Expand Down Expand Up @@ -754,40 +737,18 @@ function filter_get_default() {
$t_default_show_changed = config_get( 'default_show_changed' );

$t_filter = array(
FILTER_PROPERTY_CATEGORY_ID => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_SEVERITY => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_STATUS => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_CATEGORY_ID => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_SEVERITY => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_STATUS => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_HIGHLIGHT_CHANGED => $t_default_show_changed,
FILTER_PROPERTY_REPORTER_ID => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_HANDLER_ID => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_PROJECT_ID => array(
'0' => META_FILTER_CURRENT,
),
FILTER_PROPERTY_RESOLUTION => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_BUILD => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_VERSION => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_HIDE_STATUS => array(
'0' => $t_hide_status_default,
),
FILTER_PROPERTY_MONITOR_USER_ID => array(
'0' => META_FILTER_ANY,
),
FILTER_PROPERTY_REPORTER_ID => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_HANDLER_ID => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_PROJECT_ID => array( '0' => META_FILTER_CURRENT, ),
FILTER_PROPERTY_RESOLUTION => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_BUILD => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_VERSION => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_HIDE_STATUS => array( '0' => $t_hide_status_default, ),
FILTER_PROPERTY_MONITOR_USER_ID => array( '0' => META_FILTER_ANY, ),
FILTER_PROPERTY_SORT_FIELD_NAME => 'last_updated',
FILTER_PROPERTY_SORT_DIRECTION => 'DESC',
FILTER_PROPERTY_ISSUES_PER_PAGE => config_get( 'default_limit_view' ),
Expand All @@ -799,10 +760,12 @@ function filter_get_default() {
/**
* De-serialize filter string
* @param string $p_serialized_filter serialized filter
* @param int $p_user_id user id
* @param int $p_project_id project id
* @return mixed filter array
* @see filter_ensure_valid_filter
*/
function filter_deserialize( $p_serialized_filter ) {
function filter_deserialize( $p_serialized_filter, $p_user_id = null, $p_project_id = null ) {
if( is_blank( $p_serialized_filter ) ) {
return false;
}
Expand All @@ -826,7 +789,7 @@ function filter_deserialize( $p_serialized_filter ) {
if( $t_filter_array['_version'] != config_get_global( 'cookie_version' ) ) {

# if the version is not new enough, update it using defaults
return filter_ensure_valid_filter( $t_filter_array );
return filter_ensure_valid_filter( $t_filter_array, $p_user_id, $p_project_id );
}

return $t_filter_array;
Expand Down
4 changes: 2 additions & 2 deletions core/helper_api.php
Expand Up @@ -459,11 +459,11 @@ function helper_get_columns_to_view( $p_columns_target = COLUMNS_TARGET_VIEW_PAG
$t_keys_to_remove[] = 'attachment';
}

if( $t_current_project_id != ALL_PROJECTS && !access_has_project_level( config_get( 'view_handler_threshold', null, $p_user_id, $t_current_project_id ), $t_current_project_id ) ) {
if( $t_current_project_id != ALL_PROJECTS && !access_has_project_level( config_get( 'view_handler_threshold', null, $p_user_id, $t_current_project_id ), $t_current_project_id, $p_user_id ) ) {
$t_keys_to_remove[] = 'handler_id';
}

if( $t_current_project_id != ALL_PROJECTS && !access_has_project_level( config_get( 'roadmap_view_threshold', null, $p_user_id, $t_current_project_id ), $t_current_project_id ) ) {
if( $t_current_project_id != ALL_PROJECTS && !access_has_project_level( config_get( 'roadmap_view_threshold', null, $p_user_id, $t_current_project_id ), $t_current_project_id, $p_user_id ) ) {
$t_keys_to_remove[] = 'target_version';
}

Expand Down
2 changes: 1 addition & 1 deletion core/install_helper_functions_api.php
Expand Up @@ -321,7 +321,7 @@ function install_stored_filter_migrate() {
$t_query = "SELECT * FROM {filters}";
$t_result = db_query( $t_query );
while( $t_row = db_fetch_array( $t_result ) ) {
$t_filter_arr = filter_deserialize( $t_row['filter_string'] );
$t_filter_arr = filter_deserialize( $t_row['filter_string'], $t_row['user_id'], $t_row['project_id'] );
foreach( $t_filter_fields AS $t_old=>$t_new ) {
if ( isset( $t_filter_arr[$t_old] ) ) {
$t_value = $t_filter_arr[$t_old];
Expand Down

1 comment on commit 17298ed

@atrol
Copy link
Member

@atrol atrol commented on 17298ed Apr 8, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a better performance you might consider calling the changed functions with parameters whenever the needed values are already available.

e.g. instead of calling columns_get_all() in config_columns_set.php call columns_get_all( $t_project_id, $t_user_id );
This will avoid double calls of helper_get_current_project() and auth_get_current_user_id().

If there remain only calls with all parameters, the === null stuff can be removed.

Please sign in to comment.