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

(-)a/Koha/Template/Plugin/Branches.pm (-17 / +33 lines)
Lines 69-104 sub GetURL { Link Here
69
69
70
sub all {
70
sub all {
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 $search_params = $params->{search_params} || {};
74
    my $ip_limit      = $params->{ip_limit} && C4::Context->preference('StaffLoginRestrictLibraryByIP') || 0;
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
75
    my $search_params = $params->{search_params}                                                        || {};
76
    my $do_not_select_my_library = $params->{do_not_select_my_library}
77
        || 0;    # By default we select the library of the logged in user if no selected passed
76
78
77
    if ( !$unfiltered ) {
79
    if ( !$unfiltered ) {
78
        $search_params->{only_from_group} = $params->{only_from_group} || 0;
80
        $search_params->{only_from_group} = $params->{only_from_group} || 0;
79
    }
81
    }
80
82
81
    my @selected =
83
    my @selected =
82
      ref $selected eq 'Koha::Libraries'
84
        ref $selected eq 'Koha::Libraries'
83
      ? $selected->get_column('branchcode')
85
        ? $selected->get_column('branchcode')
84
      : ( $selected // () );
86
        : ( $selected // () );
85
87
86
    my $libraries = $unfiltered
88
    my $libraries =
87
      ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
89
        $unfiltered
88
      : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
90
        ? Koha::Libraries->search( $search_params, { order_by => ['branchname'] } )->unblessed
91
        : Koha::Libraries->search_filtered( $search_params, { order_by => ['branchname'] } )->unblessed;
92
93
    if ($ip_limit) {
94
        my $ip           = $ENV{'REMOTE_ADDR'};
95
        my @ip_libraries = ();
96
        for my $l (@$libraries) {
97
            my $domain = $l->{branchip} // '';
98
            $domain =~ s|\.\*||g;
99
            $domain =~ s/\s+//g;
100
            unless ( $domain && $ip !~ /^$domain/ ) {
101
                push @ip_libraries, $l;
102
            }
103
        }
104
        $libraries = \@ip_libraries;
105
    }
89
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
93
                and not $do_not_select_my_library
110
            and not $do_not_select_my_library
94
                and C4::Context->userenv
111
            and C4::Context->userenv
95
                and $l->{branchcode} eq ( C4::Context->userenv->{branch} // q{} ) )
112
            and $l->{branchcode} eq ( C4::Context->userenv->{branch} // q{} ) )
96
        {
113
        {
97
             $l->{selected} = 1;
114
            $l->{selected} = 1;
98
        }
115
        }
99
    }
116
    }
100
117
101
102
    return $libraries;
118
    return $libraries;
103
}
119
}
104
120
(-)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 / +83 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
    my $plugin = Koha::Template::Plugin::Branches->new();
37
    subtest 'when given no parameters' => sub {
38
        plan tests => 1;
39
        my $libraries     = $plugin->all();
40
        my $library_count = Koha::Libraries->search()->count();
41
42
        is( scalar @$libraries, $library_count, 'We get all the branches' );
43
    };
44
45
    subtest 'when given parameter "ip_limit"' => sub {
46
        plan tests => 4;
47
        t::lib::Mocks::mock_preference( 'StaffLoginRestrictLibraryByIP', '' );
48
        $ENV{REMOTE_ADDR} = '127.0.0.1';
49
        my $library   = $builder->build_object( { class => 'Koha::Libraries', value => { branchip => '127.0.0.2' } } );
50
        my $libraries = $plugin->all( { ip_limit => 1 } );
51
        my $library_count = Koha::Libraries->search()->count();
52
53
        is(
54
            scalar @$libraries, $library_count,
55
            'We get all the libraries when ip_limit passed but StaffLoginRestrictLibraryIP not enabled'
56
        );
57
58
        t::lib::Mocks::mock_preference( 'StaffLoginRestrictLibraryByIP', '1' );
59
        $library_count = Koha::Libraries->search( { branchip => [ undef, '127.0.0.1' ] } )->count();
60
        $libraries     = $plugin->all( { ip_limit => 1 } );
61
        is(
62
            scalar @$libraries, $library_count,
63
            'We remove non-matching libraries when ip_limit passed and StaffLoginRestrictLibraryIP enabled'
64
        );
65
66
        $ENV{REMOTE_ADDR} = '127.0.0.2';
67
        $libraries        = $plugin->all( { ip_limit => 1 } );
68
        $library_count    = Koha::Libraries->search( { branchip => [ undef, '127.0.0.2' ] } )->count();
69
        is(
70
            scalar @$libraries, $library_count,
71
            'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches'
72
        );
73
74
        $library->branchip("127.0.*.*");
75
        $library_count = Koha::Libraries->search( { branchip => [ undef, '127.0.0.2', '127.0.*.*' ] } )->count();
76
        is(
77
            scalar @$libraries, $library_count,
78
            'We get all the expected libraries when ip_limit passed and StaffLoginRestrictLibraryIP and IP matches patternwise'
79
        );
80
    };
81
82
    $schema->storage->txn_rollback;
83
};

Return to bug 36941