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

(-)a/Koha/DenyList.pm (+60 lines)
Line 0 Link Here
1
package Koha::DenyList;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use base qw(Koha::AllowList);
21
22
=head1 NAME
23
24
Koha::DenyList - Deny list implementation base class
25
26
=head1 API
27
28
=head2 Class Methods
29
30
=head3 apply
31
32
    my $result = $denylist->apply({
33
        input => $hashref | $arrayref, dump => $dump,
34
    });
35
36
    Apply a denylist to input data. Returns result as hash- or arrayref.
37
    If you pass a dump hashref, blocked entries will be written. When you
38
    pass an arrayref, the dump looks like: { blocked1 => 1, .. }
39
40
=cut
41
42
sub apply {
43
    my ( $self, $params ) = @_;
44
    my $input = $params->{input} or return;
45
    return if ref($input) ne 'HASH' && ref($input) ne 'ARRAY';
46
47
    my $dump = {};
48
    my $result;
49
    if( ref($input) eq 'ARRAY' ) {
50
        $result = [];
51
        map { !$self->{_entries}->{$_} ? ( push @$result, $_ ) : ( $dump->{$_} = 1 );  } @$input;
52
    } else { # Handle hashref
53
        $result = {};
54
        map { !$self->{_entries}->{$_} ? ( $result->{$_} = $input->{$_} ) : ( $dump->{$_} = $input->{$_} );  } keys %$input;
55
    }
56
    map { $params->{dump}->{$_} = $dump->{$_}; } keys %$dump if ref($params->{dump}) eq 'HASH';
57
    return $result;
58
}
59
60
1;
(-)a/t/Koha/AllowList.t (-3 / +21 lines)
Lines 16-25 Link Here
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
use Data::Dumper qw( Dumper );
19
use Test::More tests => 2;
20
use Test::More tests => 1;
21
20
22
use Koha::AllowList;
21
use Koha::AllowList;
22
use Koha::DenyList;
23
23
24
subtest 'AllowList' => sub {
24
subtest 'AllowList' => sub {
25
    plan tests => 17;
25
    plan tests => 17;
Lines 73-75 subtest 'AllowList' => sub { Link Here
73
    # Test keys method
73
    # Test keys method
74
    is( join(',', $allowlist->keys), 'col1,col4', 'List current entries' );
74
    is( join(',', $allowlist->keys), 'col1,col4', 'List current entries' );
75
};
75
};
76
- 
76
77
subtest 'DenyList' => sub {
78
    plan tests => 6;
79
80
    my $defaults = [ 'col1', 'col3' ];
81
    my $denylist = Koha::DenyList->new({ defaults => $defaults });
82
    my $input = { col1 => 1, col2 => 2, col3 => 3, col4 => 4 };
83
    my $blocked = {};
84
    my $result = $denylist->apply({ input => $input, dump => $blocked });
85
    is( keys %$result, 2, 'Two remain' );
86
    is( $result->{col2}, 2, 'col2 was ok' );
87
    is( keys %$blocked, 2, 'Two got denied' );
88
    is( $blocked->{col1}, 1, 'col1 blocked' );
89
    $denylist->remove('col1');
90
    $denylist->add('col5');
91
    is( $denylist->check({ input => $input }), q{}, 'input still fails' );
92
    delete $input->{col3};
93
    is( $denylist->check({ input => $input }), 1, 'input is ok now' );
94
};

Return to bug 28999