From 7865a1c978c7f7c4f73d65cd2f5b943a7305770e Mon Sep 17 00:00:00 2001 From: Raphael Straub Date: Thu, 28 Mar 2024 12:31:15 +0000 Subject: [PATCH] Bug 36503: Add a plugin hook after user authentication This plugin hook allows to change patron data or define the patron based on the authenticated user. To test: Run prove t/db_dependent/Koha/Auth/Client.t Sponsored-by: Karlsruhe Institute of Technology (KIT) Signed-off-by: Thomas Klausner --- Koha/Auth/Client.pm | 25 ++++++++++++++++++ t/db_dependent/Koha/Auth/Client.t | 43 ++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Koha/Auth/Client.pm b/Koha/Auth/Client.pm index 159dd40c8f..7e9d69e640 100644 --- a/Koha/Auth/Client.pm +++ b/Koha/Auth/Client.pm @@ -19,6 +19,8 @@ package Koha::Auth::Client; use Modern::Perl; +use C4::Context; + use Koha::Exceptions::Auth; use Koha::Auth::Identity::Providers; @@ -62,6 +64,29 @@ sub get_user { my ( $mapped_data, $patron ) = $self->_get_data_and_patron({ provider => $provider, data => $data, config => $config }); + # Call the plugin hook "auth_client_get_user" of all plugins in + # ascending priority. + if ( C4::Context->config('enable_plugins') ) { + my @plugins = Koha::Plugins->new()->GetPlugins( + { + method => 'auth_client_get_user', + } + ); + @plugins = sort { $a->retrieve_data('priority') <=> $b->retrieve_data('priority') } @plugins; + my $args = { + provider => $provider, + data => $data, + config => $config, + mapped_data => $mapped_data, + patron => $patron, + }; + foreach my $plugin (@plugins) { + $plugin->auth_client_get_user($args); + } + $mapped_data = $args->{'mapped_data'}; + $patron = $args->{'patron'}; + } + $mapped_data //= {}; my $domain = $self->has_valid_domain_config({ provider => $provider, email => $mapped_data->{email}, interface => $interface}); diff --git a/t/db_dependent/Koha/Auth/Client.t b/t/db_dependent/Koha/Auth/Client.t index 149aaa4818..7020cef9ec 100755 --- a/t/db_dependent/Koha/Auth/Client.t +++ b/t/db_dependent/Koha/Auth/Client.t @@ -22,6 +22,7 @@ use Modern::Perl; use Test::More tests => 4; use Test::MockModule; +use Test::MockObject; use Test::Exception; use JSON qw(encode_json); @@ -38,7 +39,7 @@ my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; subtest 'get_user() tests' => sub { - plan tests => 4; + plan tests => 5; $schema->storage->txn_begin; @@ -77,6 +78,46 @@ subtest 'get_user() tests' => sub { is( $mapped_data->{surname}, undef, 'No surname mapped' ); is( $domain->identity_provider_domain_id, $resolved_domain->identity_provider_domain_id, 'Is the same domain' ); + # Test the plugin hook "auth_client_get_user". + t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + my $get_plugins = sub { + my $methods = [ + { + auth_client_get_user => sub { + $_[1]->{mapped_data}->{firstname} = 'test name modified 1'; + return; + }, + retrieve_data => sub { return $_[1] eq 'priority' ? 2 : undef; } + }, + { + auth_client_get_user => sub { + $_[1]->{mapped_data}->{firstname} = 'test name modified 2'; + return; + }, + retrieve_data => sub { return $_[1] eq 'priority' ? 1 : undef; } + } + ]; + my @plugins; + foreach my $method ( @{$methods} ) { + my $plugin = Test::MockObject->new; + foreach my $name ( keys %{$method} ) { + $plugin->mock( $name, $method->{$name} ); + } + push @plugins, $plugin; + } + return @plugins; + }; + my $plugins_module = Test::MockModule->new('Koha::Plugins'); + $plugins_module->mock( 'GetPlugins', $get_plugins ); + $plugins_module->mock( 'get_enabled_plugins', $get_plugins ); + ( $resolved_patron, $mapped_data, $resolved_domain ) = + $client->get_user( { provider => $provider->code, data => $data, interface => 'opac' } ); + is( + $mapped_data->{firstname}, + 'test name modified 1', + 'Data modified correctly by plugins' + ); + $schema->storage->txn_rollback; }; -- 2.30.2