0 ) { return db_result( $result ); } else { return false; } } # -------------------- # return the descriptor holding all the info from the project user list # for the specified project function project_get_local_user_rows( $p_project_id ) { $c_project_id = db_prepare_int( $p_project_id ); $t_project_user_list_table = config_get( 'mantis_project_user_list_table' ); $query = "SELECT * FROM $t_project_user_list_table WHERE project_id='$c_project_id'"; $result = db_query( $query ); $t_user_rows = array(); $t_row_count = db_num_rows( $result ); for ( $i=0 ; $i < $t_row_count ; $i++ ) { array_push( $t_user_rows, db_fetch_array( $result ) ); } return $t_user_rows; } # -------------------- # Return an array of info about users who have access to the the given project # For each user we have 'id', 'username', and 'access_level' (overall access level) # If the second parameter is given, return only users with an access level # higher than the given value. # if the first parameter is given as 'ALL_PROJECTS', return the global access level (without # any reference to the specific project function project_get_all_user_rows( $p_project_id = ALL_PROJECTS, $p_access_level = ANYBODY ) { $c_project_id = db_prepare_int( $p_project_id ); # Optimization when access_level is NOBODY if ( NOBODY == $p_access_level ) { return array(); } $t_user_table = config_get( 'mantis_user_table' ); $t_project_user_list_table = config_get( 'mantis_project_user_list_table' ); $t_project_table = config_get( 'mantis_project_table' ); $t_global_access_level = $p_access_level; if ( $c_project_id != ALL_PROJECTS ) { # looking for specific project if ( VS_PRIVATE == project_get_field( $p_project_id, 'view_state' ) ) { # @@@ (thraxisp) this is probably more complex than it needs to be # When a new project is created, those who meet 'private_project_threshold' are added # automatically, but don't have an entry in project_user_list_table. # if they did, you would not have to add global levels. $t_private_project_threshold = config_get( 'private_project_threshold' ); if ( is_array( $t_private_project_threshold ) ) { if ( is_array( $p_access_level ) ) { # both private threshold and request are arrays, use intersection $t_global_access_level = array_intersect( $p_access_level, $t_private_project_threshold ); } else { # private threshold is an array, but request is a number, use values in threshold higher than request $t_global_access_level = array(); foreach ( $t_private_project_threshold as $t_threshold ) { if ( $p_access_level <= $t_threshold ) { $t_global_access_level[] = $t_threshold; } } } } else { if ( is_array( $p_access_level ) ) { # private threshold is a number, but request is an array, use values in request higher than threshold $t_global_access_level = array(); foreach ( $p_access_level as $t_threshold ) { if ( $t_threshold >= $t_private_project_threshold ) { $t_global_access_level[] = $t_threshold; } } } else { # both private threshold and request are numbers, use maximum $t_global_access_level = max( $p_access_level, $t_private_project_threshold ); } } } } $t_project_clause = ( $c_project_id != ALL_PROJECTS ) ? ' AND p.id = ' . $c_project_id : ''; if ( is_array( $t_global_access_level ) ) { if ( 0 == count( $t_global_access_level ) ) { $t_global_access_clause = ">= " . NOBODY . " "; } else if ( 1 == count( $t_global_access_level ) ) { $t_global_access_clause = "= " . array_shift( $t_global_access_level ) . " "; } else { $t_global_access_clause = "IN (" . implode( ',', $t_global_access_level ) . ")"; } } else { $t_global_access_clause = ">= $t_global_access_level "; } $t_on = ON; $t_adm = ADMINISTRATOR; $t_users = array(); $query = "SELECT id, username, realname, access_level FROM $t_user_table WHERE enabled = $t_on AND access_level $t_global_access_clause"; $result = db_query( $query ); $t_row_count = db_num_rows( $result ); for ( $i=0 ; $i < $t_row_count ; $i++ ) { $row = db_fetch_array( $result ); $t_users[$row['id']] = $row; } if( $c_project_id != ALL_PROJECTS ) { # Get the project overrides $query = "SELECT u.id, u.username, u.realname, l.access_level FROM $t_project_user_list_table l, $t_user_table u WHERE l.user_id = u.id AND u.enabled = $t_on AND l.project_id = $c_project_id"; $result = db_query( $query ); $t_row_count = db_num_rows( $result ); for ( $i=0 ; $i < $t_row_count ; $i++ ) { $row = db_fetch_array( $result ); if ( is_array( $p_access_level ) ) { $t_keep = in_array( $row['access_level'], $p_access_level ); } else { $t_keep = $row['access_level'] >= $p_access_level; } if ( $t_keep ) { $t_users[$row['id']] = $row; } else { # If user's overridden level is lower than required, so remove # them from the list if they were previously there unset( $t_users[$row['id']] ); } } } return array_values( $t_users ); } #=================================== # Data Modification #=================================== # -------------------- # add user with the specified access level to a project function project_add_user( $p_project_id, $p_user_id, $p_access_level ) { $t_project_user_list_table = config_get( 'mantis_project_user_list_table' ); $c_project_id = db_prepare_int( $p_project_id ); $c_user_id = db_prepare_int( $p_user_id ); $c_access_level = db_prepare_int( $p_access_level ); if ( DEFAULT_ACCESS_LEVEL == $p_access_level ) { # Default access level for this user $c_access_level = db_prepare_int( user_get_access_level ( $p_user_id ) ); } $query = "INSERT INTO $t_project_user_list_table ( project_id, user_id, access_level ) VALUES ( '$c_project_id', '$c_user_id', '$c_access_level')"; db_query( $query ); # db_query errors on failure so: return true; } # -------------------- # update entry # must make sure entry exists beforehand function project_update_user_access( $p_project_id, $p_user_id, $p_access_level ) { $t_project_user_list_table = config_get( 'mantis_project_user_list_table' ); $c_project_id = db_prepare_int( $p_project_id ); $c_user_id = db_prepare_int( $p_user_id ); $c_access_level = db_prepare_int( $p_access_level ); $query = "UPDATE $t_project_user_list_table SET access_level='$c_access_level' WHERE project_id='$c_project_id' AND user_id='$c_user_id'"; db_query( $query ); # db_query errors on failure so: return true; } # -------------------- # update or add the entry as appropriate # This function involves one more db query than project_update_user_acces() # or project_add_user() function project_set_user_access( $p_project_id, $p_user_id, $p_access_level ) { if ( project_includes_user( $p_project_id, $p_user_id ) ) { return project_update_user_access( $p_project_id, $p_user_id, $p_access_level ); } else { return project_add_user( $p_project_id, $p_user_id, $p_access_level ); } } # -------------------- # remove user from project function project_remove_user( $p_project_id, $p_user_id ) { $t_project_user_list_table = config_get( 'mantis_project_user_list_table' ); $c_project_id = db_prepare_int( $p_project_id ); $c_user_id = db_prepare_int( $p_user_id ); $query = "DELETE FROM $t_project_user_list_table WHERE project_id='$c_project_id' AND user_id='$c_user_id'"; db_query( $query ); # db_query errors on failure so: return true; } # -------------------- # delete all users from the project user list for a given project # this is useful when deleting or closing a project function project_remove_all_users( $p_project_id ) { $t_project_user_list_table = config_get( 'mantis_project_user_list_table' ); $c_project_id = db_prepare_int( $p_project_id ); $query = "DELETE FROM $t_project_user_list_table WHERE project_id='$c_project_id'"; db_query( $query ); # db_query errors on failure so: return true; } # -------------------- # Copy all users and their permissions from the source project to the # destination project function project_copy_users( $p_destination_id, $p_source_id ) { # Copy all users from current project over to another project $rows = project_get_local_user_rows( $p_source_id ); for ( $i = 0 ; $i < sizeof( $rows ) ; $i++ ) { extract( $rows[$i], EXTR_PREFIX_ALL, 'v' ); # if there is no duplicate then add a new entry # otherwise just update the access level for the existing entry if ( project_includes_user( $p_destination_id, $v_user_id ) ) { project_update_user_access( $p_destination_id, $v_user_id, $v_access_level ); } else { project_add_user( $p_destination_id, $v_user_id, $v_access_level ); } } } # @@@ Matthieu Dhallenne # Copy the configuration from the source project to the # destination project function project_copy_config( $p_destination_id, $p_source_id ) { $t_options = config_get_options( $p_source_id ); config_delete_project ($p_destination_id); while ( false <> ( $row = db_fetch_array( $t_options ) ) ) { $t_config = $row['config_id']; #@@@ debug @@@ echo("
".$t_config."
"); project_copy_option( $t_config, $p_destination_id, $p_source_id); } } # @@@ Matthieu Dhallenne # Copy an option from the source project to the # destination project function project_copy_option( $p_option, $p_destination_id, $p_source_id ) { $t_workflow = config_get( $p_option, null, null, $p_source_id ); # access required is only ADMINISTRATOR after the copy config_set($p_option, $t_workflow, NO_USER, $p_destination_id, ADMINISTRATOR); } # -------------------- # Delete all files associated with a project function project_delete_all_files( $p_project_id ) { file_delete_project_files( $p_project_id ); } #=================================== # Other #=================================== # -------------------- # Pads the project id with the appropriate number of zeros. function project_format_id( $p_project_id ) { $t_padding = config_get( 'display_project_padding' ); return( str_pad( $p_project_id, $t_padding, '0', STR_PAD_LEFT ) ); } # -------------------- # Return true if the file name identifier is unique, false otherwise function project_file_is_name_unique( $p_name ) { $t_file_table = config_get( 'mantis_project_file_table' ); $c_name = db_prepare_string( $p_name ); $query = "SELECT COUNT(*) FROM $t_file_table WHERE filename='$c_name'"; $result = db_query( $query ); $t_count = db_result( $result ); if ( $t_count > 0 ) { return false; } else { return true; } } ?>