From fe758533567e7f658ba2454443faa4328cd0d108 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 5 Aug 2025 15:34:43 -0300 Subject: [PATCH] Bug 40596: Add client-level user registration and update methods Implements client-level authentication with user registration and update capabilities, moving the logic from controller-level to client-level. Changes to base Koha::Auth::Client: * Added register_user() method for creating new patrons * Added update_user() method for updating existing patrons * Modified get_user() to automatically handle registration/updates based on domain configuration * Uses domain settings (auto_register, update_on_auth) to control behavior This provides the foundation for OAuth to be refactored to use the same client-level registration/update methods, creating a consistent authentication architecture across all protocols. Test plan: 1. Apply patch 2. Configure identity provider domains with auto_register/update_on_auth 3. Test user auto-creation when domain allows it 4. Test user updates when domain allows it 5. Verify CAS and Shibboleth authentication still works 6. Verify OAuth functionality is preserved --- Koha/Auth/Client.pm | 68 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/Koha/Auth/Client.pm b/Koha/Auth/Client.pm index ee8e0d23a4a..8b4f7685344 100644 --- a/Koha/Auth/Client.pm +++ b/Koha/Auth/Client.pm @@ -20,6 +20,7 @@ package Koha::Auth::Client; use Modern::Perl; use C4::Context; +use Try::Tiny qw( catch try ); use Koha::Exceptions::Auth; use Koha::Auth::Identity::Providers; @@ -85,7 +86,12 @@ sub get_user { $patron = $args->{'patron'}; $domain = $args->{'domain'}; - $patron->set($mapped_data)->store if $patron && $domain->update_on_auth; + # Handle user registration and updates at client level + if ( $patron && $domain->update_on_auth ) { + $patron = $self->update_user( $patron, $mapped_data ); + } elsif ( !$patron && $domain->auto_register ) { + $patron = $self->register_user( $mapped_data, $domain ); + } $mapped_data->{categorycode} = $domain->default_category_id; $mapped_data->{branchcode} = $domain->default_library_id; @@ -93,6 +99,66 @@ sub get_user { return ( $patron, $mapped_data, $domain ); } +=head3 register_user + + my $patron = $client->register_user( $user_data, $domain ); + +Creates a new patron from the provided user data and domain configuration. + +=cut + +sub register_user { + my ( $self, $user_data, $domain ) = @_; + + return unless $user_data && $domain; + + # Set required fields from domain configuration + $user_data->{categorycode} = $domain->default_category_id; + $user_data->{branchcode} = $domain->default_library_id; + + try { + my $patron = Koha::Patron->new($user_data)->store; + my $logger = Koha::Logger->get( { interface => 'api' } ); + $logger->info( "Auto-registered user: " . $patron->userid ); + return $patron; + } catch { + my $logger = Koha::Logger->get( { interface => 'api' } ); + $logger->error("Failed to auto-register user: $_"); + return; + }; +} + +=head3 update_user + + my $patron = $client->update_user( $patron, $user_data ); + +Updates an existing patron with the provided user data. + +=cut + +sub update_user { + my ( $self, $patron, $user_data ) = @_; + + return $patron unless $patron && $user_data; + + # Remove fields that shouldn't be updated + my $update_data = {%$user_data}; + delete $update_data->{borrowernumber}; + delete $update_data->{categorycode}; + delete $update_data->{branchcode}; + + try { + $patron->set($update_data)->store; + my $logger = Koha::Logger->get( { interface => 'api' } ); + $logger->debug( "Updated user data for: " . $patron->userid ); + return $patron; + } catch { + my $logger = Koha::Logger->get( { interface => 'api' } ); + $logger->error("Failed to update user: $_"); + return $patron; # Return original patron on failure + }; +} + =head3 get_valid_domain_config my $domain = Koha::Auth::Client->get_valid_domain_config( -- 2.50.1