Bug 36207: (RM follow-up) CSRF correction
[koha.git] / admin / restrictions.pl
1 #!/usr/bin/perl
2
3 # Copyright 2019 PTFS Europe
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 CGI qw ( -utf8 );
23 use Try::Tiny qw( try catch );
24
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use Koha::Patron::Restriction::Types;
28
29 my $input = CGI->new;
30 my $op    = $input->param('op') // 'list';
31 my $code  = uc $input->param('code');
32 my @messages = ();
33
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {
36         template_name   => "admin/restrictions.tt",
37         query           => $input,
38         type            => "intranet",
39         flagsrequired   => { parameters => 'manage_patron_restrictions' },
40         debug           => 1,
41     }
42 );
43
44 if ( $op eq 'add_form') {
45     # Get all existing restrictions, so we can do client-side validation
46     $template->param(
47         existing => scalar Koha::Patron::Restriction::Types->search()
48     );
49     if ($code) {
50         $template->param(
51             restriction => scalar Koha::Patron::Restriction::Types->find($code)
52         );
53     }
54 } elsif ( $op eq 'cud-add_validate' ) {
55
56     my $display_text       = $input->param('display_text');
57     my $lift_after_payment = $input->param('lift_after_payment');
58     my $fee_limit          = $input->param('fee_limit');
59     my $is_a_modif         = $input->param("is_a_modif");
60
61     if ($is_a_modif) {
62         # Check whether another restriction already has this display text
63         my $dupe = Koha::Patron::Restriction::Types->search(
64             {
65                 code         => { '!=' => $code },
66                 display_text => $display_text,
67             }
68         );
69         if ( $dupe->count ) {
70             push @messages, {
71                 type => 'error', code => 'duplicate_display_text'
72             };
73         } else {
74             my $restriction = Koha::Patron::Restriction::Types->find($code);
75             $restriction->display_text($display_text);
76             $restriction->lift_after_payment($lift_after_payment);
77             $restriction->fee_limit($fee_limit);
78             $restriction->store;
79             push @messages, { type => 'message', code => 'update_success' };
80         }
81     } else {
82         # Check whether another restriction already has this code
83         my $dupe = Koha::Patron::Restriction::Types->find($code);
84         if ($dupe) {
85             push @messages, {
86                 type => 'error', code => 'duplicate_code'
87             };
88         } else {
89             my $restriction = Koha::Patron::Restriction::Type->new(
90                 {
91                     code               => $code,
92                     display_text       => $display_text,
93                     lift_after_payment => $lift_after_payment,
94                     fee_limit          => $fee_limit
95                 }
96             );
97             $restriction->store;
98             push @messages, { type => 'message', code => 'add_success' };
99         }
100     }
101     $op = 'list';
102 } elsif ( $op eq 'cud-make_default' ) {
103     my $restriction = Koha::Patron::Restriction::Types->find($code);
104     $restriction->make_default;
105     $op = 'list';
106 } elsif ( $op eq 'delete_confirm' ) {
107     $template->param(
108         restriction => scalar Koha::Patron::Restriction::Types->find($code)
109     );
110 } elsif ( $op eq 'cud-delete_confirmed' ) {
111     try {
112         Koha::Patron::Restriction::Types->find($code)->delete;
113         push @messages, { type => 'message', code => 'delete_success' };
114     }
115     catch {
116         if ( blessed $_ ) {
117             if ( $_->isa('Koha::Exceptions::CannotDeleteDefault') ) {
118                 push @messages, { type => 'error', code => 'delete_default' };
119             }
120             elsif ( $_->isa('Koha::Exceptions::CannotDeleteSystem') ) {
121                 push @messages, { type => 'error', code => 'delete_system' };
122             }
123         }
124     };
125     $op = 'list';
126 }
127
128 $template->param(
129     messages => \@messages,
130     op       => $op
131 );
132
133 if ( $op eq 'list' ) {
134     my $restrictions = Koha::Patron::Restriction::Types->search();
135     $template->param(
136         restrictions => $restrictions,
137     )
138 }
139
140 output_html_with_http_headers $input, $cookie, $template->output;
141
142 exit 0;