bug_6624: Added Open Library Search and Read API calls to opac-results.tt and opac...
[srvgit] / C4 / Auth.pm
index 03b8068..7655a4f 100644 (file)
@@ -20,6 +20,7 @@ package C4::Auth;
 use strict;
 use warnings;
 use Digest::MD5 qw(md5_base64);
+use File::Spec;
 use JSON qw/encode_json/;
 use URI::Escape;
 use CGI::Session;
@@ -32,6 +33,7 @@ use C4::Branch;       # GetBranches
 use C4::Search::History;
 use Koha;
 use Koha::AuthUtils qw(hash_password);
+use Koha::LibraryCategories;
 use POSIX qw/strftime/;
 use List::MoreUtils qw/ any /;
 use Encode qw( encode is_utf8);
@@ -178,6 +180,29 @@ sub get_template_and_user {
         );
     }
 
+
+    # If the user logged in is the SCO user and he tries to go out the SCO module, log the user out removing the CGISESSID cookie
+    if ( $in->{type} eq 'opac' and $in->{template_name} !~ m|sco/| ) {
+        if (  C4::Context->preference('AutoSelfCheckID') && $user eq C4::Context->preference('AutoSelfCheckID') ) {
+            $template = C4::Templates::gettemplate( 'opac-auth.tt', 'opac', $in->{query} );
+            my $cookie = $in->{query}->cookie(
+                -name     => 'CGISESSID',
+                -value    => '',
+                -expires  => '',
+                -HttpOnly => 1,
+            );
+
+            $template->param( loginprompt => 1 );
+            print $in->{query}->header(
+                -type    => 'text/html',
+                -charset => 'utf-8',
+                -cookie  => $cookie,
+              ),
+            $template->output;
+            safe_exit;
+        }
+    }
+
     my $borrowernumber;
     if ($user) {
         require C4::Members;
@@ -185,9 +210,10 @@ sub get_template_and_user {
         # It's possible for $user to be the borrowernumber if they don't have a
         # userid defined (and are logging in through some other method, such
         # as SSL certs against an email address)
+        my $borrower;
         $borrowernumber = getborrowernumber($user) if defined($user);
         if ( !defined($borrowernumber) && defined($user) ) {
-            my $borrower = C4::Members::GetMember( borrowernumber => $user );
+            $borrower = C4::Members::GetMember( borrowernumber => $user );
             if ($borrower) {
                 $borrowernumber = $user;
 
@@ -195,6 +221,8 @@ sub get_template_and_user {
                 # to do it.
                 $user = $borrower->{firstname} . ' ' . $borrower->{surname};
             }
+        } else {
+            $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber );
         }
 
         # user info
@@ -203,20 +231,25 @@ sub get_template_and_user {
         $template->param( sessionID          => $sessionID );
 
         if ( $in->{'type'} eq 'opac' ) {
-            require C4::VirtualShelves;
-            my ( $total, $pubshelves, $barshelves ) = C4::VirtualShelves::GetSomeShelfNames( $borrowernumber, 'MASTHEAD' );
+            require Koha::Virtualshelves;
+            my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
+                {
+                    borrowernumber => $borrowernumber,
+                    category       => 1,
+                }
+            );
+            my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
+                {
+                    category       => 2,
+                }
+            );
             $template->param(
-                pubshelves     => $total->{pubtotal},
-                pubshelvesloop => $pubshelves,
-                barshelves     => $total->{bartotal},
-                barshelvesloop => $barshelves,
+                some_private_shelves => $some_private_shelves,
+                some_public_shelves  => $some_public_shelves,
             );
         }
 
-        my ($borr) = C4::Members::GetMemberDetails($borrowernumber);
-        my @bordat;
-        $bordat[0] = $borr;
-        $template->param( "USER_INFO" => \@bordat );
+        $template->param( "USER_INFO" => $borrower );
 
         my $all_perms = get_all_subpermissions();
 
@@ -340,11 +373,14 @@ sub get_template_and_user {
         $template->param( sessionID => $sessionID );
 
         if ( $in->{'type'} eq 'opac' ){
-            require C4::VirtualShelves;
-            my ( $total, $pubshelves ) = C4::VirtualShelves::GetSomeShelfNames( undef, 'MASTHEAD' );
+            require Koha::Virtualshelves;
+            my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
+                {
+                    category       => 2,
+                }
+            );
             $template->param(
-                pubshelves     => $total->{pubtotal},
-                pubshelvesloop => $pubshelves,
+                some_public_shelves  => $some_public_shelves,
             );
         }
     }
@@ -362,6 +398,8 @@ sub get_template_and_user {
         $template->param( dateformat => C4::Context->preference('dateformat') );
     }
 
+    $template->param(auth_forwarded_hash => $in->{'query'}->param('auth_forwarded_hash'));
+
     # these template parameters are set the same regardless of $in->{'type'}
 
     # Set the using_https variable for templates
@@ -398,7 +436,6 @@ sub get_template_and_user {
             AmazonCoverImages                                                          => C4::Context->preference("AmazonCoverImages"),
             AutoLocation                                                               => C4::Context->preference("AutoLocation"),
             "BiblioDefaultView" . C4::Context->preference("IntranetBiblioDefaultView") => 1,
-            CalendarFirstDayOfWeek                                                     => C4::Context->preference("CalendarFirstDayOfWeek"),
             CircAutocompl                                                              => C4::Context->preference("CircAutocompl"),
             FRBRizeEditions                                                            => C4::Context->preference("FRBRizeEditions"),
             IndependentBranches                                                        => C4::Context->preference("IndependentBranches"),
@@ -466,13 +503,14 @@ sub get_template_and_user {
             $opac_name = C4::Context->userenv->{'branch'};
         }
 
+        my $library_categories = Koha::LibraryCategories->search({categorytype => 'searchdomain', show_in_pulldown => 1}, { order_by => ['categorytype', 'categorycode']});
         $template->param(
             OpacAdditionalStylesheet                   => C4::Context->preference("OpacAdditionalStylesheet"),
             AnonSuggestions                       => "" . C4::Context->preference("AnonSuggestions"),
             AuthorisedValueImages                 => C4::Context->preference("AuthorisedValueImages"),
             BranchesLoop                          => GetBranchesLoop($opac_name),
-            BranchCategoriesLoop                  => GetBranchCategories( 'searchdomain', 1, $opac_name ),
-            CalendarFirstDayOfWeek                => C4::Context->preference("CalendarFirstDayOfWeek"),
+            BranchCategoriesLoop                  => $library_categories,
+            opac_name                             => $opac_name,
             LibraryName                           => "" . C4::Context->preference("LibraryName"),
             LibraryNameTitle                      => "" . $LibraryNameTitle,
             LoginBranchname                       => C4::Context->userenv ? C4::Context->userenv->{"branchname"} : "",
@@ -511,6 +549,7 @@ sub get_template_and_user {
             opacreadinghistory                    => C4::Context->preference("opacreadinghistory"),
             OPACUserJS                            => C4::Context->preference("OPACUserJS"),
             opacuserlogin                         => "" . C4::Context->preference("opacuserlogin"),
+            OpenLibrarySearch                     => C4::Context->preference("OpenLibrarySearch"),
             ShowReviewer                          => C4::Context->preference("ShowReviewer"),
             ShowReviewerPhoto                     => C4::Context->preference("ShowReviewerPhoto"),
             suggestion                            => "" . C4::Context->preference("suggestion"),
@@ -716,7 +755,7 @@ sub checkauth {
     # state variables
     my $loggedin = 0;
     my %info;
-    my ( $userid, $cookie, $sessionID, $flags, $barshelves, $pubshelves );
+    my ( $userid, $cookie, $sessionID, $flags );
     my $logout = $query->param('logout.x');
 
     my $anon_search_history;
@@ -776,7 +815,9 @@ sub checkauth {
             $sessiontype = $session->param('sessiontype') || '';
         }
         if ( ( $query->param('koha_login_context') && ( $q_userid ne $s_userid ) )
-            || ( $cas && $query->param('ticket') && !C4::Context->userenv->{'id'} ) || ( $shib && $shib_login && !$logout ) ) {
+            || ( $cas && $query->param('ticket') && !C4::Context->userenv->{'id'} )
+            || ( $shib && $shib_login && !$logout && !C4::Context->userenv->{'id'} )
+        ) {
 
             #if a user enters an id ne to the id in the current session, we need to log them in...
             #first we need to clear the anonymous session...
@@ -1195,6 +1236,7 @@ sub checkauth {
         intranetbookbag                       => C4::Context->preference("intranetbookbag"),
         IntranetNav                           => C4::Context->preference("IntranetNav"),
         IntranetFavicon                       => C4::Context->preference("IntranetFavicon"),
+        IntranetUserCSS                       => C4::Context->preference("IntranetUserCSS"),
         IntranetUserJS                        => C4::Context->preference("IntranetUserJS"),
         IndependentBranches                   => C4::Context->preference("IndependentBranches"),
         AutoLocation                          => C4::Context->preference("AutoLocation"),
@@ -1209,11 +1251,14 @@ sub checkauth {
     $template->param( loginprompt => 1 ) unless $info{'nopermission'};
 
     if ( $type eq 'opac' ) {
-        require C4::VirtualShelves;
-        my ( $total, $pubshelves ) = C4::VirtualShelves::GetSomeShelfNames( undef, 'MASTHEAD' );
+        require Koha::Virtualshelves;
+        my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
+            {
+                category       => 2,
+            }
+        );
         $template->param(
-            pubshelves     => $total->{pubtotal},
-            pubshelvesloop => $pubshelves,
+            some_public_shelves  => $some_public_shelves,
         );
     }
 
@@ -1247,9 +1292,7 @@ sub checkauth {
         );
     }
 
-    my $self_url = $query->url( -absolute => 1 );
     $template->param(
-        url         => $self_url,
         LibraryName => C4::Context->preference("LibraryName"),
     );
     $template->param(%info);
@@ -1672,7 +1715,9 @@ sub get_session {
     }
     else {
         # catch all defaults to tmp should work on all systems
-        $session = new CGI::Session( "driver:File;serializer:yaml;id:md5", $sessionID, { Directory => '/tmp' } );
+        my $dir = File::Spec->tmpdir;
+        my $instance = C4::Context->config( 'database' ); #actually for packages not exactly the instance name, but generally safer to leave it as it is
+        $session = new CGI::Session( "driver:File;serializer:yaml;id:md5", $sessionID, { Directory => "$dir/cgisess_$instance" } );
     }
     return $session;
 }
@@ -1904,14 +1949,14 @@ of the subpermission.
 
 sub get_all_subpermissions {
     my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare( "SELECT flag, code, description
+    my $sth = $dbh->prepare( "SELECT flag, code
                              FROM permissions
                              JOIN userflags ON (module_bit = bit)" );
     $sth->execute();
 
     my $all_perms = {};
     while ( my $perm = $sth->fetchrow_hashref ) {
-        $all_perms->{ $perm->{'flag'} }->{ $perm->{'code'} } = $perm->{'description'};
+        $all_perms->{ $perm->{'flag'} }->{ $perm->{'code'} } = 1;
     }
     return $all_perms;
 }