Bug 10900 - Incorrect calling conventions accessing C4::Context
authorMark Tompsett <mtompset@hotmail.com>
Sat, 21 Sep 2013 01:36:46 +0000 (21:36 -0400)
committerTomas Cohen Arazi <tomascohen@gmail.com>
Mon, 9 Feb 2015 20:00:13 +0000 (17:00 -0300)
There were multiple calling conventions for C4::Context's
set_userenv routine. So the following commands were used to
find discrepancies:
    grep "::set_userenv" `find .`
    grep "\->set_userenv" `find .`

The first grep demonstrated that the smaller change is from
:: to -> as only C4/Auth.pm, installer/InstallAuth.pm, and
t/db_dependent/Circulation.t would need to be modified. This
patch corrects C4::Context's set_userenv routine to be object
call based (use ->) by using a shift to ignore the first
parameter, and modify the three files found with :: calls.

As the result of trying to roll a distribution,
t/Circulation_barcodedecode.t was discovered to be faulty. The
cause being incorrect parameters! This was hidden when there
was no shift in the set_userenv routine. However, with its
correction, the test broke.

This led me to read the POD documentation for the function
set_userenv in C4::Context and realize it was outdated as
well. It has been revised to match the current version of
the function.

Then intentionally bad parameters passed to the set_userenv
routine in C4::Context were hunted down. The biggest problems
were missing surnames or branch names.

Rebase required because of shibboleth change in C4/Context.pm

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
C4/Auth.pm
C4/Context.pm
C4/InstallAuth.pm
t/Circulation_barcodedecode.t
t/db_dependent/Circulation.t

index 40e20f0..22f1e8e 100644 (file)
@@ -752,7 +752,7 @@ sub checkauth {
         my $s_userid = '';
         if ($session) {
             $s_userid = $session->param('id') // '';
-            C4::Context::set_userenv(
+            C4::Context->set_userenv(
                 $session->param('number'),       $s_userid,
                 $session->param('cardnumber'),   $session->param('firstname'),
                 $session->param('surname'),      $session->param('branch'),
@@ -1084,7 +1084,7 @@ sub checkauth {
                 if ($persona) {
                     $session->param( 'persona', 1 );
                 }
-                C4::Context::set_userenv(
+                C4::Context->set_userenv(
                     $session->param('number'),       $session->param('id'),
                     $session->param('cardnumber'),   $session->param('firstname'),
                     $session->param('surname'),      $session->param('branch'),
@@ -1333,7 +1333,7 @@ sub check_api_auth {
         my $session = get_session($sessionID);
         C4::Context->_new_userenv($sessionID);
         if ($session) {
-            C4::Context::set_userenv(
+            C4::Context->set_userenv(
                 $session->param('number'),       $session->param('id'),
                 $session->param('cardnumber'),   $session->param('firstname'),
                 $session->param('surname'),      $session->param('branch'),
@@ -1505,7 +1505,7 @@ sub check_api_auth {
                 $session->param( 'ip',           $session->remote_addr() );
                 $session->param( 'lasttime',     time() );
             }
-            C4::Context::set_userenv(
+            C4::Context->set_userenv(
                 $session->param('number'),       $session->param('id'),
                 $session->param('cardnumber'),   $session->param('firstname'),
                 $session->param('surname'),      $session->param('branch'),
@@ -1586,7 +1586,7 @@ sub check_cookie_auth {
     my $session   = get_session($sessionID);
     C4::Context->_new_userenv($sessionID);
     if ($session) {
-        C4::Context::set_userenv(
+        C4::Context->set_userenv(
             $session->param('number'),       $session->param('id'),
             $session->param('cardnumber'),   $session->param('firstname'),
             $session->param('surname'),      $session->param('branch'),
@@ -1733,35 +1733,35 @@ sub checkpw_internal {
 
     my $sth =
       $dbh->prepare(
-        "select password,cardnumber,borrowernumber,userid,firstname,surname,branchcode,flags from borrowers where userid=?"
+        "select password,cardnumber,borrowernumber,userid,firstname,surname,borrowers.branchcode,branches.branchname,flags from borrowers join branches on borrowers.branchcode=branches.branchcode where userid=?"
       );
     $sth->execute($userid);
     if ( $sth->rows ) {
         my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname,
-            $surname, $branchcode, $flags )
+            $surname, $branchcode, $branchname, $flags )
           = $sth->fetchrow;
 
         if ( checkpw_hash( $password, $stored_hash ) ) {
 
             C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber,
-                $firstname, $surname, $branchcode, $flags );
+                $firstname, $surname, $branchcode, $branchname, $flags );
             return 1, $cardnumber, $userid;
         }
     }
     $sth =
       $dbh->prepare(
-        "select password,cardnumber,borrowernumber,userid, firstname,surname,branchcode,flags from borrowers where cardnumber=?"
+        "select password,cardnumber,borrowernumber,userid,firstname,surname,borrowers.branchcode,branches.branchname,flags from borrowers join branches on borrowers.branchcode=branches.branchcode where cardnumber=?"
       );
     $sth->execute($userid);
     if ( $sth->rows ) {
         my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname,
-            $surname, $branchcode, $flags )
+            $surname, $branchcode, $branchname, $flags )
           = $sth->fetchrow;
 
         if ( checkpw_hash( $password, $stored_hash ) ) {
 
             C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber,
-                $firstname, $surname, $branchcode, $flags );
+                $firstname, $surname, $branchcode, $branchname, $flags );
             return 1, $cardnumber, $userid;
         }
     }
index 54b59b5..274522e 100644 (file)
@@ -1086,9 +1086,10 @@ sub userenv {
 
 =head2 set_userenv
 
-  C4::Context->set_userenv($usernum, $userid, $usercnum, $userfirstname, 
-                  $usersurname, $userbranch, $userflags, $emailaddress, $branchprinter,
-                  $persona);
+  C4::Context->set_userenv($usernum, $userid, $usercnum,
+                           $userfirstname, $usersurname,
+                           $userbranch, $branchname, $userflags,
+                           $emailaddress, $branchprinter, $persona);
 
 Establish a hash of user environment variables.
 
@@ -1098,6 +1099,7 @@ set_userenv is called in Auth.pm
 
 #'
 sub set_userenv {
+    shift @_;
     my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter, $persona, $shibboleth)=
     map { Encode::is_utf8( $_ ) ? $_ : Encode::decode('UTF-8', $_) } # CGI::Session doesn't handle utf-8, so we decode it here
     @_;
index 1fe45ae..d6dbdd8 100644 (file)
@@ -246,7 +246,7 @@ sub checkauth {
           new CGI::Session( "driver:File;serializer:yaml", $sessionID,
             { Directory => '/tmp' } );
         if ( $session->param('cardnumber') ) {
-            C4::Context::set_userenv(
+            C4::Context->set_userenv(
                 $session->param('number'),
                 $session->param('id'),
                 $session->param('cardnumber'),
@@ -308,7 +308,7 @@ sub checkauth {
            #Only superlibrarian should have access to this page.
            #Since if it is a user, it is supposed that there is a borrower table
            #And thus that data structure is loaded.
-                my $hash = C4::Context::set_userenv(
+                my $hash = C4::Context->set_userenv(
                     0,                           0,
                     C4::Context->config('user'), C4::Context->config('user'),
                     C4::Context->config('user'), "",
@@ -412,7 +412,7 @@ sub checkpw {
             C4::Context->config('user'),
             C4::Context->config('user'),
             C4::Context->config('user'),
-            "", 1
+            "", "NO_LIBRARY_SET", 1
         );
         return 2;
     }
index 5090e9b..f397cc9 100644 (file)
@@ -21,7 +21,7 @@ use Test::More tests => 26;
 use t::lib::Mocks;
 
 C4::Context->_new_userenv(123456);
-C4::Context->set_userenv(1,'kmkale' , 1, 'kk1' , 'IMS', 0, 'kmkale@anantcorp.com');
+C4::Context->set_userenv(1,'kmkale' , 1, 'km', 'kale' , 'IMS', 'IMS Branch DEscription', 0, 'kmkale@anantcorp.com');
 
 BEGIN {
     # Mock the DB connexion and C4::Context
index 58dbc2b..d020d24 100755 (executable)
@@ -95,7 +95,7 @@ is(
 
 # Now, set a userenv
 C4::Context->_new_userenv('xxx');
-C4::Context::set_userenv(0,0,0,'firstname','surname', 'MPL', 'Midway Public Library', '', '', '');
+C4::Context->set_userenv(0,0,0,'firstname','surname', 'MPL', 'Midway Public Library', '', '', '');
 is(C4::Context->userenv->{branch}, 'MPL', 'userenv set');
 
 # Userenv set, PickupLibrary