From fa4f558e1231af0c9fd73e6e8bf59d0c51b92262 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 5 Mar 2024 17:03:06 +0000 Subject: [PATCH] Bug 36197: Update how form params are validated Validating an unauth request form requires checking for unauth info fields. Validating a regular authenticated request requires checking for borrower information. Signed-off-by: David Nind --- Koha/ILL/Backend/Standard.pm | 105 ++++++++++++++++------ Koha/ILL/Backend/opac-includes/create.inc | 4 + 2 files changed, 82 insertions(+), 27 deletions(-) diff --git a/Koha/ILL/Backend/Standard.pm b/Koha/ILL/Backend/Standard.pm index f55f207bca2..22f489a2c1a 100644 --- a/Koha/ILL/Backend/Standard.pm +++ b/Koha/ILL/Backend/Standard.pm @@ -276,9 +276,6 @@ sub create { } # Received completed details of form. Validate and create request. - ## Validate - my ( $brw_count, $brw ) = - _validate_borrower( $other->{'cardnumber'} ); my $result = { cwd => dirname(__FILE__), status => "", @@ -290,31 +287,37 @@ sub create { core => $core_fields }; my $failed = 0; - if ( !$other->{'type'} ) { - $result->{status} = "missing_type"; - $result->{value} = $params; - $failed = 1; - } elsif ( !$other->{'branchcode'} ) { - $result->{status} = "missing_branch"; - $result->{value} = $params; - $failed = 1; - } elsif ( !Koha::Libraries->find( $other->{'branchcode'} ) ) { - $result->{status} = "invalid_branch"; - $result->{value} = $params; - $failed = 1; - } elsif ( $brw_count == 0 ) { - $result->{status} = "invalid_borrower"; - $result->{value} = $params; - $failed = 1; - } elsif ( $brw_count > 1 ) { - - # We must select a specific borrower out of our options. - $params->{brw} = $brw; - $result->{value} = $params; - $result->{stage} = "borrowers"; - $result->{error} = 0; - $failed = 1; + + my $unauthenticated_request = + C4::Context->preference("ILLOpacUnauthenticatedRequest") && !$other->{'cardnumber'}; + if ($unauthenticated_request) { + ( $failed, $result ) = _validate_form_params( $other, $result, $params ); + if ( !_unauth_request_data_check($other) ) { + $result->{status} = "missing_unauth_data"; + $result->{value} = $params; + $failed = 1; + } + } else { + ( $failed, $result ) = _validate_form_params( $other, $result, $params ); + + my ( $brw_count, $brw ) = + _validate_borrower( $other->{'cardnumber'} ); + + if ( $brw_count == 0 ) { + $result->{status} = "invalid_borrower"; + $result->{value} = $params; + $failed = 1; + } elsif ( $brw_count > 1 ) { + + # We must select a specific borrower out of our options. + $params->{brw} = $brw; + $result->{value} = $params; + $result->{stage} = "borrowers"; + $result->{error} = 0; + $failed = 1; + } } + return $result if $failed; $self->add_request( { request => $params->{request}, other => $other } ); @@ -1221,6 +1224,54 @@ sub _set_suppression { return 1; } +=head3 _unauth_request_data_check + + _unauth_request_data_check($other); + +Checks if unauthenticated request data is present + +=cut + +sub _unauth_request_data_check { + my ($other) = @_; + + return 1 unless C4::Context->preference("ILLOpacUnauthenticatedRequest"); + + return + $other->{unauthenticated_first_name} + && $other->{unauthenticated_last_name} + && $other->{unauthenticated_email}; +} + +=head3 _validate_form_params + + _validate_form_params( $other, $result, $params ); + +Validate form parameters and return the validation result + +=cut + +sub _validate_form_params { + my ( $other, $result, $params ) = @_; + + my $failed = 0; + if ( !$other->{'type'} ) { + $result->{status} = "missing_type"; + $result->{value} = $params; + $failed = 1; + } elsif ( !$other->{'branchcode'} ) { + $result->{status} = "missing_branch"; + $result->{value} = $params; + $failed = 1; + } elsif ( !Koha::Libraries->find( $other->{'branchcode'} ) ) { + $result->{status} = "invalid_branch"; + $result->{value} = $params; + $failed = 1; + } + + return ( $failed, $result ); +} + =head1 AUTHORS Alex Sassmannshausen diff --git a/Koha/ILL/Backend/opac-includes/create.inc b/Koha/ILL/Backend/opac-includes/create.inc index f80c151e751..fa2bad04236 100644 --- a/Koha/ILL/Backend/opac-includes/create.inc +++ b/Koha/ILL/Backend/opac-includes/create.inc @@ -11,6 +11,10 @@
Please note: Type is a mandatory field.
+[% ELSIF whole.status == 'missing_unauth_data' %] +
+ Please note: Patron data (first name, last name and e-mail) are mandatory fields. +
[% ELSIF whole.status == 'missing_branch' %]
Please note: Library is a mandatory field. -- 2.39.5