]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 283237: Move DBname_to_id out of globals.pl
authormkanat%kerio.com <>
Fri, 25 Feb 2005 07:42:47 +0000 (07:42 +0000)
committermkanat%kerio.com <>
Fri, 25 Feb 2005 07:42:47 +0000 (07:42 +0000)
Patch By Max Kanat-Alexander <mkanat@kerio.com> r=wurblzap, a=myk

12 files changed:
Bugzilla/Bug.pm
Bugzilla/BugMail.pm
Bugzilla/Flag.pm
Bugzilla/Search.pm
Bugzilla/User.pm
contrib/bug_email.pl
contrib/sendbugmail.pl
editcomponents.cgi
editusers.cgi
editwhines.cgi
globals.pl
importxml.pl

index 2f1df58bdfafdbf2ce7f1a50a1f90971a2f71e55..9092ddf89a0ce7eca9666183323807ce2f0c52c6 100755 (executable)
@@ -131,7 +131,7 @@ sub initBug  {
   }
   else {
      if ($user_id =~ /^\@/) {
-        $user_id = &::DBname_to_id($user_id); 
+        $user_id = login_to_id($user_id); 
      }
   }
 
index bfe1c897ed3a074b3a4894db0a47b53d14ab08f7..df734ef6f4fb32d31e8e74dac23f17303f52bbd7 100644 (file)
@@ -627,7 +627,7 @@ sub filterEmailGroup ($$$) {
         # 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
         # default preference is to receive all mail.
-        my $userid = DBname_to_id($user);
+        my $userid = login_to_id($user);
         if (!$userid) {
             push(@recipients, $user);
             next;
index ffed79bdef706b98ac1f88a87235a5b44208f13e..4b5f76fb0c98594a7af3732a1adf405050464da5 100644 (file)
@@ -449,7 +449,7 @@ sub modify {
             # Get the requestee, if any.
             my $requestee_id = "NULL";
             if ($requestee_email) {
-                $requestee_id = &::DBname_to_id($requestee_email);
+                $requestee_id = login_to_id($requestee_email);
                 $flag->{'requestee'} = new Bugzilla::User($requestee_id);
             }
 
@@ -531,7 +531,7 @@ sub FormToNewFlags {
         if ($status eq "?") {
             my $requestee = $data->{"requestee_type-$type_id"};
             if ($requestee) {
-                my $requestee_id = &::DBname_to_id($requestee);
+                my $requestee_id = login_to_id($requestee);
                 $flag->{'requestee'} = new Bugzilla::User($requestee_id);
             }
         }
index 8a08ef6180c73c9d4402b74a27883c2f64463285..aedb2b2b4b50f2dee59c9e148a2dae236ffa7b9a 100644 (file)
@@ -41,6 +41,7 @@ use Bugzilla::Error;
 use Bugzilla::Util;
 use Bugzilla::Constants;
 use Bugzilla::Group;
+use Bugzilla::User;
 
 use Date::Format;
 use Date::Parse;
@@ -1414,7 +1415,7 @@ sub ListIDsForEmail {
     if ($type eq 'anyexact') {
         foreach my $w (split(/,/, $email)) {
             $w = trim($w);
-            my $id = &::DBname_to_id($w);
+            my $id = login_to_id($w);
             if ($id > 0) {
                 push(@list,$id)
             }
index 0a67400b3dc61255e098ba717dd96f56602d55a1..8809d80dd7fd050b315e830f1cdc517bad0e905a 100644 (file)
@@ -22,6 +22,7 @@
 #                 Bradley Baetz <bbaetz@acm.org>
 #                 Joel Peshkin <bugreport@peshkin.net> 
 #                 Byron Jones <bugzilla@glob.com.au>
+#                 Max Kanat-Alexander <mkanat@kerio.com>
 
 ################################################################################
 # Module Initialization
@@ -40,7 +41,9 @@ use Bugzilla::Constants;
 use Bugzilla::Auth;
 
 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
@@ -961,7 +964,7 @@ sub insert_new_user ($$) {
 sub is_available_username ($;$) {
     my ($username, $old_username) = @_;
 
-    if(&::DBname_to_id($username) != 0) {
+    if(login_to_id($username) != 0) {
         return 0;
     }
 
@@ -992,6 +995,19 @@ sub is_available_username ($;$) {
     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;
 
 __END__
@@ -1232,6 +1248,20 @@ Params: $username (scalar, string) - The full login name of the username
 
 =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
 
 L<Bugzilla|Bugzilla>
index 7fb6b533c1f6420eea0605964bfc55cbc1efe2ab..ed7e8143acfdea62b4ccde544639ba64edea568e 100755 (executable)
@@ -38,7 +38,7 @@
 #
 # 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)
@@ -94,6 +94,7 @@ use lib ".";
 use lib "../";
 use Bugzilla::Constants;
 use Bugzilla::BugMail;
+use Bugzilla::User;
 
 my @mailerrors = ();       # Buffer for Errors in the mail
 my @mailwarnings = ();     # Buffer for Warnings found in the mail
@@ -920,7 +921,7 @@ $Control{'component'} = $Component;
 # otherwise, retrieve it from the database.
 if ( defined($Control{'assigned_to'}) 
      && $Control{'assigned_to'} !~ /^\s*$/ ) {
-    $Control{'assigned_to'} = DBname_to_id($Control{'assigned_to'});
+    $Control{'assigned_to'} = login_to_id($Control{'assigned_to'});
 } else {
     SendSQL("select initialowner from components, products where " .
             "  components.product_id=products.id AND products.name=" .
@@ -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'} ) {
     BugMailError( 1, "Could not resolve reporter !\n" );
 }
index 8e00ff49c4f2d1b0a07f60685ed8357be0d57103..ad96428950e401e065a24bf7ec84d0e185156880 100644 (file)
@@ -4,7 +4,7 @@
 #
 # 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
 # bugmail for a bug which has been changed directly in the database.
@@ -18,6 +18,7 @@ use lib qw(.);
 
 require "globals.pl";
 use Bugzilla::BugMail;
+use Bugzilla::User;
 
 sub usage {
     print STDERR "Usage: $0 bug_id user_email\n";
@@ -53,7 +54,7 @@ if ($changer !~ /$match/) {
     print STDERR "Changer \"$changer\" doesn't match email regular expression.\n";
     usage();
 }
-if(!DBname_to_id($changer)) {
+if(!login_to_id($changer)) {
     print STDERR "\"$changer\" is not a login ID.\n";
     usage();
 }
index 08cfab14cf21ca4c06f513e738df26c4fa288dcd..12a25905d612805466d706d347b4197b10bdd00d 100755 (executable)
@@ -35,6 +35,7 @@ use Bugzilla::Constants;
 use Bugzilla::Config qw(:DEFAULT $datadir);
 use Bugzilla::Series;
 use Bugzilla::Util;
+use Bugzilla::User;
 
 use vars qw($template $vars);
 
@@ -296,7 +297,7 @@ if ($action eq 'new') {
         exit;
     }
 
-    my $initialownerid = DBname_to_id ($initialowner);
+    my $initialownerid = login_to_id ($initialowner);
     if (!$initialownerid) {
         ThrowUserError('component_need_valid_initialowner',
                        {'name' => $component});
@@ -304,7 +305,7 @@ if ($action eq 'new') {
     }
 
     my $initialqacontact = trim($cgi->param('initialqacontact') || '');
-    my $initialqacontactid = DBname_to_id ($initialqacontact);
+    my $initialqacontactid = login_to_id ($initialqacontact);
     if (Param('useqacontact')) {
         if (!$initialqacontactid && $initialqacontact ne '') {
             ThrowUserError('component_need_valid_initialqacontact',
@@ -600,7 +601,7 @@ if ($action eq 'update') {
 
     if ($initialowner ne $initialownerold) {
 
-        my $initialownerid = DBname_to_id($initialowner);
+        my $initialownerid = login_to_id($initialowner);
         unless ($initialownerid) {
             $dbh->bz_unlock_tables(UNLOCK_ABORT);
             ThrowUserError('component_need_valid_initialowner',
@@ -618,7 +619,7 @@ if ($action eq 'update') {
     }
 
     if (Param('useqacontact') && $initialqacontact ne $initialqacontactold) {
-        my $initialqacontactid = DBname_to_id($initialqacontact);
+        my $initialqacontactid = login_to_id($initialqacontact);
         if (!$initialqacontactid && $initialqacontact ne '') {
             $dbh->bz_unlock_tables(UNLOCK_ABORT);
             ThrowUserError('component_need_valid_initialqacontact',
index 9a6de0d1748d839238e99fce0fae242f269782df..c6b342efd429472cbd6834162635e520f02e4a6b 100755 (executable)
@@ -536,7 +536,7 @@ if ($action eq 'del') {
     SendSQL("SELECT products.name, components.name " .
             "FROM products, components " .
             "WHERE products.id = components.product_id " .
-            " AND initialowner=" . DBname_to_id($user));
+            " AND initialowner=" . login_to_id($user));
     $found = 0;
     while (MoreSQLData()) {
         if ($found) {
@@ -561,7 +561,7 @@ if ($action eq 'del') {
     SendSQL("SELECT products.name, components.name " .
             "FROM products, components " .
             "WHERE products.id = components.product_id " .
-            " AND initialqacontact=" . DBname_to_id($user));
+            " AND initialqacontact=" . login_to_id($user));
     $found = 0;
     while (MoreSQLData()) {
         if ($found) {
index b9808941a667a929900ddd388dfd353b13d71e95..013ee0df4c4627afdd577172fa4e79f9317d346d 100755 (executable)
@@ -248,7 +248,7 @@ if ($cgi->param('update')) {
                             my $emailregexp = Param('emailregexp');
                             $mailto =~ /($emailregexp)/;
                             $mailto =~ $1;
-                            $mailto_id = DBname_to_id($mailto);
+                            $mailto_id = login_to_id($mailto);
                         }
                         elsif ($mailto_type == MAILTO_GROUP) {
                             # detaint the group parameter
index 100c8fab6d790175a59f9e3063696a8dee0cb258..2d01cb5f1b0cf5741526a23e5c03df4af9d1b5ff 100644 (file)
@@ -37,6 +37,7 @@ use Bugzilla::Util;
 use Bugzilla::Config qw(:DEFAULT ChmodDataFile $localconfig $datadir);
 use Bugzilla::BugMail;
 use Bugzilla::Auth;
+use Bugzilla::User;
 
 # Shut up misguided -w warnings about "used only once".  For some reason,
 # "use vars" chokes on me when I try it here.
@@ -654,24 +655,9 @@ sub DBID_to_name {
     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 {
     my ($name) = (@_);
-    my $result = DBname_to_id($name);
+    my $result = login_to_id($name);
     if ($result > 0) {
         return $result;
     }
index 3b3b24a176c05b1d1614e04386513a1f95fd4907..9ff9626350cd0124b7ee7ce0926dec8892f9f5a7 100755 (executable)
@@ -67,6 +67,7 @@ use XML::Parser;
 use Data::Dumper;
 $Data::Dumper::Useqq = 1;
 use Bugzilla::BugMail;
+use Bugzilla::User;
 
 require "CGI.pl";
 require "globals.pl";
@@ -202,7 +203,7 @@ unless ( Param("move-enabled") ) {
   exit;
 }
 
-my $exporterid = DBname_to_id($exporter);
+my $exporterid = login_to_id($exporter);
 if ( ! $exporterid ) {
   my $subject = "Bug import error: invalid exporter";
   my $message = "The user <$tree->[1][0]->{'exporter'}> who tried to move\n";
@@ -504,7 +505,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) {
     $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 ) ) {
     push (@values, SqlQuote($reporterid));
     push (@query, "reporter");
@@ -523,8 +524,8 @@ for (my $k=1 ; $k <= $bugqty ; $k++) {
 
   my $changed_owner = 0;
   if ( ($bug_fields{'assigned_to'}) && 
-       ( DBname_to_id($bug_fields{'assigned_to'})) ) {
-    push (@values, SqlQuote(DBname_to_id($bug_fields{'assigned_to'})));
+       ( login_to_id($bug_fields{'assigned_to'})) ) {
+    push (@values, SqlQuote(login_to_id($bug_fields{'assigned_to'})));
     push (@query, "assigned_to");
   } else {
     push (@values, SqlQuote($exporterid) );
@@ -587,7 +588,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) {
   if (Param("useqacontact")) {
     my $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 (@query, "qa_contact");
     } else {
@@ -615,7 +616,7 @@ for (my $k=1 ; $k <= $bugqty ; $k++) {
   if (defined $bug_fields{'cc'}) {
     foreach my $person (split(/[ ,]/, $bug_fields{'cc'})) {
       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) .")");
       }
     }