Commit fa9b63f5 authored by mkanat%kerio.com's avatar mkanat%kerio.com

Bug 283237: Move DBname_to_id out of globals.pl

Patch By Max Kanat-Alexander <mkanat@kerio.com> r=wurblzap, a=myk
parent 63dde600
...@@ -131,7 +131,7 @@ sub initBug { ...@@ -131,7 +131,7 @@ sub initBug {
} }
else { else {
if ($user_id =~ /^\@/) { if ($user_id =~ /^\@/) {
$user_id = &::DBname_to_id($user_id); $user_id = login_to_id($user_id);
} }
} }
......
...@@ -627,7 +627,7 @@ sub filterEmailGroup ($$$) { ...@@ -627,7 +627,7 @@ sub filterEmailGroup ($$$) {
# but the code that was here before I re-wrote it allows this), # but the code that was here before I re-wrote it allows this),
# then we do not have any preferences for them, so assume the # then we do not have any preferences for them, so assume the
# default preference is to receive all mail. # default preference is to receive all mail.
my $userid = DBname_to_id($user); my $userid = login_to_id($user);
if (!$userid) { if (!$userid) {
push(@recipients, $user); push(@recipients, $user);
next; next;
......
...@@ -449,7 +449,7 @@ sub modify { ...@@ -449,7 +449,7 @@ sub modify {
# Get the requestee, if any. # Get the requestee, if any.
my $requestee_id = "NULL"; my $requestee_id = "NULL";
if ($requestee_email) { if ($requestee_email) {
$requestee_id = &::DBname_to_id($requestee_email); $requestee_id = login_to_id($requestee_email);
$flag->{'requestee'} = new Bugzilla::User($requestee_id); $flag->{'requestee'} = new Bugzilla::User($requestee_id);
} }
...@@ -531,7 +531,7 @@ sub FormToNewFlags { ...@@ -531,7 +531,7 @@ sub FormToNewFlags {
if ($status eq "?") { if ($status eq "?") {
my $requestee = $data->{"requestee_type-$type_id"}; my $requestee = $data->{"requestee_type-$type_id"};
if ($requestee) { if ($requestee) {
my $requestee_id = &::DBname_to_id($requestee); my $requestee_id = login_to_id($requestee);
$flag->{'requestee'} = new Bugzilla::User($requestee_id); $flag->{'requestee'} = new Bugzilla::User($requestee_id);
} }
} }
......
...@@ -41,6 +41,7 @@ use Bugzilla::Error; ...@@ -41,6 +41,7 @@ use Bugzilla::Error;
use Bugzilla::Util; use Bugzilla::Util;
use Bugzilla::Constants; use Bugzilla::Constants;
use Bugzilla::Group; use Bugzilla::Group;
use Bugzilla::User;
use Date::Format; use Date::Format;
use Date::Parse; use Date::Parse;
...@@ -1414,7 +1415,7 @@ sub ListIDsForEmail { ...@@ -1414,7 +1415,7 @@ sub ListIDsForEmail {
if ($type eq 'anyexact') { if ($type eq 'anyexact') {
foreach my $w (split(/,/, $email)) { foreach my $w (split(/,/, $email)) {
$w = trim($w); $w = trim($w);
my $id = &::DBname_to_id($w); my $id = login_to_id($w);
if ($id > 0) { if ($id > 0) {
push(@list,$id) push(@list,$id)
} }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
# Bradley Baetz <bbaetz@acm.org> # Bradley Baetz <bbaetz@acm.org>
# Joel Peshkin <bugreport@peshkin.net> # Joel Peshkin <bugreport@peshkin.net>
# Byron Jones <bugzilla@glob.com.au> # Byron Jones <bugzilla@glob.com.au>
# Max Kanat-Alexander <mkanat@kerio.com>
################################################################################ ################################################################################
# Module Initialization # Module Initialization
...@@ -40,7 +41,9 @@ use Bugzilla::Constants; ...@@ -40,7 +41,9 @@ use Bugzilla::Constants;
use Bugzilla::Auth; use Bugzilla::Auth;
use base qw(Exporter); use base qw(Exporter);
@Bugzilla::User::EXPORT = qw(insert_new_user is_available_username); @Bugzilla::User::EXPORT = qw(insert_new_user is_available_username
login_to_id
);
################################################################################ ################################################################################
# Functions # Functions
...@@ -961,7 +964,7 @@ sub insert_new_user ($$) { ...@@ -961,7 +964,7 @@ sub insert_new_user ($$) {
sub is_available_username ($;$) { sub is_available_username ($;$) {
my ($username, $old_username) = @_; my ($username, $old_username) = @_;
if(&::DBname_to_id($username) != 0) { if(login_to_id($username) != 0) {
return 0; return 0;
} }
...@@ -992,6 +995,19 @@ sub is_available_username ($;$) { ...@@ -992,6 +995,19 @@ sub is_available_username ($;$) {
return 1; return 1;
} }
sub login_to_id ($) {
my ($login) = (@_);
my $dbh = Bugzilla->dbh;
my $user_id = $dbh->selectrow_array(
"SELECT userid FROM profiles WHERE login_name = ?", undef, $login);
# $user_id should be a positive integer, this makes Taint mode happy
if (defined $user_id && detaint_natural($user_id)) {
return $user_id;
} else {
return 0;
}
}
1; 1;
__END__ __END__
...@@ -1232,6 +1248,20 @@ Params: $username (scalar, string) - The full login name of the username ...@@ -1232,6 +1248,20 @@ Params: $username (scalar, string) - The full login name of the username
=back =back
=item C<login_to_id($login)>
Takes a login name of a Bugzilla user and changes that into a numeric
ID for that user. This ID can then be passed to Bugzilla::User::new to
create a new user.
If no valid user exists with that login name, then the function will return 0.
This function can also be used when you want to just find out the userid
of a user, but you don't want the full weight of Bugzilla::User.
However, consider using a Bugzilla::User object instead of this function
if you need more information about the user than just their ID.
=head1 SEE ALSO =head1 SEE ALSO
L<Bugzilla|Bugzilla> L<Bugzilla|Bugzilla>
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
# #
# You need to work with bug_email.pl the MIME::Parser installed. # You need to work with bug_email.pl the MIME::Parser installed.
# #
# $Id: bug_email.pl,v 1.24 2005/02/18 16:01:48 mkanat%kerio.com Exp $ # $Id: bug_email.pl,v 1.25 2005/02/24 23:42:48 mkanat%kerio.com Exp $
############################################################### ###############################################################
# 02/12/2000 (SML) # 02/12/2000 (SML)
...@@ -94,6 +94,7 @@ use lib "."; ...@@ -94,6 +94,7 @@ use lib ".";
use lib "../"; use lib "../";
use Bugzilla::Constants; use Bugzilla::Constants;
use Bugzilla::BugMail; use Bugzilla::BugMail;
use Bugzilla::User;
my @mailerrors = (); # Buffer for Errors in the mail my @mailerrors = (); # Buffer for Errors in the mail
my @mailwarnings = (); # Buffer for Warnings found in the mail my @mailwarnings = (); # Buffer for Warnings found in the mail
...@@ -920,7 +921,7 @@ $Control{'component'} = $Component; ...@@ -920,7 +921,7 @@ $Control{'component'} = $Component;
# otherwise, retrieve it from the database. # otherwise, retrieve it from the database.
if ( defined($Control{'assigned_to'}) if ( defined($Control{'assigned_to'})
&& $Control{'assigned_to'} !~ /^\s*$/ ) { && $Control{'assigned_to'} !~ /^\s*$/ ) {
$Control{'assigned_to'} = DBname_to_id($Control{'assigned_to'}); $Control{'assigned_to'} = login_to_id($Control{'assigned_to'});
} else { } else {
SendSQL("select initialowner from components, products where " . SendSQL("select initialowner from components, products where " .
" components.product_id=products.id AND products.name=" . " components.product_id=products.id AND products.name=" .
...@@ -940,7 +941,7 @@ if ( $Control{'assigned_to'} == 0 ) { ...@@ -940,7 +941,7 @@ if ( $Control{'assigned_to'} == 0 ) {
} }
$Control{'reporter'} = DBname_to_id($Control{'reporter'}); $Control{'reporter'} = login_to_id($Control{'reporter'});
if ( ! $Control{'reporter'} ) { if ( ! $Control{'reporter'} ) {
BugMailError( 1, "Could not resolve reporter !\n" ); BugMailError( 1, "Could not resolve reporter !\n" );
} }
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# #
# Nick Barnes, Ravenbrook Limited, 2004-04-01. # Nick Barnes, Ravenbrook Limited, 2004-04-01.
# #
# $Id: sendbugmail.pl,v 1.2 2004/11/20 12:35:17 jocuri%softhome.net Exp $ # $Id: sendbugmail.pl,v 1.3 2005/02/24 23:42:48 mkanat%kerio.com Exp $
# #
# Bugzilla email script for Bugzilla 2.17.4 and later. Invoke this to send # Bugzilla email script for Bugzilla 2.17.4 and later. Invoke this to send
# bugmail for a bug which has been changed directly in the database. # bugmail for a bug which has been changed directly in the database.
...@@ -18,6 +18,7 @@ use lib qw(.); ...@@ -18,6 +18,7 @@ use lib qw(.);
require "globals.pl"; require "globals.pl";
use Bugzilla::BugMail; use Bugzilla::BugMail;
use Bugzilla::User;
sub usage { sub usage {
print STDERR "Usage: $0 bug_id user_email\n"; print STDERR "Usage: $0 bug_id user_email\n";
...@@ -53,7 +54,7 @@ if ($changer !~ /$match/) { ...@@ -53,7 +54,7 @@ if ($changer !~ /$match/) {
print STDERR "Changer \"$changer\" doesn't match email regular expression.\n"; print STDERR "Changer \"$changer\" doesn't match email regular expression.\n";
usage(); usage();
} }
if(!DBname_to_id($changer)) { if(!login_to_id($changer)) {
print STDERR "\"$changer\" is not a login ID.\n"; print STDERR "\"$changer\" is not a login ID.\n";
usage(); usage();
} }
......
...@@ -35,6 +35,7 @@ use Bugzilla::Constants; ...@@ -35,6 +35,7 @@ use Bugzilla::Constants;
use Bugzilla::Config qw(:DEFAULT $datadir); use Bugzilla::Config qw(:DEFAULT $datadir);
use Bugzilla::Series; use Bugzilla::Series;
use Bugzilla::Util; use Bugzilla::Util;
use Bugzilla::User;
use vars qw($template $vars); use vars qw($template $vars);
...@@ -296,7 +297,7 @@ if ($action eq 'new') { ...@@ -296,7 +297,7 @@ if ($action eq 'new') {
exit; exit;
} }
my $initialownerid = DBname_to_id ($initialowner); my $initialownerid = login_to_id ($initialowner);
if (!$initialownerid) { if (!$initialownerid) {
ThrowUserError('component_need_valid_initialowner', ThrowUserError('component_need_valid_initialowner',
{'name' => $component}); {'name' => $component});
...@@ -304,7 +305,7 @@ if ($action eq 'new') { ...@@ -304,7 +305,7 @@ if ($action eq 'new') {
} }
my $initialqacontact = trim($cgi->param('initialqacontact') || ''); my $initialqacontact = trim($cgi->param('initialqacontact') || '');
my $initialqacontactid = DBname_to_id ($initialqacontact); my $initialqacontactid = login_to_id ($initialqacontact);
if (Param('useqacontact')) { if (Param('useqacontact')) {
if (!$initialqacontactid && $initialqacontact ne '') { if (!$initialqacontactid && $initialqacontact ne '') {
ThrowUserError('component_need_valid_initialqacontact', ThrowUserError('component_need_valid_initialqacontact',
...@@ -600,7 +601,7 @@ if ($action eq 'update') { ...@@ -600,7 +601,7 @@ if ($action eq 'update') {
if ($initialowner ne $initialownerold) { if ($initialowner ne $initialownerold) {
my $initialownerid = DBname_to_id($initialowner); my $initialownerid = login_to_id($initialowner);
unless ($initialownerid) { unless ($initialownerid) {
$dbh->bz_unlock_tables(UNLOCK_ABORT); $dbh->bz_unlock_tables(UNLOCK_ABORT);
ThrowUserError('component_need_valid_initialowner', ThrowUserError('component_need_valid_initialowner',
...@@ -618,7 +619,7 @@ if ($action eq 'update') { ...@@ -618,7 +619,7 @@ if ($action eq 'update') {
} }
if (Param('useqacontact') && $initialqacontact ne $initialqacontactold) { if (Param('useqacontact') && $initialqacontact ne $initialqacontactold) {
my $initialqacontactid = DBname_to_id($initialqacontact); my $initialqacontactid = login_to_id($initialqacontact);
if (!$initialqacontactid && $initialqacontact ne '') { if (!$initialqacontactid && $initialqacontact ne '') {
$dbh->bz_unlock_tables(UNLOCK_ABORT); $dbh->bz_unlock_tables(UNLOCK_ABORT);
ThrowUserError('component_need_valid_initialqacontact', ThrowUserError('component_need_valid_initialqacontact',
......
...@@ -536,7 +536,7 @@ if ($action eq 'del') { ...@@ -536,7 +536,7 @@ if ($action eq 'del') {
SendSQL("SELECT products.name, components.name " . SendSQL("SELECT products.name, components.name " .
"FROM products, components " . "FROM products, components " .
"WHERE products.id = components.product_id " . "WHERE products.id = components.product_id " .
" AND initialowner=" . DBname_to_id($user)); " AND initialowner=" . login_to_id($user));
$found = 0; $found = 0;
while (MoreSQLData()) { while (MoreSQLData()) {
if ($found) { if ($found) {
...@@ -561,7 +561,7 @@ if ($action eq 'del') { ...@@ -561,7 +561,7 @@ if ($action eq 'del') {
SendSQL("SELECT products.name, components.name " . SendSQL("SELECT products.name, components.name " .
"FROM products, components " . "FROM products, components " .
"WHERE products.id = components.product_id " . "WHERE products.id = components.product_id " .
" AND initialqacontact=" . DBname_to_id($user)); " AND initialqacontact=" . login_to_id($user));
$found = 0; $found = 0;
while (MoreSQLData()) { while (MoreSQLData()) {
if ($found) { if ($found) {
......
...@@ -248,7 +248,7 @@ if ($cgi->param('update')) { ...@@ -248,7 +248,7 @@ if ($cgi->param('update')) {
my $emailregexp = Param('emailregexp'); my $emailregexp = Param('emailregexp');
$mailto =~ /($emailregexp)/; $mailto =~ /($emailregexp)/;
$mailto =~ $1; $mailto =~ $1;
$mailto_id = DBname_to_id($mailto); $mailto_id = login_to_id($mailto);
} }
elsif ($mailto_type == MAILTO_GROUP) { elsif ($mailto_type == MAILTO_GROUP) {
# detaint the group parameter # detaint the group parameter
......
...@@ -37,6 +37,7 @@ use Bugzilla::Util; ...@@ -37,6 +37,7 @@ use Bugzilla::Util;
use Bugzilla::Config qw(:DEFAULT ChmodDataFile $localconfig $datadir); use Bugzilla::Config qw(:DEFAULT ChmodDataFile $localconfig $datadir);
use Bugzilla::BugMail; use Bugzilla::BugMail;
use Bugzilla::Auth; use Bugzilla::Auth;
use Bugzilla::User;
# Shut up misguided -w warnings about "used only once". For some reason, # Shut up misguided -w warnings about "used only once". For some reason,
# "use vars" chokes on me when I try it here. # "use vars" chokes on me when I try it here.
...@@ -654,24 +655,9 @@ sub DBID_to_name { ...@@ -654,24 +655,9 @@ sub DBID_to_name {
return $::cachedNameArray{$id}; return $::cachedNameArray{$id};
} }
sub DBname_to_id {
my ($name) = (@_);
PushGlobalSQLState();
SendSQL("select userid from profiles where login_name = @{[SqlQuote($name)]}");
my $r = FetchOneColumn();
PopGlobalSQLState();
# $r should be a positive integer, this makes Taint mode happy
if (defined $r && $r =~ m/^([1-9][0-9]*)$/) {
return $1;
} else {
return 0;
}
}
sub DBNameToIdAndCheck { sub DBNameToIdAndCheck {
my ($name) = (@_); my ($name) = (@_);
my $result = DBname_to_id($name); my $result = login_to_id($name);
if ($result > 0) { if ($result > 0) {
return $result; return $result;
} }
......
...@@ -67,6 +67,7 @@ use XML::Parser; ...@@ -67,6 +67,7 @@ use XML::Parser;
use Data::Dumper; use Data::Dumper;
$Data::Dumper::Useqq = 1; $Data::Dumper::Useqq = 1;
use Bugzilla::BugMail; use Bugzilla::BugMail;
use Bugzilla::User;
require "CGI.pl"; require "CGI.pl";
require "globals.pl"; require "globals.pl";
...@@ -202,7 +203,7 @@ unless ( Param("move-enabled") ) { ...@@ -202,7 +203,7 @@ unless ( Param("move-enabled") ) {
exit; exit;
} }
my $exporterid = DBname_to_id($exporter); my $exporterid = login_to_id($exporter);
if ( ! $exporterid ) { if ( ! $exporterid ) {
my $subject = "Bug import error: invalid exporter"; my $subject = "Bug import error: invalid exporter";
my $message = "The user <$tree->[1][0]->{'exporter'}> who tried to move\n"; my $message = "The user <$tree->[1][0]->{'exporter'}> who tried to move\n";
...@@ -504,7 +505,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) { ...@@ -504,7 +505,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) {
$err .= ". Setting to default severity \"normal\".\n"; $err .= ". Setting to default severity \"normal\".\n";
} }
my $reporterid = DBname_to_id($bug_fields{'reporter'}); my $reporterid = login_to_id($bug_fields{'reporter'});
if ( ($bug_fields{'reporter'}) && ( $reporterid ) ) { if ( ($bug_fields{'reporter'}) && ( $reporterid ) ) {
push (@values, SqlQuote($reporterid)); push (@values, SqlQuote($reporterid));
push (@query, "reporter"); push (@query, "reporter");
...@@ -523,8 +524,8 @@ for (my $k=1 ; $k <= $bugqty ; $k++) { ...@@ -523,8 +524,8 @@ for (my $k=1 ; $k <= $bugqty ; $k++) {
my $changed_owner = 0; my $changed_owner = 0;
if ( ($bug_fields{'assigned_to'}) && if ( ($bug_fields{'assigned_to'}) &&
( DBname_to_id($bug_fields{'assigned_to'})) ) { ( login_to_id($bug_fields{'assigned_to'})) ) {
push (@values, SqlQuote(DBname_to_id($bug_fields{'assigned_to'}))); push (@values, SqlQuote(login_to_id($bug_fields{'assigned_to'})));
push (@query, "assigned_to"); push (@query, "assigned_to");
} else { } else {
push (@values, SqlQuote($exporterid) ); push (@values, SqlQuote($exporterid) );
...@@ -587,7 +588,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) { ...@@ -587,7 +588,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) {
if (Param("useqacontact")) { if (Param("useqacontact")) {
my $qa_contact; my $qa_contact;
if ( (defined $bug_fields{'qa_contact'}) && if ( (defined $bug_fields{'qa_contact'}) &&
($qa_contact = DBname_to_id($bug_fields{'qa_contact'})) ){ ($qa_contact = login_to_id($bug_fields{'qa_contact'})) ){
push (@values, $qa_contact); push (@values, $qa_contact);
push (@query, "qa_contact"); push (@query, "qa_contact");
} else { } else {
...@@ -615,7 +616,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) { ...@@ -615,7 +616,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) {
if (defined $bug_fields{'cc'}) { if (defined $bug_fields{'cc'}) {
foreach my $person (split(/[ ,]/, $bug_fields{'cc'})) { foreach my $person (split(/[ ,]/, $bug_fields{'cc'})) {
my $uid; my $uid;
if ( ($person ne "") && ($uid = DBname_to_id($person)) ) { if ( ($person ne "") && ($uid = login_to_id($person)) ) {
SendSQL("insert into cc (bug_id, who) values ($id, " . SqlQuote($uid) .")"); SendSQL("insert into cc (bug_id, who) values ($id, " . SqlQuote($uid) .")");
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment