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

(-)a/Koha/Template/Plugin/Branches.pm (+17 lines)
Lines 71-76 sub all { Link Here
71
    my ( $self, $params ) = @_;
71
    my ( $self, $params ) = @_;
72
    my $selected = $params->{selected} // ();
72
    my $selected = $params->{selected} // ();
73
    my $unfiltered = $params->{unfiltered} || 0;
73
    my $unfiltered = $params->{unfiltered} || 0;
74
    my $ip_limit = $params->{ip_limit} && C4::Context->preference('StaffLoginRestrictLibraryByIP') || 0;
74
    my $search_params = $params->{search_params} || {};
75
    my $search_params = $params->{search_params} || {};
75
    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
76
    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
76
77
Lines 87-92 sub all { Link Here
87
      ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
88
      ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
88
      : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
89
      : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
89
90
91
92
    if( $ip_limit ){
93
        my $ip = $ENV{'REMOTE_ADDR'};
94
        my @ip_libraries = ();
95
        for my $l (@$libraries) {
96
            my $domain = $l->{branchip} // '';
97
            $domain =~ s|\.\*||g;
98
            $domain =~ s/\s+//g;
99
            unless( $domain && $ip !~ /^$domain/ ){
100
                push @ip_libraries, $l;
101
            }
102
        }
103
        $libraries = \@ip_libraries;
104
    }
105
106
90
    for my $l (@$libraries) {
107
    for my $l (@$libraries) {
91
        if ( grep { $l->{branchcode} eq $_ } @selected
108
        if ( grep { $l->{branchcode} eq $_ } @selected
92
            or  not @selected
109
            or  not @selected
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/auth.tt (-1 / +1 lines)
Lines 151-157 Link Here
151
                <select name="branch" id="set-library-branch" class="input" tabindex="3">
151
                <select name="branch" id="set-library-branch" class="input" tabindex="3">
152
                <option value="">My library</option>
152
                <option value="">My library</option>
153
            [% END %]
153
            [% END %]
154
                [% FOREACH l IN Branches.all( unfiltered => 1 ) %]
154
                [% FOREACH l IN Branches.all( unfiltered => 1 ip_limit => 1 ) %]
155
                    <option value="[% l.branchcode | html %]">[% l.branchname | html %]</option>
155
                    <option value="[% l.branchcode | html %]">[% l.branchname | html %]</option>
156
                 [% END %]
156
                 [% END %]
157
            </select>
157
            </select>
(-)a/t/db_dependent/Koha/Template/Plugin/Branches.t (-1 / +72 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, see <http://www.gnu.org/licenses>.
16
17
use Modern::Perl;
18
19
use Test::More tests => 2;
20
21
use t::lib::Mocks;
22
use t::lib::TestBuilder;
23
24
BEGIN {
25
    use_ok('Koha::Template::Plugin::Branches');
26
}
27
28
my $schema  = Koha::Database->schema;
29
my $builder = t::lib::TestBuilder->new;
30
31
subtest 'all' => sub {
32
    plan tests => 2;
33
34
    $schema->storage->txn_begin;
35
36
37
    my $plugin = Koha::Template::Plugin::Branches->new();
38
    subtest 'when given no parameters' => sub {
39
        plan tests => 1;
40
        my $libraries = $plugin->all();
41
        my $library_count = Koha::Libraries->search()->count();
42
43
        is(scalar @$libraries, $library_count, 'We get all the branches');
44
    };
45
46
    subtest 'when given parameter "ip_limit"' => sub {
47
        plan tests => 4;
48
        t::lib::Mocks::mock_preference('StaffLoginRestrictLibraryByIP', '');
49
        $ENV{REMOTE_ADDR} = '127.0.0.1';
50
        my $library = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => '127.0.0.2' } } );
51
        my $libraries = $plugin->all({ ip_limit => 1 });
52
        my $library_count = Koha::Libraries->search()->count();
53
54
        is(scalar @$libraries, $library_count, 'We get all the libraries when ip_limit passed but StaffLoginRestrictLibraryIP not enabled');
55
56
        t::lib::Mocks::mock_preference('StaffLoginRestrictLibraryByIP', '1');
57
        $library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.1'] })->count();
58
        $libraries = $plugin->all({ ip_limit => 1 });
59
        is(scalar @$libraries, $library_count , 'We remove non-matching libraries when ip_limit passed and StaffLoginRestrictLibraryIP enabled');
60
61
        $ENV{REMOTE_ADDR} = '127.0.0.2';
62
        $libraries = $plugin->all({ ip_limit => 1 });
63
        $library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.2'] })->count();
64
        is(scalar @$libraries, $library_count, 'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches');
65
66
        $library->branchip("127.0.*.*");
67
        $library_count = Koha::Libraries->search({ branchip => [undef,'127.0.0.2','127.0.*.*'] })->count();
68
        is(scalar @$libraries, $library_count, 'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches patternwise');
69
    };
70
71
    $schema->storage->txn_rollback;
72
};

Return to bug 36941