View | Details | Raw Unified | Return to bug 36098
Collapse All | Expand All

(-)a/C4/Auth.pm (-29 / +6 lines)
Lines 52-57 use C4::Log qw( logaction ); Link Here
52
use Koha::CookieManager;
52
use Koha::CookieManager;
53
use Koha::Auth::Permissions;
53
use Koha::Auth::Permissions;
54
use Koha::Token;
54
use Koha::Token;
55
use Koha::Session;
55
56
56
# use utf8;
57
# use utf8;
57
58
Lines 1864-1902 will be created. Link Here
1864
1865
1865
=cut
1866
=cut
1866
1867
1868
#NOTE: We're keeping this for backwards compatibility
1867
sub _get_session_params {
1869
sub _get_session_params {
1868
    my $storage_method = C4::Context->preference('SessionStorage');
1870
    return Koha::Session->_get_session_params();
1869
    if ( $storage_method eq 'mysql' ) {
1870
        my $dbh = C4::Context->dbh;
1871
        return { dsn => "serializer:yamlxs;driver:MySQL;id:md5", dsn_args => { Handle => $dbh } };
1872
    }
1873
    elsif ( $storage_method eq 'Pg' ) {
1874
        my $dbh = C4::Context->dbh;
1875
        return { dsn => "serializer:yamlxs;driver:PostgreSQL;id:md5", dsn_args => { Handle => $dbh } };
1876
    }
1877
    elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) {
1878
        my $memcached = Koha::Caches->get_instance()->memcached_cache;
1879
        return { dsn => "serializer:yamlxs;driver:memcached;id:md5", dsn_args => { Memcached => $memcached } };
1880
    }
1881
    else {
1882
        # catch all defaults to tmp should work on all systems
1883
        my $dir = C4::Context::temporary_directory;
1884
        my $instance = C4::Context->config( 'database' ); #actually for packages not exactly the instance name, but generally safer to leave it as it is
1885
        return { dsn => "serializer:yamlxs;driver:File;id:md5", dsn_args => { Directory => "$dir/cgisess_$instance" } };
1886
    }
1887
}
1871
}
1888
1872
1873
#NOTE: We're keeping this for backwards compatibility
1889
sub get_session {
1874
sub get_session {
1890
    my $sessionID      = shift;
1875
    my $sessionID = shift;
1891
    my $params = _get_session_params();
1876
    my $session   = Koha::Session->get_session( { sessionID => $sessionID } );
1892
    my $session;
1893
    if( $sessionID ) { # find existing
1894
        CGI::Session::ErrorHandler->set_error( q{} ); # clear error, cpan issue #111463
1895
        $session = CGI::Session->load( $params->{dsn}, $sessionID, $params->{dsn_args} );
1896
    } else {
1897
        $session = CGI::Session->new( $params->{dsn}, $sessionID, $params->{dsn_args} );
1898
        # no need to flush here
1899
    }
1900
    return $session;
1877
    return $session;
1901
}
1878
}
1902
1879
(-)a/Koha/Session.pm (+63 lines)
Line 0 Link Here
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
sub _get_session_params {
25
    my $class          = shift;
26
    my $storage_method = C4::Context->preference('SessionStorage');
27
    if ( $storage_method eq 'mysql' ) {
28
        my $dbh = C4::Context->dbh;
29
        return { dsn => "serializer:yamlxs;driver:MySQL;id:md5", dsn_args => { Handle => $dbh } };
30
    } elsif ( $storage_method eq 'Pg' ) {
31
        my $dbh = C4::Context->dbh;
32
        return { dsn => "serializer:yamlxs;driver:PostgreSQL;id:md5", dsn_args => { Handle => $dbh } };
33
    } elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) {
34
        my $memcached = Koha::Caches->get_instance()->memcached_cache;
35
        return { dsn => "serializer:yamlxs;driver:memcached;id:md5", dsn_args => { Memcached => $memcached } };
36
    } else {
37
38
        # catch all defaults to tmp should work on all systems
39
        my $dir      = C4::Context::temporary_directory;
40
        my $instance = C4::Context->config('database')
41
            ;    #actually for packages not exactly the instance name, but generally safer to leave it as it is
42
        return { dsn => "serializer:yamlxs;driver:File;id:md5", dsn_args => { Directory => "$dir/cgisess_$instance" } };
43
    }
44
    return;
45
}
46
47
sub get_session {
48
    my ( $class, $args ) = @_;
49
    my $sessionID = $args->{sessionID};
50
    my $params = $class->_get_session_params();
51
    my $session;
52
    if ($sessionID) {    # find existing
53
        CGI::Session::ErrorHandler->set_error(q{});    # clear error, cpan issue #111463
54
        $session = CGI::Session->load( $params->{dsn}, $sessionID, $params->{dsn_args} );
55
    } else {
56
        $session = CGI::Session->new( $params->{dsn}, $sessionID, $params->{dsn_args} );
57
58
        # no need to flush here
59
    }
60
    return $session;
61
}
62
63
1;
(-)a/t/db_dependent/Koha/Session.t (-1 / +29 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More tests => 1;
5
6
use t::lib::TestBuilder;
7
use C4::Auth;
8
use Koha::Session;
9
10
my $schema  = Koha::Database->schema;
11
my $builder = t::lib::TestBuilder->new;
12
13
subtest 'basic session fetch' => sub {
14
    plan tests => 2;
15
16
    $schema->storage->txn_begin;
17
18
    my $patron =
19
        $builder->build_object( { class => 'Koha::Patrons', value => { userid => 'superman' } } );
20
21
    my $basic_session = C4::Auth::create_basic_session( { patron => $patron, interface => 'staff' } );
22
    is( $basic_session->param('id'), 'superman', 'basic session created as expected' );
23
    $basic_session->flush;
24
25
    my $session = Koha::Session->get_session( { sessionID => $basic_session->id } );
26
    is( $session->param('id'), 'superman', 'basic session fetched as expected' );
27
28
    $schema->storage->txn_rollback;
29
};

Return to bug 36098