Skip to content

Commit

Permalink
New User Pref API functions to clear project_default
Browse files Browse the repository at this point in the history
Issue #27144
  • Loading branch information
dregad committed Aug 17, 2020
1 parent 56bbac9 commit 5acff67
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions core/user_pref_api.php
Expand Up @@ -520,6 +520,64 @@ function user_pref_db_delete_user( $p_user_id ) {
db_query( $t_query, array( $p_user_id ) );
}

/**
* Sets project default to ALL_PROJECTS.
*
* @param integer $p_project_id A valid project identifier.
* @param array $p_users A list of users (empty = all users).
*
* @return void
*/
function user_pref_clear_project_default( $p_project_id, array $p_users = array() ) {
$t_query = new DbQuery( 'UPDATE {user_pref}'
. ' SET default_project = ' . ALL_PROJECTS
. ' WHERE default_project = :default'
);
$t_query->bind( 'default', (int)$p_project_id );
if( $p_users ) {
$t_query->append_sql( ' AND ' . $t_query->sql_in( 'user_id', 'users' ) );
$t_query->bind( 'users', $p_users );
}
$t_query->execute();
}

/**
* Sets project default to ALL_PROJECTS if current default is not valid.
*
* When users are removed from a project, the ones having that project as
* default but who are no longer authorized to access it, need to have their
* now-invalid preference updated.
*
* @param integer $p_project_id A valid project identifier.
* @param array $p_users A list of users (empty = all users).
*
* @return void
*/
function user_pref_clear_invalid_project_default( $p_project_id, array $p_users = array() ) {
# Get all users having the project as default
$t_query = new DbQuery( 'SELECT user_id FROM {user_pref} WHERE default_project = :default' );
$t_query->bind( 'default', (int)$p_project_id );
if( $p_users ) {
$t_query->append_sql( ' AND ' . $t_query->sql_in( 'user_id', 'users' ) );
$t_query->bind( 'users', $p_users );
}
$t_query->execute();

$t_users_having_project_as_default = array_column( $t_query->fetch_all(), 'user_id' );

# Users who can't access the project anymore must have the default cleared
$t_users_to_clear = array();
foreach( $t_users_having_project_as_default as $t_id ) {
if( access_get_project_level( $p_project_id, $t_id ) == ANYBODY ) {
$t_users_to_clear[] = $t_id;
}
}

if( !empty( $t_users_to_clear ) ) {
user_pref_clear_project_default( $p_project_id, $t_users_to_clear );
}
}

/**
* delete all preferences for a project for all users (part of deleting the project)
* returns true if the prefs were successfully deleted
Expand Down

0 comments on commit 5acff67

Please sign in to comment.