From 89fd1a79d66d9520c3afc1b13bfb3542f22c3519 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Sat, 11 Sep 2021 11:51:38 +0000 Subject: [PATCH] Bug 28999: Add Koha::DenyList as counterpart Content-Type: text/plain; charset=utf-8 --- Koha/DenyList.pm | 60 ++++++++++++++++++++++++++++++++++++++++++++++ t/Koha/AllowList.t | 23 ++++++++++++++++-- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 Koha/DenyList.pm diff --git a/Koha/DenyList.pm b/Koha/DenyList.pm new file mode 100644 index 0000000000..518858854c --- /dev/null +++ b/Koha/DenyList.pm @@ -0,0 +1,60 @@ +package Koha::DenyList; + +# 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 base qw(Koha::AllowList); + +=head1 NAME + +Koha::DenyList - Deny list implementation base class + +=head1 API + +=head2 Class Methods + +=head3 apply + + my $result = $denylist->apply({ + input => $hashref | $arrayref, dump => $dump, + }); + + Apply a denylist to input data. Returns result as hash- or arrayref. + If you pass a dump hashref, blocked entries will be written. When you + pass an arrayref, the dump looks like: { blocked1 => 1, .. } + +=cut + +sub apply { + my ( $self, $params ) = @_; + my $input = $params->{input} or return; + return if ref($input) ne 'HASH' && ref($input) ne 'ARRAY'; + + my $dump = {}; + my $result; + if( ref($input) eq 'ARRAY' ) { + $result = []; + map { !$self->{_entries}->{$_} ? ( push @$result, $_ ) : ( $dump->{$_} = 1 ); } @$input; + } else { # Handle hashref + $result = {}; + map { !$self->{_entries}->{$_} ? ( $result->{$_} = $input->{$_} ) : ( $dump->{$_} = $input->{$_} ); } keys %$input; + } + map { $params->{dump}->{$_} = $dump->{$_}; } keys %$dump if ref($params->{dump}) eq 'HASH'; + return $result; +} + +1; diff --git a/t/Koha/AllowList.t b/t/Koha/AllowList.t index ace4d96404..62b26a3172 100755 --- a/t/Koha/AllowList.t +++ b/t/Koha/AllowList.t @@ -16,10 +16,10 @@ # along with Koha; if not, see . use Modern::Perl; -use Data::Dumper qw( Dumper ); -use Test::More tests => 1; +use Test::More tests => 2; use Koha::AllowList; +use Koha::DenyList; subtest 'AllowList' => sub { plan tests => 17; @@ -73,3 +73,22 @@ subtest 'AllowList' => sub { # Test keys method is( join(',', $allowlist->keys), 'col1,col4', 'List current entries' ); }; + +subtest 'DenyList' => sub { + plan tests => 6; + + my $defaults = [ 'col1', 'col3' ]; + my $denylist = Koha::DenyList->new({ defaults => $defaults }); + my $input = { col1 => 1, col2 => 2, col3 => 3, col4 => 4 }; + my $blocked = {}; + my $result = $denylist->apply({ input => $input, dump => $blocked }); + is( keys %$result, 2, 'Two remain' ); + is( $result->{col2}, 2, 'col2 was ok' ); + is( keys %$blocked, 2, 'Two got denied' ); + is( $blocked->{col1}, 1, 'col1 blocked' ); + $denylist->remove('col1'); + $denylist->add('col5'); + is( $denylist->check({ input => $input }), q{}, 'input still fails' ); + delete $input->{col3}; + is( $denylist->check({ input => $input }), 1, 'input is ok now' ); +}; -- 2.20.1