3 # Copyright 2022 Theke Solutions
5 # This file is part of Koha.
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.
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.
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>.
23 use Scalar::Util qw( blessed );
24 use Try::Tiny qw( catch try );
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
30 use Koha::Auth::Identity::Providers;
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;
38 $identity_provider = Koha::Auth::Identity::Providers->find($identity_provider_id)
39 unless !$identity_provider_id;
41 my $template_name = $domain_ops ? 'admin/identity_provider_domains.tt' : 'admin/identity_providers.tt';
43 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
44 { template_name => $template_name,
47 flagsrequired => { parameters => 'manage_identity_providers' },
53 if ( !$domain_ops && $op eq 'add' ) {
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');
73 Koha::Database->new->schema->txn_do(
75 my $provider = Koha::Auth::Identity::Provider->new(
78 description => $description,
79 icon_url => $icon_url,
81 matchpoint => $matchpoint,
82 protocol => $protocol,
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,
94 update_on_auth => $update_on_auth,
98 push @messages, { type => 'message', code => 'success_on_insert' };
103 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
107 code => 'error_on_insert',
108 reason => 'duplicate_id'
113 # list servers after adding
116 elsif ( $domain_ops && $op eq 'add' ) {
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');
129 Koha::Auth::Identity::Provider::Domain->new(
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,
138 update_on_auth => $update_on_auth,
142 push @messages, { type => 'message', code => 'success_on_insert' };
145 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
149 code => 'error_on_insert',
150 reason => 'duplicate_id'
155 # list servers after adding
158 elsif ( !$domain_ops && $op eq 'edit_form' ) {
160 if ( $identity_provider ) {
162 identity_provider => $identity_provider
169 code => 'error_on_edit',
170 reason => 'invalid_id'
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;
178 $identity_provider_domain = Koha::Auth::Identity::Provider::Domains->find($identity_provider_domain_id)
179 unless !$identity_provider_domain_id;
181 if ( $identity_provider_domain ) {
183 identity_provider_domain => $identity_provider_domain
190 code => 'error_on_edit',
191 reason => 'invalid_id'
195 elsif ( !$domain_ops && $op eq 'edit_save' ) {
197 if ( $identity_provider ) {
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');
209 $identity_provider->set(
212 description => $description,
213 icon_url => $icon_url,
215 matchpoint => $matchpoint,
216 protocol => $protocol,
223 code => 'success_on_update'
230 code => 'error_on_update'
234 # list servers after adding
241 code => 'error_on_update',
242 reason => 'invalid_id'
246 elsif ( $domain_ops && $op eq 'edit_save' ) {
248 my $identity_provider_domain_id = $input->param('identity_provider_domain_id');
249 my $identity_provider_domain;
251 $identity_provider_domain = Koha::Auth::Identity::Provider::Domains->find($identity_provider_domain_id)
252 unless !$identity_provider_domain_id;
254 if ( $identity_provider_domain ) {
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');
267 $identity_provider_domain->set(
269 identity_provider_id => $identity_provider_id,
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,
283 code => 'success_on_update'
290 code => 'error_on_update'
294 # list servers after adding
301 code => 'error_on_update',
302 reason => 'invalid_id'
309 identity_provider_code => $identity_provider->code,
310 identity_provider_id => $identity_provider_id,
316 messages => \@messages,
319 output_html_with_http_headers $input, $cookie, $template->output;