Skip to content

Commit

Permalink
New API current_user_set()
Browse files Browse the repository at this point in the history
The function sets the $g_cache_current_user_id global variable, and
clears the user preferences cache ($g_cache_current_user_pref) if
necessary.

Using this new API to set the current user as opposed to working with
$g_cache_current_user_id makes sure we always get the correct data from
get_user_pref().

Resolves an issue where the order (as well as the number) of bugnotes in
email notifications is based on the user who triggered the action,
instead of the email's recipient.

Fixes #17925 (porting 559ba4e)
  • Loading branch information
dregad committed Dec 14, 2014
1 parent b751f3b commit 257c1fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
10 changes: 5 additions & 5 deletions core/authentication_api.php
Expand Up @@ -350,7 +350,7 @@ function auth_attempt_script_login( $p_username, $p_password = null ) {
$g_script_login_cookie = $t_user['cookie_string'];

# cache user id for future reference
$g_cache_current_user_id = $t_user_id;
current_user_set( $t_user_id );

return true;
}
Expand All @@ -366,7 +366,7 @@ function auth_logout() {

# clear cached userid
user_clear_cache( $g_cache_current_user_id );
$g_cache_current_user_id = null;
current_user_set( null );
$g_cache_cookie_valid = null;

# clear cookies, if they were set
Expand Down Expand Up @@ -651,7 +651,7 @@ function auth_get_current_user_cookie( $p_login_anonymous = true ) {
$t_cookie = $t_row['cookie_string'];

$g_cache_anonymous_user_cookie_string = $t_cookie;
$g_cache_current_user_id = $t_row['id'];
current_user_set( $t_row['id'] );
}
}
} else {
Expand Down Expand Up @@ -833,7 +833,7 @@ function auth_get_current_user_id() {

if( $t_result = user_search_cache( 'cookie_string', $t_cookie_string ) ) {
$t_user_id = (int)$t_result['id'];
$g_cache_current_user_id = $t_user_id;
current_user_set( $t_user_id );
return $t_user_id;
}

Expand All @@ -851,7 +851,7 @@ function auth_get_current_user_id() {
exit();
}

$g_cache_current_user_id = $t_user_id;
current_user_set( $t_user_id );

return $t_user_id;
}
Expand Down
24 changes: 24 additions & 0 deletions core/current_user_api.php
Expand Up @@ -44,6 +44,30 @@
require_api( 'user_pref_api.php' );
require_api( 'utility_api.php' );

/**
* Sets the current user
*
* @param integer $p_user_id Id to set as current user
* @return Old current user id
* @access public
*/
function current_user_set( $p_user_id ) {
global $g_cache_current_user_id;
global $g_cache_current_user_pref;

if( $p_user_id == $g_cache_current_user_id ) {
return $p_user_id;
}

$t_old_current = $g_cache_current_user_id;
$g_cache_current_user_id = $p_user_id;

# Clear current user preferences cache
$g_cache_current_user_pref = array();

return $t_old_current;
}

# ## Current User API ###
# Wrappers around the User API that pass in the logged-in user for you
/**
Expand Down
6 changes: 2 additions & 4 deletions core/email_api.php
Expand Up @@ -1315,9 +1315,7 @@ function email_format_attribute( array $p_visible_bug_data, $p_attribute_id ) {
function email_build_visible_bug_data( $p_user_id, $p_bug_id, $p_message_id ) {
# Override current user with user to construct bug data for.
# This is to make sure that APIs that check against current user (e.g. relationship) work correctly.
global $g_cache_current_user_id;
$t_current_user_id = $g_cache_current_user_id;
$g_cache_current_user_id = $p_user_id;
$t_current_user_id = current_user_set( $p_user_id );

$t_project_id = bug_get_field( $p_bug_id, 'project_id' );
$t_user_access_level = user_get_access_level( $p_user_id, $t_project_id );
Expand Down Expand Up @@ -1393,7 +1391,7 @@ function email_build_visible_bug_data( $p_user_id, $p_bug_id, $p_message_id ) {

$t_bug_data['relations'] = relationship_get_summary_text( $p_bug_id );

$g_cache_current_user_id = $t_current_user_id;
current_user_set( $t_current_user_id );

return $t_bug_data;
}

0 comments on commit 257c1fe

Please sign in to comment.