[tools] testbot: Clean up the Authenticate() and NewSession() methods.

Francois Gouget fgouget at codeweavers.com
Thu Jul 7 08:11:09 CDT 2022


Make sure all Authenticate() methods return an (error, user) pair
instead of just an error string. The result is the same but it's better
to be consistent.
Clearly identify unused variables and tweak the formatting a bit.

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---
 testbot/lib/WineTestBot/CGI/Sessions.pm |  3 +-
 testbot/lib/WineTestBot/Users.pm        | 52 ++++++-------------------
 2 files changed, 13 insertions(+), 42 deletions(-)

diff --git a/testbot/lib/WineTestBot/CGI/Sessions.pm b/testbot/lib/WineTestBot/CGI/Sessions.pm
index 8594cfcdf0..afe78d0632 100644
--- a/testbot/lib/WineTestBot/CGI/Sessions.pm
+++ b/testbot/lib/WineTestBot/CGI/Sessions.pm
@@ -131,8 +131,7 @@ sub NewSession($$$)
   $Session->User($User);
   $Session->Permanent($Permanent);
 
-  my ($ErrKey, $ErrProperty, $ErrMessage) = $self->Save();
-
+  my ($_ErrKey, $_ErrProperty, $ErrMessage) = $self->Save();
   return ($ErrMessage, $Session);
 }
 
diff --git a/testbot/lib/WineTestBot/Users.pm b/testbot/lib/WineTestBot/Users.pm
index 70d0d7b75e..e9d68b09c0 100644
--- a/testbot/lib/WineTestBot/Users.pm
+++ b/testbot/lib/WineTestBot/Users.pm
@@ -223,15 +223,10 @@ sub Authenticate($$)
 
   if (! $self->Activated())
   {
-    if ($self->WaitingForApproval())
-    {
-      return ("Your account has not been approved yet", undef);
-    }
-    else
-    {
-      return ("You need to activate your account, see email for instructions",
-              undef);
-    }
+    return ($self->WaitingForApproval() ?
+            "Your account has not been approved yet" :
+            "You need to activate your account, see email for instructions",
+            undef);
   }
 
   my $CorrectHashedPassword = $self->Password;
@@ -352,41 +347,22 @@ sub AuthenticateLDAP($$$)
   my ($self, $Name, $Password) = @_;
 
   my $LDAP = Net::LDAP->new($LDAPServer);
-  if (! defined($LDAP))
-  {
-    return ("Can't connect to LDAP server: $@", undef);
-  }
+  return ("Can't connect to LDAP server: $@", undef) if (!$LDAP);
 
   my $BindDN = $LDAPBindDN;
   $BindDN =~ s/%USERNAME%/$Name/;
   my $Msg = $LDAP->bind($BindDN, password => $Password);
-  if ($Msg->code)
-  {
-    return "Unknown username or incorrect password";
-  }
+  return ("Unknown username or incorrect password", undef) if ($Msg->code);
 
   my $User = $self->GetItem($Name);
-  if (defined($User))
-  {
-    return (undef, $User);
-  }
+  return (undef, $User) if (defined $User);
 
   $User = $self->Add();
   my $ErrMessage = $User->FromLDAP($LDAP, $Name);
-  if ($ErrMessage)
-  {
-    return $ErrMessage;
-  }
-
-  my $ErrKey;
-  my $ErrProperty;
-  ($ErrKey, $ErrProperty, $ErrMessage) = $self->Save();
-  if ($ErrMessage)
-  {
-    return $ErrMessage;
-  }
+  return ($ErrMessage, undef) if ($ErrMessage);
 
-  return (undef, $User);
+  (my $_ErrKey, my $_ErrProperty, $ErrMessage) = $self->Save();
+  return $ErrMessage ? ($ErrMessage, undef) : (undef, $User);
 }
 
 sub AuthenticateBuiltin($$$)
@@ -394,12 +370,8 @@ sub AuthenticateBuiltin($$$)
   my ($self, $Name, $Password) = @_;
 
   my $User = $self->GetItem($Name);
-  if (! defined($User))
-  {
-    return "Unknown username or incorrect password";
-  }
-
-  return $User->Authenticate($Password);
+  return $User ? $User->Authenticate($Password) :
+                 ("Unknown username or incorrect password", undef);
 }
 
 sub Authenticate($$$)
-- 
2.30.2



More information about the wine-devel mailing list