@@ -, +, @@
enabled
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
---
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
--- a/Koha/Template/Plugin/Branches.pm
+++ a/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
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt
+++ a/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt
@@ -151,7 +151,7 @@
--- a/t/db_dependent/Koha/Template/Plugin/Branches.t
+++ a/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;
+};
--