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

(-)a/Koha/Validator.pm (+57 lines)
Line 0 Link Here
1
package Koha::Validator;
2
3
#TODO License
4
5
use Modern::Perl;
6
use List::Util qw(any);
7
use Koha::Exceptions;
8
use Koha::Email;
9
10
our $default_rules = {
11
    digits_only => qr/^\d+$/,
12
    email       => \&_check_email,
13
};
14
15
#TODO POD
16
17
sub new {
18
    my ( $class, $params ) = @_;
19
    my $self = bless $params // {}, $class;
20
    return $self;
21
}
22
23
sub load {
24
    my ( $self, $rules ) = @_;
25
    $self->{rules} = $rules if ref($rules) eq 'HASH';
26
}
27
28
sub check_array {    #TODO Add later
29
}
30
31
sub check_hash {
32
    my ( $self, $values ) = @_;
33
    return if ref($values) ne 'HASH';
34
35
    foreach my $key (%$values) {
36
        my $rule = $self->{rules}->{$key} or next;
37
        $rule = $default_rules->{$rule} if ref($rule) eq '';
38
        if ( ref($rule) eq 'Regexp' ) {
39
            next if $values->{$key} =~ $rule;
40
        } elsif ( ref($rule) eq 'CODE' ) {
41
            next if $rule->( $values->{$key} );
42
        } elsif ( ref($rule) eq 'ARRAY' ) {
43
            next if any { $_ eq $values->{$key} } @$rule;
44
        }
45
        Koha::Exceptions::BadParameter->throw( parameter => $key );
46
    }
47
    return 1;
48
}
49
50
sub _check_email {    # TODO Incorporate into Koha::Email?
51
    my $email = shift;
52
    return 0 if !Koha::Email->is_valid($email);
53
    return 0 if $email =~ /[\`\$]/;
54
    return 1;
55
}
56
57
1;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/scheduler.tt (+4 lines)
Lines 31-38 Link Here
31
31
32
[% IF ( job_add_failed ) %]
32
[% IF ( job_add_failed ) %]
33
<div class="dialog message">Failed to add scheduled task</div>
33
<div class="dialog message">Failed to add scheduled task</div>
34
[% ELSIF validation_failed %]
35
    <div class="dialog message">Validation for [% invalid_field | html %] failed</div>
34
[% END %]
36
[% END %]
35
37
38
[% UNLESS validation_failed %]
36
<form name="form1" action="scheduler.pl" method="post">
39
<form name="form1" action="scheduler.pl" method="post">
37
<input type="hidden" name="mode" value="job_add" />
40
<input type="hidden" name="mode" value="job_add" />
38
41
Lines 73-78 Link Here
73
</ol></fieldset>
76
</ol></fieldset>
74
<fieldset class="action"><input class="btn btn-primary" type="submit" value="Save" /></fieldset>
77
<fieldset class="action"><input class="btn btn-primary" type="submit" value="Save" /></fieldset>
75
</form>
78
</form>
79
[% END %]
76
80
77
[% IF ( JOBS ) %]<h2>Jobs already entered</h2>
81
[% IF ( JOBS ) %]<h2>Jobs already entered</h2>
78
<table>
82
<table>
(-)a/t/Koha/Validator.t (+58 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2023 Rijksmuseum
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 1;
23
use Test::Warn;
24
use Try::Tiny;
25
26
use Koha::Validator;
27
28
subtest 'trivial tests' => sub {
29
    plan tests => 3;
30
31
    my $rules = {
32
        test01 => 'digits_only',
33
        test02 => [ 1, 2 ],
34
        test03 => sub { 0 },
35
        test04 => 'email',
36
        test05 => qr/test/,
37
    };
38
    my $validator = Koha::Validator->new( { rules => $rules } );
39
    try {
40
        $validator->check_hash(
41
            { test01 => 2, test02 => 1, test04 => 'marcel@test.nl', test05 => 'includes test word' } );
42
        ok( 1, 'validator green' );
43
    } catch {
44
        ok(0);
45
    };
46
    try {
47
        $validator->check_hash( { test01 => 2, test02 => 1, test03 => 'nomatterwhat' } );
48
        ok( 0, 'we should not be here' );
49
    } catch {
50
        ok( 1, 'validator red' );
51
    };
52
    try {
53
        $validator->check_hash( { test04 => 'withbacktic`ls`@test.com' } );
54
        ok( 0, 'we should not be here' );
55
    } catch {
56
        ok( 1, 'validator red for email' );
57
    };
58
};
(-)a/tools/scheduler.pl (-17 / +34 lines)
Lines 18-33 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use CGI qw ( -utf8 );
22
use Try::Tiny qw(try catch);
23
21
use C4::Context;
24
use C4::Context;
22
use C4::Scheduler qw( add_at_job get_jobs remove_at_job );
25
use C4::Scheduler qw( add_at_job get_jobs remove_at_job );
23
use C4::Reports::Guided qw( get_saved_reports );
26
use C4::Reports::Guided qw( get_saved_reports );
24
use C4::Auth qw( get_template_and_user );
27
use C4::Auth qw( get_template_and_user );
25
use CGI qw ( -utf8 );
26
use C4::Output qw( output_html_with_http_headers );
28
use C4::Output qw( output_html_with_http_headers );
27
use Koha::DateUtils qw( dt_from_string );;
29
30
use Koha::DateUtils qw( dt_from_string );
31
use Koha::Validator;
28
32
29
my $input = CGI->new;
33
my $input = CGI->new;
30
my $base;
34
my $base;
35
my $rules = {
36
    id        => 'digits_only',
37
    mode      => [ q(), qw(job_add job_change) ],
38
    delete    => [qw(Delete)],
39
    job_id    => 'digits_only',
40
    starttime => qr/^\d{2}:?\d{2}$/,
41
    startdate => qr/^\d{4}-?\d{2}-?\d{2}$/,
42
    report    => 'digits_only',
43
    format    => [ q{}, qw( text html csv tsv ) ],
44
    email     => 'email'
45
};
31
46
32
if ( C4::Context->config('supportdir') ) {
47
if ( C4::Context->config('supportdir') ) {
33
     $base = C4::Context->config('supportdir');
48
     $base = C4::Context->config('supportdir');
Lines 47-55 my ( $template, $borrowernumber, $cookie ) = get_template_and_user( Link Here
47
    }
62
    }
48
);
63
);
49
64
50
my $mode = $input->param('mode');
65
my $mode = $input->param('mode') // q{};
51
my $id   = $input->param('id');
66
my $id   = $input->param('id');
52
67
68
try {
69
    Koha::Validator->new({ rules => $rules })->check_hash(scalar $input->Vars);
70
} catch {
71
    my $field = 'some field';
72
    if ( ref($_) =~ /Koha::Exception/ ) {
73
        warn $_->full_message;
74
        $field = $_->parameter if $_->parameter;
75
    }
76
    $template->param( validation_failed => 1, invalid_field => $field );
77
    output_html_with_http_headers $input, $cookie, $template->output;
78
    exit;
79
};
80
53
if ( $mode eq 'job_add' ) {
81
if ( $mode eq 'job_add' ) {
54
82
55
    my $startdate = dt_from_string( scalar $input->param('startdate'), 'iso' )->ymd;
83
    my $startdate = dt_from_string( scalar $input->param('startdate'), 'iso' )->ymd;
Lines 59-81 if ( $mode eq 'job_add' ) { Link Here
59
    my $starttime = $input->param('starttime');
87
    my $starttime = $input->param('starttime');
60
    $starttime =~ s/\://g;
88
    $starttime =~ s/\://g;
61
    my $start  = $startdate . $starttime;
89
    my $start  = $startdate . $starttime;
62
    my $report = $input->param('report');
90
    my $report = $input->param('report') // 0;
63
    my $format = $input->param('format');
91
    my $format = $input->param('format') // q{};
64
    my $email  = $input->param('email');
92
    my $email  = $input->param('email') // q{};
65
    my $command =
93
    my $command =
66
        "export KOHA_CONF=\"$CONFIG_NAME\"; " .
94
        "export KOHA_CONF=\"$CONFIG_NAME\"; " .
67
        "$base/cronjobs/runreport.pl $report --format=$format --to=$email";
95
        "$base/cronjobs/runreport.pl $report --format=$format --to=$email";
68
96
69
#FIXME commit ea899bc55933cd74e4665d70b1c48cab82cd1257 added recurring parameter (it is not in template) and call to add_cron_job (undefined)
70
#    my $recurring = $input->param('recurring');
71
#    if ($recurring) {
72
#        my $frequency = $input->param('frequency');
73
#        add_cron_job( $start, $command );
74
#    }
75
#    else {
76
#        #here was the the unless ( add_at_job
77
#    }
78
79
    unless ( add_at_job( $start, $command ) ) {
97
    unless ( add_at_job( $start, $command ) ) {
80
        $template->param( job_add_failed => 1 );
98
        $template->param( job_add_failed => 1 );
81
    }
99
    }
82
- 

Return to bug 34349