Bug 31378: (follow-up) Fix QA concerns
[koha.git] / admin / identity_providers.pl
1 #!/usr/bin/perl
2
3 # Copyright 2022 Theke Solutions
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 Scalar::Util qw( blessed );
24 use Try::Tiny qw( catch try );
25
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28
29 use Koha::Auth::Identity::Providers;
30
31 my $input         = CGI->new;
32 my $op            = $input->param('op') || 'list';
33 my $domain_ops    = $input->param('domain_ops');
34 my $identity_provider_id = $input->param('identity_provider_id');
35 my $identity_provider;
36
37 $identity_provider = Koha::Auth::Identity::Providers->find($identity_provider_id)
38     unless !$identity_provider_id;
39
40 my $template_name = $domain_ops ? 'admin/identity_provider_domains.tt' : 'admin/identity_providers.tt';
41
42 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
43     {   template_name   => $template_name,
44         query           => $input,
45         type            => "intranet",
46         flagsrequired   => { parameters => 'manage_identity_providers' },
47     }
48 );
49
50 my @messages;
51
52 if ( !$domain_ops && $op eq 'add' ) {
53
54     my $code        = $input->param('code');
55     my $config      = $input->param('config');
56     my $description = $input->param('description');
57     my $icon_url    = $input->param('icon_url');
58     my $mapping     = $input->param('mapping');
59     my $matchpoint  = $input->param('matchpoint'),
60     my $protocol    = $input->param('protocol');
61
62     try {
63         my $provider = Koha::Auth::Identity::Provider->new(
64             {   code        => $code,
65                 config      => $config,
66                 description => $description,
67                 icon_url    => $icon_url,
68                 mapping     => $mapping,
69                 matchpoint  => $matchpoint,
70                 protocol    => $protocol,
71             }
72         )->store;
73
74         Koha::Auth::Identity::Provider::Domain->new(
75             {
76                 identity_provider_id => $provider->identity_provider_id,
77             }
78         )->store;
79
80         push @messages, { type => 'message', code => 'success_on_insert' };
81     }
82     catch {
83         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
84             push @messages,
85               {
86                 type   => 'alert',
87                 code   => 'error_on_insert',
88                 reason => 'duplicate_id'
89               };
90         }
91     };
92
93     # list servers after adding
94     $op = 'list';
95 }
96 elsif ( $domain_ops && $op eq 'add' ) {
97
98     my $allow_opac              = $input->param('allow_opac');
99     my $allow_staff             = $input->param('allow_staff');
100     my $identity_provider_id    = $input->param('identity_provider_id');
101     my $auto_register           = $input->param('auto_register');
102     my $default_category_id     = $input->param('default_category_id') || undef;
103     my $default_library_id      = $input->param('default_library_id') || undef;
104     my $domain                  = $input->param('domain');
105     my $update_on_auth          = $input->param('update_on_auth');
106
107     try {
108
109         Koha::Auth::Identity::Provider::Domain->new(
110             {
111                 allow_opac          => $allow_opac,
112                 allow_staff         => $allow_staff,
113                 identity_provider_id    => $identity_provider_id,
114                 auto_register       => $auto_register,
115                 default_category_id => $default_category_id,
116                 default_library_id  => $default_library_id,
117                 domain              => $domain,
118                 update_on_auth      => $update_on_auth,
119             }
120         )->store;
121
122         push @messages, { type => 'message', code => 'success_on_insert' };
123     }
124     catch {
125         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
126             push @messages,
127               {
128                 type   => 'alert',
129                 code   => 'error_on_insert',
130                 reason => 'duplicate_id'
131               };
132         }
133     };
134
135     # list servers after adding
136     $op = 'list';
137 }
138 elsif ( !$domain_ops && $op eq 'edit_form' ) {
139
140     if ( $identity_provider ) {
141         $template->param(
142             identity_provider => $identity_provider
143         );
144     }
145     else {
146         push @messages,
147             {
148                 type   => 'alert',
149                 code   => 'error_on_edit',
150                 reason => 'invalid_id'
151             };
152     }
153 }
154 elsif ( $domain_ops && $op eq 'edit_form' ) {
155     my $identity_provider_domain_id = $input->param('identity_provider_domain_id');
156     my $identity_provider_domain;
157
158     $identity_provider_domain = Koha::Auth::Identity::Provider::Domains->find($identity_provider_domain_id)
159         unless !$identity_provider_domain_id;
160
161     if ( $identity_provider_domain ) {
162         $template->param(
163             identity_provider_domain => $identity_provider_domain
164         );
165     }
166     else {
167         push @messages,
168             {
169                 type   => 'alert',
170                 code   => 'error_on_edit',
171                 reason => 'invalid_id'
172             };
173     }
174 }
175 elsif ( !$domain_ops && $op eq 'edit_save' ) {
176
177     if ( $identity_provider ) {
178
179         my $code        = $input->param('code');
180         my $config      = $input->param('config');
181         my $description = $input->param('description');
182         my $icon_url    = $input->param('icon_url');
183         my $mapping     = $input->param('mapping');
184         my $matchpoint  = $input->param('matchpoint');
185         my $protocol    = $input->param('protocol');
186
187         try {
188
189             $identity_provider->set(
190                 {   code        => $code,
191                     config      => $config,
192                     description => $description,
193                     icon_url    => $icon_url,
194                     mapping     => $mapping,
195                     matchpoint  => $matchpoint,
196                     protocol    => $protocol,
197                 }
198             )->store;
199
200             push @messages,
201             {
202                 type => 'message',
203                 code => 'success_on_update'
204             };
205         }
206         catch {
207             push @messages,
208             {
209                 type   => 'alert',
210                 code   => 'error_on_update'
211             };
212         };
213
214         # list servers after adding
215         $op = 'list';
216     }
217     else {
218         push @messages,
219             {
220                 type   => 'alert',
221                 code   => 'error_on_update',
222                 reason => 'invalid_id'
223             };
224     }
225 }
226 elsif ( $domain_ops && $op eq 'edit_save' ) {
227
228     my $identity_provider_domain_id = $input->param('identity_provider_domain_id');
229     my $identity_provider_domain;
230
231     $identity_provider_domain = Koha::Auth::Identity::Provider::Domains->find($identity_provider_domain_id)
232         unless !$identity_provider_domain_id;
233
234     if ( $identity_provider_domain ) {
235
236         my $identity_provider_id    = $input->param('identity_provider_id');
237         my $domain              = $input->param('domain');
238         my $auto_register       = $input->param('auto_register');
239         my $update_on_auth      = $input->param('update_on_auth');
240         my $default_library_id  = $input->param('default_library_id') || undef;
241         my $default_category_id = $input->param('default_category_id') || undef;
242         my $allow_opac          = $input->param('allow_opac');
243         my $allow_staff         = $input->param('allow_staff');
244
245         try {
246
247             $identity_provider_domain->set(
248                 {
249                     identity_provider_id    => $identity_provider_id,
250                     domain              => $domain,
251                     auto_register       => $auto_register,
252                     update_on_auth      => $update_on_auth,
253                     default_library_id  => $default_library_id,
254                     default_category_id => $default_category_id,
255                     allow_opac          => $allow_opac,
256                     allow_staff         => $allow_staff,
257                 }
258             )->store;
259
260             push @messages,
261             {
262                 type => 'message',
263                 code => 'success_on_update'
264             };
265         }
266         catch {
267             push @messages,
268             {
269                 type   => 'alert',
270                 code   => 'error_on_update'
271             };
272         };
273
274         # list servers after adding
275         $op = 'list';
276     }
277     else {
278         push @messages,
279             {
280                 type   => 'alert',
281                 code   => 'error_on_update',
282                 reason => 'invalid_id'
283             };
284     }
285 }
286
287 if ( $domain_ops ) {
288     $template->param(
289         identity_provider_code => $identity_provider->code,
290         identity_provider_id   => $identity_provider_id,
291     );
292 }
293
294 $template->param(
295     op       => $op,
296     messages => \@messages,
297 );
298
299 output_html_with_http_headers $input, $cookie, $template->output;