Bug 31378: Rename Auth Provider to Identity Provider and add Client.t tests
[koha.git] / admin / authtypes.pl
1 #!/usr/bin/perl
2
3 # Copyright 2002 paul.poulain@biblibre.com
4 # Copyright 2000-2002 Katipo Communications
5 # Copyright 2015 Koha Development Team
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27
28 use Koha::Authorities;
29 use Koha::Authority::Types;
30
31 my $input        = CGI->new;
32 my $authtypecode = $input->param('authtypecode');
33 my $op           = $input->param('op') || 'list';
34 my @messages;
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {   template_name   => "admin/authtypes.tt",
37         query           => $input,
38         type            => "intranet",
39         flagsrequired   => { parameters => 'manage_marc_frameworks' },
40     }
41 );
42
43 if ( $op eq 'add_form' ) {
44     my $authority_type;
45     if (defined $authtypecode) {
46         $authority_type = Koha::Authority::Types->find($authtypecode);
47     }
48
49     $template->param( authority_type => $authority_type );
50 } elsif ( $op eq 'add_validate' ) {
51     my $authtypecode       = $input->param('authtypecode');
52     my $authtypetext       = $input->param('authtypetext');
53     my $auth_tag_to_report = $input->param('auth_tag_to_report');
54     my $summary            = $input->param('summary');
55     my $is_a_modif         = $input->param('is_a_modif');
56
57     if ($is_a_modif) {
58         my $authority_type = Koha::Authority::Types->find($authtypecode);
59         $authority_type->authtypetext($authtypetext);
60         $authority_type->auth_tag_to_report($auth_tag_to_report);
61         $authority_type->summary($summary);
62         eval { $authority_type->store; };
63         if ($@) {
64             push @messages, { type => 'error', code => 'error_on_update' };
65         } else {
66             push @messages, { type => 'message', code => 'success_on_update' };
67         }
68     } else {
69         my $authority_type = Koha::Authority::Type->new(
70             {   authtypecode       => $authtypecode,
71                 authtypetext       => $authtypetext,
72                 auth_tag_to_report => $auth_tag_to_report,
73                 summary            => $summary,
74             }
75         );
76         eval { $authority_type->store; };
77         if ($@) {
78             push @messages, { type => 'error', code => 'error_on_insert' };
79         } else {
80             push @messages, { type => 'message', code => 'success_on_insert' };
81         }
82     }
83     $op = 'list';
84
85 } elsif ( $op eq 'delete_confirm' ) {
86     my $authority_type = Koha::Authority::Types->find($authtypecode);
87     my $authorities_using_it = Koha::Authorities->search( { authtypecode => $authtypecode } )->count;
88     $template->param(
89         authority_type       => $authority_type,
90         authorities_using_it => $authorities_using_it,
91     );
92 } elsif ( $op eq 'delete_confirmed' ) {
93     my $authorities_using_it = Koha::Authorities->search( { authtypecode => $authtypecode } )->count;
94     if ( $authorities_using_it == 0 ) {
95         my $authority_type = Koha::Authority::Types->find($authtypecode);
96         my $deleted = eval { $authority_type->delete; };
97
98         if ( $@ or not $deleted ) {
99             push @messages, { type => 'error', code => 'error_on_delete' };
100         } else {
101             push @messages, { type => 'message', code => 'success_on_delete' };
102         }
103     } else {
104         push @messages, { type => 'error', code => 'error_on_delete' };
105     }
106     $op = 'list';
107 }
108
109 if ( $op eq 'list' ) {
110     my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
111     $template->param( authority_types => $authority_types, );
112 }
113
114 $template->param(
115     messages => \@messages,
116     op       => $op,
117 );
118
119 output_html_with_http_headers $input, $cookie, $template->output;