Bug 36098: Fix storage_method pass
[koha.git] / Koha / Session.pm
1 package Koha::Session;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use CGI::Session;
20
21 use C4::Context;
22 use Koha::Caches;
23
24 =head1 NAME
25
26 Koha::Session - Session class for Koha
27
28 =head1 SYNOPSIS
29
30   use Koha::Session;
31   my $session = Koha::Session->get_session({ sessionID => $sessionID});
32
33 =head1 DESCRIPTION
34
35 This simple class exposes some basic methods for managing user sessions.
36
37 =head1 METHODS
38
39 =head2 get_session
40
41   my $session = Koha::Session->get_session({ sessionID => $sessionID});
42
43 Given a session ID, retrieves the CGI::Session object used to store
44 the session's state.  The session object can be used to store
45 data that needs to be accessed by different scripts during a
46 user's session.
47
48 If the C<$sessionID> parameter is an empty string, a new session
49 will be created.
50
51 =cut
52
53 sub _get_session_params {
54     my ( $class, $args ) = @_;
55     my $storage_method = $args->{storage_method};
56     $storage_method ||= C4::Context->preference('SessionStorage');
57     if ( $storage_method eq 'mysql' ) {
58         my $dbh = C4::Context->dbh;
59         return { dsn => "serializer:yamlxs;driver:MySQL;id:md5", dsn_args => { Handle => $dbh } };
60     } elsif ( $storage_method eq 'Pg' ) {
61         my $dbh = C4::Context->dbh;
62         return { dsn => "serializer:yamlxs;driver:PostgreSQL;id:md5", dsn_args => { Handle => $dbh } };
63     } elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) {
64         my $memcached = Koha::Caches->get_instance()->memcached_cache;
65         return { dsn => "serializer:yamlxs;driver:memcached;id:md5", dsn_args => { Memcached => $memcached } };
66     } else {
67
68         # catch all defaults to tmp should work on all systems
69         my $dir      = C4::Context::temporary_directory;
70         my $instance = C4::Context->config('database')
71             ;    #actually for packages not exactly the instance name, but generally safer to leave it as it is
72         return { dsn => "serializer:yamlxs;driver:File;id:md5", dsn_args => { Directory => "$dir/cgisess_$instance" } };
73     }
74     return;
75 }
76
77 sub get_session {
78     my ( $class, $args ) = @_;
79     my $sessionID      = $args->{sessionID};
80     my $storage_method = $args->{storage_method};
81     my $params         = $class->_get_session_params( { storage_method => $storage_method } );
82     my $session;
83     if ($sessionID) {    # find existing
84         CGI::Session::ErrorHandler->set_error(q{});    # clear error, cpan issue #111463
85         $session = CGI::Session->load( $params->{dsn}, $sessionID, $params->{dsn_args} );
86     } else {
87         $session = CGI::Session->new( $params->{dsn}, $sessionID, $params->{dsn_args} );
88
89         # no need to flush here
90     }
91     return $session;
92 }
93
94 1;