3 # This file is part of Koha.
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.
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.
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>.
26 Koha::Session - Session class for Koha
31 my $session = Koha::Session->get_session({ sessionID => $sessionID});
35 This simple class exposes some basic methods for managing user sessions.
41 my $session = Koha::Session->get_session({ sessionID => $sessionID});
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
48 If the C<$sessionID> parameter is an empty string, a new session
53 sub _get_session_params {
54 my ( $class, $args ) = @_;
55 my $storage_method = $args->{storage_method};
56 $storage_method ||= C4::Context->preference('SessionStorage') || 'file';
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 } };
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" } };
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 } );
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} );
87 $session = CGI::Session->new( $params->{dsn}, $sessionID, $params->{dsn_args} );
89 # no need to flush here