C4::Context::session provides parameters for CGI::Session constructor
with the passed session id.

C4/Auth.pm and installer/InstallAuth.pm now use this function
---
 C4/Auth.pm               |   18 +-----------
 C4/Context.pm            |   70 ++++++++++++++++++++++++++++++++++++++++++++++
 installer/InstallAuth.pm |    6 +--
 3 files changed, 73 insertions(+), 21 deletions(-)

diff --git a/C4/Auth.pm b/C4/Auth.pm
index f986542..92f9c19 100755
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -1167,23 +1167,7 @@ will be created.
 
 =cut
 
-sub get_session {
-    my $sessionID = shift;
-    my $storage_method = C4::Context->preference('SessionStorage');
-    my $dbh = C4::Context->dbh;
-    my $session;
-    if ($storage_method eq 'mysql'){
-        $session = new CGI::Session("driver:MySQL;serializer:yaml;id:md5", 
$sessionID, {Handle=>$dbh});
-    }
-    elsif ($storage_method eq 'Pg') {
-        $session = new 
CGI::Session("driver:PostgreSQL;serializer:yaml;id:md5", $sessionID, 
{Handle=>$dbh});
-    }
-    else {
-        # catch all defaults to tmp should work on all systems
-        $session = new CGI::Session("driver:File;serializer:yaml;id:md5", 
$sessionID, {Directory=>'/tmp'});
-    }
-    return $session;
-}
+sub get_session { C4::Context->session(shift); }
 
 sub checkpw {
 
diff --git a/C4/Context.pm b/C4/Context.pm
index a4aa894..f23cf40 100644
--- a/C4/Context.pm
+++ b/C4/Context.pm
@@ -19,6 +19,8 @@ package C4::Context;
 use strict;
 use vars qw($VERSION $AUTOLOAD $context @context_stack);
 
+our $error;
+
 BEGIN {
        if ($ENV{'HTTP_USER_AGENT'})    {
                require CGI::Carp;
@@ -868,6 +870,74 @@ sub userenv
     }
 }
 
+=item session
+
+C4::Context::session($sessionID) returns the args that have to be passed to 
the CGI::Session constructor.
+
+  my $session = CGI::session->new( C4::Context::session($sessionID) );
+
+If there is no available driver for the Syspref session storage, session set a 
C4::Context::error.
+
+Todo    : can be configured from the sysprefs ?
+Toughts : what about just let the default CGI::Session values if not set under 
koha
+
+=cut
+
+sub session {
+    my (undef, $sessionID ) = @_;
+    my $conf = {}; # session configuration
+
+    my %dsn = (
+         id         => $ENV{KOHA_SESSION_ID}         || 'md5'
+       , serializer => $ENV{KOHA_SESSION_SERIALIZER} || 'yaml'
+    );
+
+    # Todo:
+    # ensure that this module doesn't have to be loaded by CGI::Session
+    exists $dsn{id}
+       and $dsn{id} eq 'md5'
+       and require CGI::Session::ID::md5;
+
+    my $filedir    = $ENV{KOHA_SESSION_FILES}      || '/tmp';
+    # -d -r $filedir
+
+    my $dbh = dbh;
+
+    # Available drivers
+    my %driver = (
+       File => [ 'File', { Directory => $filedir } ]
+       , mysql => [ 'MySQL'     , { Handle => $dbh } ]
+       , Pg    => [ 'PostgreSQL', { Handle => $dbh } ]
+    );
+
+    my $prefered = $context->preference('SessionStorage') || 'File';
+
+    if ( exists $driver{$prefered} ) {
+       ( $dsn{driver}, $conf ) = @{ $driver{$prefered} };
+    } else {
+       $error = 'no available driver for CGI::Session';
+       return undef;
+    }
+
+    my $dsn_string = join(';', map { "$_:$dsn{$_}" } keys %dsn );
+    return $dsn_string
+       , $sessionID
+       , $conf 
+    ;
+}
+
+=item error
+
+Just return the last unchecked error from C4::Context.
+
+=cut
+
+sub error {
+    my $r = $error;
+    $error = undef;
+    $r;
+}
+
 =item set_userenv
 
   C4::Context->set_userenv($usernum, $userid, $usercnum, $userfirstname, 
$usersurname, $userbranch, $userflags, $emailaddress);
diff --git a/installer/InstallAuth.pm b/installer/InstallAuth.pm
index 73cb814..9ad71d1 100644
--- a/installer/InstallAuth.pm
+++ b/installer/InstallAuth.pm
@@ -237,9 +237,7 @@ sub checkauth {
     my $logout = $query->param('logout.x');
     if ( $sessionID = $query->cookie("CGISESSID") ) {
         C4::Context->_new_userenv($sessionID);
-        my $session =
-          new CGI::Session( "driver:File;serializer:yaml", $sessionID,
-            { Directory => '/tmp' } );
+        my $session = CGI::Session->new( C4::Context::session($sessionID) );
         if ( $session->param('cardnumber') ) {
             C4::Context::set_userenv(
                 $session->param('number'),
@@ -274,7 +272,7 @@ sub checkauth {
     }
     unless ($userid) {
         my $session =
-          new CGI::Session( "driver:File;serializer:yaml", undef, { Directory 
=> '/tmp' } );
+          CGI::Session->new( C4::Context::session );
         $sessionID = $session->id;
         $userid    = $query->param('userid');
         C4::Context->_new_userenv($sessionID);
-- 
1.5.5.3

_______________________________________________
Koha-patches mailing list
[email protected]
http://lists.koha.org/mailman/listinfo/koha-patches

Reply via email to