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 1861-1899 will be created. Link Here
1861
1862
1862
=cut
1863
=cut
1863
1864
1865
#NOTE: We're keeping this for backwards compatibility
1864
sub _get_session_params {
1866
sub _get_session_params {
1865
    my $storage_method = C4::Context->preference('SessionStorage');
1867
    return Koha::Session->_get_session_params();
1866
    if ( $storage_method eq 'mysql' ) {
1867
        my $dbh = C4::Context->dbh;
1868
        return { dsn => "serializer:yamlxs;driver:MySQL;id:md5", dsn_args => { Handle => $dbh } };
1869
    }
1870
    elsif ( $storage_method eq 'Pg' ) {
1871
        my $dbh = C4::Context->dbh;
1872
        return { dsn => "serializer:yamlxs;driver:PostgreSQL;id:md5", dsn_args => { Handle => $dbh } };
1873
    }
1874
    elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) {
1875
        my $memcached = Koha::Caches->get_instance()->memcached_cache;
1876
        return { dsn => "serializer:yamlxs;driver:memcached;id:md5", dsn_args => { Memcached => $memcached } };
1877
    }
1878
    else {
1879
        # catch all defaults to tmp should work on all systems
1880
        my $dir = C4::Context::temporary_directory;
1881
        my $instance = C4::Context->config( 'database' ); #actually for packages not exactly the instance name, but generally safer to leave it as it is
1882
        return { dsn => "serializer:yamlxs;driver:File;id:md5", dsn_args => { Directory => "$dir/cgisess_$instance" } };
1883
    }
1884
}
1868
}
1885
1869
1870
#NOTE: We're keeping this for backwards compatibility
1886
sub get_session {
1871
sub get_session {
1887
    my $sessionID      = shift;
1872
    my $sessionID = shift;
1888
    my $params = _get_session_params();
1873
    my $session   = Koha::Session->get_session( { sessionID => $sessionID } );
1889
    my $session;
1890
    if( $sessionID ) { # find existing
1891
        CGI::Session::ErrorHandler->set_error( q{} ); # clear error, cpan issue #111463
1892
        $session = CGI::Session->load( $params->{dsn}, $sessionID, $params->{dsn_args} );
1893
    } else {
1894
        $session = CGI::Session->new( $params->{dsn}, $sessionID, $params->{dsn_args} );
1895
        # no need to flush here
1896
    }
1897
    return $session;
1874
    return $session;
1898
}
1875
}
1899
1876
(-)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 / +30 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
};
30

Return to bug 36098