Bugzilla – Attachment 162200 Details for
Bug 36098
Create Koha::Session module
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 36098: Add Koha::Session module to ease session handling
Bug-36098-Add-KohaSession-module-to-ease-session-h.patch (text/plain), 6.82 KB, created by
Martin Renvoize (ashimema)
on 2024-02-15 11:57:56 UTC
(
hide
)
Description:
Bug 36098: Add Koha::Session module to ease session handling
Filename:
MIME Type:
Creator:
Martin Renvoize (ashimema)
Created:
2024-02-15 11:57:56 UTC
Size:
6.82 KB
patch
obsolete
>From 694d1e122b03b42e131122ba6e591bab7a2cd376 Mon Sep 17 00:00:00 2001 >From: David Cook <dcook@prosentient.com.au> >Date: Thu, 15 Feb 2024 02:49:18 +0000 >Subject: [PATCH] Bug 36098: Add Koha::Session module to ease session handling > >This patch adds a Koha::Session module that makes it easier >to work with Koha sessions without needing the full C4::Auth module. > >Test plan: >0. Apply the patch >1. Run the following unit tests: >prove ./t/db_dependent/Auth.t >prove ./t/db_dependent/Auth_with_cas.t >prove ./t/db_dependent/Koha/Session.t >2. Observe that they all pass > >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> >--- > C4/Auth.pm | 35 ++++--------------- > Koha/Session.pm | 63 +++++++++++++++++++++++++++++++++++ > t/db_dependent/Koha/Session.t | 29 ++++++++++++++++ > 3 files changed, 98 insertions(+), 29 deletions(-) > create mode 100644 Koha/Session.pm > create mode 100755 t/db_dependent/Koha/Session.t > >diff --git a/C4/Auth.pm b/C4/Auth.pm >index ca4ee0c8af4..abce728db12 100644 >--- a/C4/Auth.pm >+++ b/C4/Auth.pm >@@ -52,6 +52,7 @@ use C4::Log qw( logaction ); > use Koha::CookieManager; > use Koha::Auth::Permissions; > use Koha::Token; >+use Koha::Session; > > # use utf8; > >@@ -1864,39 +1865,15 @@ will be created. > > =cut > >+#NOTE: We're keeping this for backwards compatibility > sub _get_session_params { >- my $storage_method = C4::Context->preference('SessionStorage'); >- if ( $storage_method eq 'mysql' ) { >- my $dbh = C4::Context->dbh; >- return { dsn => "serializer:yamlxs;driver:MySQL;id:md5", dsn_args => { Handle => $dbh } }; >- } >- elsif ( $storage_method eq 'Pg' ) { >- my $dbh = C4::Context->dbh; >- return { dsn => "serializer:yamlxs;driver:PostgreSQL;id:md5", dsn_args => { Handle => $dbh } }; >- } >- elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) { >- my $memcached = Koha::Caches->get_instance()->memcached_cache; >- return { dsn => "serializer:yamlxs;driver:memcached;id:md5", dsn_args => { Memcached => $memcached } }; >- } >- else { >- # catch all defaults to tmp should work on all systems >- my $dir = C4::Context::temporary_directory; >- my $instance = C4::Context->config( 'database' ); #actually for packages not exactly the instance name, but generally safer to leave it as it is >- return { dsn => "serializer:yamlxs;driver:File;id:md5", dsn_args => { Directory => "$dir/cgisess_$instance" } }; >- } >+ return Koha::Session->_get_session_params(); > } > >+#NOTE: We're keeping this for backwards compatibility > sub get_session { >- my $sessionID = shift; >- my $params = _get_session_params(); >- my $session; >- if( $sessionID ) { # find existing >- CGI::Session::ErrorHandler->set_error( q{} ); # clear error, cpan issue #111463 >- $session = CGI::Session->load( $params->{dsn}, $sessionID, $params->{dsn_args} ); >- } else { >- $session = CGI::Session->new( $params->{dsn}, $sessionID, $params->{dsn_args} ); >- # no need to flush here >- } >+ my $sessionID = shift; >+ my $session = Koha::Session->get_session( { sessionID => $sessionID } ); > return $session; > } > >diff --git a/Koha/Session.pm b/Koha/Session.pm >new file mode 100644 >index 00000000000..68a7b2787a2 >--- /dev/null >+++ b/Koha/Session.pm >@@ -0,0 +1,63 @@ >+package Koha::Session; >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+use CGI::Session; >+ >+use C4::Context; >+use Koha::Caches; >+ >+sub _get_session_params { >+ my $class = shift; >+ my $storage_method = C4::Context->preference('SessionStorage'); >+ if ( $storage_method eq 'mysql' ) { >+ my $dbh = C4::Context->dbh; >+ return { dsn => "serializer:yamlxs;driver:MySQL;id:md5", dsn_args => { Handle => $dbh } }; >+ } elsif ( $storage_method eq 'Pg' ) { >+ my $dbh = C4::Context->dbh; >+ return { dsn => "serializer:yamlxs;driver:PostgreSQL;id:md5", dsn_args => { Handle => $dbh } }; >+ } elsif ( $storage_method eq 'memcached' && Koha::Caches->get_instance->memcached_cache ) { >+ my $memcached = Koha::Caches->get_instance()->memcached_cache; >+ return { dsn => "serializer:yamlxs;driver:memcached;id:md5", dsn_args => { Memcached => $memcached } }; >+ } else { >+ >+ # catch all defaults to tmp should work on all systems >+ my $dir = C4::Context::temporary_directory; >+ my $instance = C4::Context->config('database') >+ ; #actually for packages not exactly the instance name, but generally safer to leave it as it is >+ return { dsn => "serializer:yamlxs;driver:File;id:md5", dsn_args => { Directory => "$dir/cgisess_$instance" } }; >+ } >+ return; >+} >+ >+sub get_session { >+ my ( $class, $args ) = @_; >+ my $sessionID = $args->{sessionID}; >+ my $params = $class->_get_session_params(); >+ my $session; >+ if ($sessionID) { # find existing >+ CGI::Session::ErrorHandler->set_error(q{}); # clear error, cpan issue #111463 >+ $session = CGI::Session->load( $params->{dsn}, $sessionID, $params->{dsn_args} ); >+ } else { >+ $session = CGI::Session->new( $params->{dsn}, $sessionID, $params->{dsn_args} ); >+ >+ # no need to flush here >+ } >+ return $session; >+} >+ >+1; >diff --git a/t/db_dependent/Koha/Session.t b/t/db_dependent/Koha/Session.t >new file mode 100755 >index 00000000000..3fe7cff42fc >--- /dev/null >+++ b/t/db_dependent/Koha/Session.t >@@ -0,0 +1,29 @@ >+#!/usr/bin/perl >+ >+use Modern::Perl; >+use Test::More tests => 1; >+ >+use t::lib::TestBuilder; >+use C4::Auth; >+use Koha::Session; >+ >+my $schema = Koha::Database->schema; >+my $builder = t::lib::TestBuilder->new; >+ >+subtest 'basic session fetch' => sub { >+ plan tests => 2; >+ >+ $schema->storage->txn_begin; >+ >+ my $patron = >+ $builder->build_object( { class => 'Koha::Patrons', value => { userid => 'superman' } } ); >+ >+ my $basic_session = C4::Auth::create_basic_session( { patron => $patron, interface => 'staff' } ); >+ is( $basic_session->param('id'), 'superman', 'basic session created as expected' ); >+ $basic_session->flush; >+ >+ my $session = Koha::Session->get_session( { sessionID => $basic_session->id } ); >+ is( $session->param('id'), 'superman', 'basic session fetched as expected' ); >+ >+ $schema->storage->txn_rollback; >+}; >-- >2.43.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 36098
:
162167
| 162200 |
162201
|
162204
|
162205
|
162228
|
162231
|
162314