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 <domm@plix.at>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Raphael Straub 2024-03-28 12:31:15 +00:00 committed by Katrin Fischer
parent b099f62bfc
commit 911342ffec
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
2 changed files with 67 additions and 1 deletions

View File

@ -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});

View File

@ -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;
};