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

(-)a/Koha/Allowlist.pm (+116 lines)
Line 0 Link Here
1
package Koha::Allowlist;
2
3
use Modern::Perl;
4
5
=head1 NAME
6
7
Koha::Allowlist - Allowlist implementation base class
8
9
=head1 API
10
11
=head2 Class Methods
12
13
=head3 new
14
15
    my $allow_list = Koha::Allowlist->new({ interface => 'staff' });
16
17
    Constructor.
18
19
    Interface must be 'staff' or 'opac' - defaults to 'opac'
20
21
=cut
22
23
sub new {
24
    my ($class, $args) = @_;
25
    $args = {} unless defined $args;
26
    my $self = bless ($args, $class);
27
    $self->{interface} =
28
      ( $args->{interface} && $args->{interface} eq 'staff' )
29
      ? 'staff'
30
      : 'opac';
31
    $self->{_entries} = {};
32
    return $self;
33
}
34
35
=head3 add
36
37
    $list->add( 'column1', 'column2' );
38
39
=cut
40
41
sub add {
42
    my ( $self, @args ) = @_;
43
    # TODO Could be extended by passing things like column3 => { staff => 1 }
44
    foreach my $arg ( @args ) {
45
        $self->{_entries}->{$arg} = 1;
46
    }
47
}
48
49
=head3 remove
50
51
    $list->remove( 'column3', 'column4' );
52
53
=cut
54
55
sub remove {
56
    my ( $self, @args ) = @_;
57
    foreach my $arg ( @args ) {
58
        delete $self->{_entries}->{$arg};
59
    }
60
}
61
62
=head3 apply
63
64
    my $blocked = $allowlist->apply({ input => $hashref, verbose => 1 });
65
66
    Apply an allowlist to input data. Note that we modify the $hashref argument!
67
    The verbose flag controls the warn statement.
68
69
    Returns a hash of blocked entries. Might be useful for logging purposes?
70
71
=cut
72
73
sub apply {
74
    my ( $self, $params ) = @_;
75
    my $input = $params->{input} or return;
76
77
    my $blocked = {};
78
    foreach my $key ( keys %$input ){
79
        unless ( $self->{_entries}->{$key} ) {
80
            $blocked->{$key} = delete $input->{ $key };
81
        }
82
    }
83
    warn 'Koha::Allowlist: apply blocked one or more keys' if keys %$blocked && $params->{verbose};
84
    return $blocked;
85
}
86
87
=head3 load
88
89
    my $fields = $self->load;
90
91
    Loads default allow list entries.
92
93
    This routine should be overridden when subclassing.
94
95
=cut
96
97
sub load {
98
    my ( $self ) = @_;
99
    # You could do something here like:
100
    # $self->add( 'myfield' );
101
}
102
103
=head3 unload
104
105
    $allowlist->unload;
106
107
    Clear all entries.
108
109
=cut
110
111
sub unload {
112
    my ( $self ) = @_;
113
    $self->{_entries} = {};
114
}
115
116
1;
(-)a/t/Koha/Allowlist.t (-1 / +45 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
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
use Test::More tests => 1;
20
21
use Koha::Allowlist;
22
23
subtest 'Allowlist' => sub {
24
    plan tests => 7;
25
26
    my $allowlist = Koha::Allowlist->new;
27
    $allowlist->load;
28
    is( $allowlist->apply, undef, 'No input returns undef' );
29
    is( %{$allowlist->apply({ input => {} })}, 0, 'Empty hash returns nothing blocked' );
30
31
    my @input = ( col1 => 1, col2 => 2, col3 => 3, col4 => 4 );
32
    my $blocked = $allowlist->apply({ input => { @input } });
33
    is( keys %$blocked, 4, 'Empty list blocks all' );
34
    $allowlist->add( 'col1' );
35
    $blocked = $allowlist->apply({ input => { @input } });
36
    is( keys %$blocked, 3, 'One field allowed' );
37
    is( $blocked->{col1}, undef, 'And that is col1' );
38
    $allowlist->remove( 'col1' );
39
    $blocked = $allowlist->apply({ input => { @input } });
40
    is( keys %$blocked, 4, 'Back to 4 after removing col1' );
41
    $allowlist->add( 'col2' );
42
    $allowlist->unload;
43
    $blocked = $allowlist->apply({ input => { @input } });
44
    is( keys %$blocked, 4, 'Same after unloading' );
45
};

Return to bug 28999