# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): Marc Schumann <wurblzap@gmail.com>
+# Frédéric Buclin <LpSolit@gmail.com>
use strict;
use lib ".";
use Bugzilla::Constants;
use Bugzilla::Util;
-Bugzilla->login(LOGIN_REQUIRED);
+my $user = Bugzilla->login(LOGIN_REQUIRED);
my $cgi = Bugzilla->cgi;
my $template = Bugzilla->template;
my $dbh = Bugzilla->dbh;
-my $user = Bugzilla->user;
my $userid = $user->id;
my $editusers = $user->in_group('editusers');
# Reject access if there is no sense in continuing.
$editusers
- || Bugzilla->user->can_bless()
+ || $user->can_bless()
|| ThrowUserError("auth_failure", {group => "editusers",
reason => "cant_bless",
action => "edit",
object => "users"});
-print Bugzilla->cgi->header();
+print $cgi->header();
# Common CGI params
-my $action = $cgi->param('action') || 'search';
-my $login = $cgi->param('login');
-my $password = $cgi->param('password');
-my $groupid = $cgi->param('groupid');
-my $otherUser = new Bugzilla::User($cgi->param('userid'));
-my $realname = trim($cgi->param('name') || '');
-my $disabledtext = trim($cgi->param('disabledtext') || '');
-
-# Directly from common CGI params derived values
-my $otherUserID = $otherUser->id();
+my $action = $cgi->param('action') || 'search';
+my $otherUserID = $cgi->param('userid');
+my $otherUserLogin = $cgi->param('user');
# Prefill template vars with data used in all or nearly all templates
$vars->{'editusers'} = $editusers;
my $matchstr = $cgi->param('matchstr');
my $matchtype = $cgi->param('matchtype');
my $grouprestrict = $cgi->param('grouprestrict') || '0';
+ my $groupid = $cgi->param('groupid');
my $query = 'SELECT DISTINCT userid, login_name, realname, disabledtext ' .
'FROM profiles';
my @bindValues;
my $nextCondition;
my $visibleGroups;
+ # If a group ID is given, make sure it is a valid one.
+ if ($grouprestrict) {
+ (detaint_natural($groupid) && GroupIdToName($groupid))
+ || ThrowUserError('invalid_group_ID');
+ }
+
if (!$editusers && Param('usevisibilitygroups')) {
# Show only users in visible groups.
$visibleGroups = visibleGroupsAsString();
'AND ugm.group_id = ?';
# We can trick_taint because we use the value in a SELECT only,
# using a placeholder.
- trick_taint($groupid);
push(@bindValues, $groupid);
}
$query .= ' ORDER BY profiles.login_name';
action => "add",
object => "users"});
+ my $login = $cgi->param('login');
+ my $password = $cgi->param('password');
+ my $realname = trim($cgi->param('name') || '');
+ my $disabledtext = trim($cgi->param('disabledtext') || '');
+
# Lock tables during the check+creation session.
$dbh->bz_lock_tables('profiles WRITE',
'profiles_activity WRITE',
###########################################################################
} elsif ($action eq 'edit') {
- $otherUser
- || ThrowCodeError('invalid_user_id', {'userid' => $cgi->param('userid')});
+ my $otherUser = check_user($otherUserID, $otherUserLogin);
+ $otherUserID = $otherUser->id;
$editusers || canSeeUser($otherUserID)
|| ThrowUserError('auth_failure', {reason => "not_visible",
###########################################################################
} elsif ($action eq 'update') {
- $otherUser
- || ThrowCodeError('invalid_user_id', {'userid' => $cgi->param('userid')});
+ my $otherUser = check_user($otherUserID, $otherUserLogin);
+ $otherUserID = $otherUser->id;
+
my $logoutNeeded = 0;
my @changedFields;
# Cleanups
my $loginold = $cgi->param('loginold') || '';
my $realnameold = $cgi->param('nameold') || '';
- my $password = $cgi->param('password') || '';
my $disabledtextold = $cgi->param('disabledtextold') || '';
+ my $login = $cgi->param('login');
+ my $password = $cgi->param('password');
+ my $realname = trim($cgi->param('name') || '');
+ my $disabledtext = trim($cgi->param('disabledtext') || '');
+
# Update profiles table entry; silently skip doing this if the user
# is not authorized.
if ($editusers) {
}
if (@changedFields) {
push (@values, $otherUserID);
- $logoutNeeded && Bugzilla->logout_user_by_id($otherUserID);
+ $logoutNeeded && Bugzilla->logout_user($otherUser);
$dbh->do('UPDATE profiles SET ' .
join(' = ?,', @changedFields).' = ? ' .
'WHERE userid = ?',
###########################################################################
} elsif ($action eq 'del') {
- $otherUser
- || ThrowCodeError('invalid_user_id', {'userid' => $cgi->param('userid')});
+ my $otherUser = check_user($otherUserID, $otherUserLogin);
+ $otherUserID = $otherUser->id;
Param('allowuserdeletion') || ThrowUserError('users_deletion_disabled');
$editusers || ThrowUserError('auth_failure', {group => "editusers",
###########################################################################
} elsif ($action eq 'delete') {
- $otherUser
- || ThrowCodeError('invalid_user_id', {'userid' => $cgi->param('userid')});
- my $otherUserLogin = $otherUser->login();
+ my $otherUser = check_user($otherUserID, $otherUserLogin);
+ $otherUserID = $otherUser->id;
# Cache for user accounts.
my %usercache = (0 => new Bugzilla::User());
@{$otherUser->product_responsibilities()}
&& ThrowUserError('user_has_responsibility');
- Bugzilla->logout_user_by_id($otherUserID);
+ Bugzilla->logout_user($otherUser);
# Get the timestamp for LogActivityEntry.
my $timestamp = $dbh->selectrow_array('SELECT NOW()');
$dbh->bz_unlock_tables();
$vars->{'message'} = 'account_deleted';
- $vars->{'otheruser'}{'login'} = $otherUserLogin;
+ $vars->{'otheruser'}{'login'} = $otherUser->login;
$vars->{'restrictablegroups'} = $user->bless_groups();
$template->process('admin/users/search.html.tmpl', $vars)
|| ThrowTemplateError($template->error());
# Helpers
###########################################################################
+# Try to build a user object using its ID, else its login name, and throw
+# an error if the user does not exist.
+sub check_user {
+ my ($otherUserID, $otherUserLogin) = @_;
+
+ my $otherUser;
+ my $vars = {};
+
+ if ($otherUserID) {
+ $otherUser = Bugzilla::User->new($otherUserID);
+ $vars->{'user_id'} = $otherUserID;
+ }
+ elsif ($otherUserLogin) {
+ $otherUser = Bugzilla::User->new_from_login($otherUserLogin);
+ $vars->{'user_login'} = $otherUserLogin;
+ }
+ ($otherUser && $otherUser->id) || ThrowCodeError('invalid_user', $vars);
+
+ return $otherUser;
+}
+
# Copy incoming list selection values from CGI params to template variables.
sub mirrorListSelectionValues {
if (defined($cgi->param('matchtype'))) {