From c893bf9c026fe60928ee2befc27d9101daf4a09c Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 23 May 2024 12:30:46 +0000 Subject: [PATCH] Bug 36941: Limit login branch list when IP restriction enabled This patch adds a new 'ip_limit' option to Koha::Template:Plugin::Branches To test: 1 - Set some branches in the system to have random IPs 2 - Set one branch to have your IP To find the IP in KTD I: Enabled ILS-DI Set ILS-DI:AuthorizedIPs to 1.1.1.1 Visit: http://localhost:8080/cgi-bin/koha/ilsdi.pl?service=GetAvailability&id=248&id_type=biblio And note the unauthorized IP listed 3 - Log out, confirm all branches are listed 4 - Log in, enable StaffLoginRestrictLibraryByIP 5 - Log out, confirm branches with IPs are removed 6 - Confirm branch matching your IP is listed 7 - Choose any of the branches and login 8 - Confirm login works --- Koha/Template/Plugin/Branches.pm | 17 +++++ .../intranet-tmpl/prog/en/modules/auth.tt | 2 +- .../Koha/Template/Plugin/Branches.t | 72 +++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/Koha/Template/Plugin/Branches.t diff --git a/Koha/Template/Plugin/Branches.pm b/Koha/Template/Plugin/Branches.pm index 7090530f38d..b9f20f3f033 100644 --- a/Koha/Template/Plugin/Branches.pm +++ b/Koha/Template/Plugin/Branches.pm @@ -71,6 +71,7 @@ sub all { my ( $self, $params ) = @_; my $selected = $params->{selected} // (); my $unfiltered = $params->{unfiltered} || 0; + my $ip_limit = $params->{ip_limit} && C4::Context->preference('StaffLoginRestrictLibraryByIP') || 0; my $search_params = $params->{search_params} || {}; my $do_not_select_my_library = $params->{do_not_select_my_library} || 0; # By default we select the library of the logged in user if no selected passed @@ -87,6 +88,22 @@ sub all { ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed; + + if( $ip_limit ){ + my $ip = $ENV{'REMOTE_ADDR'}; + my @ip_libraries = (); + for my $l (@$libraries) { + my $domain = $l->{branchip} // ''; + $domain =~ s|\.\*||g; + $domain =~ s/\s+//g; + unless( $domain && $ip !~ /^$domain/ ){ + push @ip_libraries, $l; + } + } + $libraries = \@ip_libraries; + } + + for my $l (@$libraries) { if ( grep { $l->{branchcode} eq $_ } @selected or not @selected diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt index 4d24895225d..253aaa7ff9a 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt @@ -151,7 +151,7 @@ diff --git a/t/db_dependent/Koha/Template/Plugin/Branches.t b/t/db_dependent/Koha/Template/Plugin/Branches.t new file mode 100755 index 00000000000..f9cc3a1fd90 --- /dev/null +++ b/t/db_dependent/Koha/Template/Plugin/Branches.t @@ -0,0 +1,72 @@ +#!/usr/bin/perl + +# 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 . + +use Modern::Perl; + +use Test::More tests => 2; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +BEGIN { + use_ok('Koha::Template::Plugin::Branches'); +} + +my $schema = Koha::Database->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'all' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + + my $plugin = Koha::Template::Plugin::Branches->new(); + subtest 'when given no parameters' => sub { + plan tests => 1; + my $libraries = $plugin->all(); + my $library_count = Koha::Libraries->search()->count(); + + is(scalar @$libraries, $library_count, 'We get all the branches'); + }; + + subtest 'when given parameter "ip_limit"' => sub { + plan tests => 4; + t::lib::Mocks::mock_preference('StaffLoginRestrictLibraryByIP', ''); + $ENV{REMOTE_ADDR} = '127.0.0.1'; + my $library = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => '127.0.0.2' } } ); + my $libraries = $plugin->all({ ip_limit => 1 }); + my $library_count = Koha::Libraries->search()->count(); + + is(scalar @$libraries, $library_count, 'We get all the libraries when ip_limit passed but StaffLoginRestrictLibraryIP not enabled'); + + t::lib::Mocks::mock_preference('StaffLoginRestrictLibraryByIP', '1'); + $library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.1'] })->count(); + $libraries = $plugin->all({ ip_limit => 1 }); + is(scalar @$libraries, $library_count , 'We remove non-matching libraries when ip_limit passed and StaffLoginRestrictLibraryIP enabled'); + + $ENV{REMOTE_ADDR} = '127.0.0.2'; + $libraries = $plugin->all({ ip_limit => 1 }); + $library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.2'] })->count(); + is(scalar @$libraries, $library_count, 'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches'); + + $library->branchip("127.0.*.*"); + $library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.2','127.0.*.*'] })->count(); + is(scalar @$libraries, $library_count, 'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches patternwise'); + }; + + $schema->storage->txn_rollback; +}; -- 2.39.2