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