Skip to content

Commit

Permalink
update MantisUser Class
Browse files Browse the repository at this point in the history
  • Loading branch information
mantis committed Jul 30, 2011
1 parent 0160cff commit 9cf3617
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
53 changes: 53 additions & 0 deletions core/classes/MantisUser.class.php
Expand Up @@ -54,7 +54,60 @@ public function Exists() {
public function getID() {
return $this->user_id;
}

public static function getByUserName($p_name) {
$t_row = GetFromDatabase( 'username', $p_name );
if ( $t_row === null ) {
throw new MantisBT\Exception\User_By_UserName_Not_Found( $p_name );
}

$t_user = new MantisUser();
$t_user->loadrow( $t_row );

return $t_user;
}

public static function getByUserID($p_user_id) {
$t_row = self::GetFromDatabase( 'id', $p_user_id );
if ( $t_row === null ) {
throw new MantisBT\Exception\User_By_UserID_Not_Found( $p_user_id );
}

$t_user = new MantisUser();
$t_user->loadrow( $t_row );

return $t_user;
}

# Cache a user row if necessary and return the cached copy
# If the second parameter is true (default), trigger an error
# if the user can't be found. If the second parameter is
# false, return false if the user can't be found.
private static function GetFromDatabase( $p_field, $p_value ) {
switch( $p_field ) {
case 'id':
$t_type = '%d';
break;
case 'username':
case 'realname':
case 'email':
$t_type = '%s';
break;
default:
throw new MantisBT\Exception\Generic();
}
$query = "SELECT * FROM {user} WHERE " . $p_field . '=' . $t_type;
$result = db_query_bound( $query, array( $p_value ) );

$row = db_fetch_array( $result );

if ( $row ) {
return $row;
} else {
return null;
}
}

# --------------------
# Cache a user row if necessary and return the cached copy
# If the second parameter is true (default), trigger an error
Expand Down
21 changes: 8 additions & 13 deletions core/user_api.php
Expand Up @@ -123,7 +123,7 @@ function user_search_cache( $p_field, $p_value ) {
* and because if the user does exist the data may well be wanted
*/
function user_exists( $p_user_id ) {
$t_user = new MantisUser($p_user_id);
$t_user = MantisUser::getByUserID( $p_user_id );
if( false == $t_user->exists() ) {
return false;
} else {
Expand Down Expand Up @@ -570,7 +570,7 @@ function user_get_row_by_name( $p_username ) {
* return a user row
*/
function user_get_row( $p_user_id ) {
$t_user = new MantisUser($p_user_id);
$t_user = MantisUser::getByUserID( $p_user_id );
return $t_user->ToArray();
}

Expand All @@ -583,7 +583,7 @@ function user_get_field( $p_user_id, $p_field_name ) {
return '@null@';
}

$t_user = new MantisUser($p_user_id);
$t_user = MantisUser::getByUserID( $p_user_id );
$row = $t_user->ToArray();

if( isset( $row[$p_field_name] ) ) {
Expand Down Expand Up @@ -630,7 +630,7 @@ function user_get_realname( $p_user_id ) {
* if show_realname is set and real name is not empty, return it instead
*/
function user_get_name( $p_user_id ) {
$t_user = new MantisUser($p_user_id);
$t_user = MantisUser::getByUserID( $p_user_id );
$row = $t_user->user_cache_row( $p_user_id );

if( false == $row ) {
Expand Down Expand Up @@ -1069,9 +1069,7 @@ function user_update_last_visit( $p_user_id ) {
* This function is only called from the login.php script
*/
function user_increment_login_count( $p_user_id ) {
$query = "UPDATE {user}
SET login_count=login_count+1
WHERE id=%d";
$query = "UPDATE {user} SET login_count=login_count+1 WHERE id=%d";

db_query_bound( $query, array( $p_user_id ) );

Expand All @@ -1085,8 +1083,7 @@ function user_increment_login_count( $p_user_id ) {
* Reset to zero the failed login attempts
*/
function user_reset_failed_login_count_to_zero( $p_user_id ) {
$query = "UPDATE {user} SET failed_login_count=0
WHERE id=%d";
$query = "UPDATE {user} SET failed_login_count=0 WHERE id=%d";
db_query_bound( $query, array( $p_user_id ) );

user_clear_cache( $p_user_id );
Expand All @@ -1098,8 +1095,7 @@ function user_reset_failed_login_count_to_zero( $p_user_id ) {
* Increment the failed login count by 1
*/
function user_increment_failed_login_count( $p_user_id ) {
$query = "UPDATE {user} SET failed_login_count=failed_login_count+1
WHERE id=%d";
$query = "UPDATE {user} SET failed_login_count=failed_login_count+1 WHERE id=%d";
db_query_bound( $query, array( $p_user_id ) );

user_clear_cache( $p_user_id );
Expand All @@ -1111,8 +1107,7 @@ function user_increment_failed_login_count( $p_user_id ) {
* Reset to zero the 'lost password' in progress attempts
*/
function user_reset_lost_password_in_progress_count_to_zero( $p_user_id ) {
$query = "UPDATE {user} SET lost_password_request_count=0
WHERE id=%d";
$query = "UPDATE {user} SET lost_password_request_count=0 WHERE id=%d";
db_query_bound( $query, array( $p_user_id ) );

user_clear_cache( $p_user_id );
Expand Down

0 comments on commit 9cf3617

Please sign in to comment.