@@ -, +, @@ - Apply this patch, - set UseKohaPlugins to enabled, - define a pluginsdir in your koha-conf.xml, - put a plugin in this directory. - I.e, take the one provided in tests directory: - t/Koha/Plugin/TestAuth.pm, - login/pass is test/test - You should have: /your/plugins/dir/Koha/Plugin/TestAuth.pm, - test authentication with and without the plugin. --- C4/Auth.pm | 15 ++++++++++ Koha/Plugins.pm | 1 + t/Koha/Plugin/TestAuth.pm | 75 +++++++++++++++++++++++++++++++++++++++++++++++ t/db_dependent/Auth.t | 16 +++++++++- 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 t/Koha/Plugin/TestAuth.pm --- a/C4/Auth.pm +++ a/C4/Auth.pm @@ -36,6 +36,7 @@ use Koha::AuthUtils qw(get_script_name hash_password); use Koha::Library::Groups; use Koha::Libraries; use Koha::Patrons; +use Koha::Plugins; use POSIX qw/strftime/; use List::MoreUtils qw/ any /; use Encode qw( encode is_utf8); @@ -1765,6 +1766,20 @@ sub checkpw { my ( $dbh, $userid, $password, $query, $type, $no_set_userenv ) = @_; $type = 'opac' unless $type; + if ( C4::Context->preference('UseKohaPlugins') && C4::Context->config("enable_plugins") ) { + my @plugins = Koha::Plugins->new()->GetPlugins({ + method => 'checkpw', + }); + + @plugins = sort { $a->retrieve_data('priority') <=> $b->retrieve_data('priority') } @plugins; + foreach my $plugin ( @plugins ) { + my ( $retval, $retcard, $retuserid ) = $plugin->checkpw(@_); + if ( $retval == 1 ) { + return ( $retval, $retcard, $retuserid ); + } + } + } + my @return; my $patron = Koha::Patrons->find({ userid => $userid }); my $check_internal_as_fallback = 0; --- a/Koha/Plugins.pm +++ a/Koha/Plugins.pm @@ -82,6 +82,7 @@ sub GetPlugins { # Limit results by method or metadata next if $method && !$plugin->can($method); my $plugin_metadata = $plugin->get_metadata; + next if $plugin_metadata and %$req_metadata and any { !$plugin_metadata->{$_} || $plugin_metadata->{$_} ne $req_metadata->{$_} } keys %$req_metadata; --- a/t/Koha/Plugin/TestAuth.pm +++ a/t/Koha/Plugin/TestAuth.pm @@ -0,0 +1,75 @@ +package Koha::Plugin::TestAuth; + +use Modern::Perl; + +use base 'Koha::Plugins::Base'; + +our $VERSION = '1.0'; +our $metadata = { + name => 'Authentication test plugin', + author => 'Alex Arnaud ', + description => 'Plugin for testing authentication plugin API', + date_authored => '2018-02-28', + date_updated => '2018-02-28', + minimum_version => '17.05', + maximum_version => undef, + type => 'authentication', + version => $VERSION, +}; + +=head1 METHODS + +=head2 new + + Koha::Plugin::TestAuth->new(); + + Create new object + +=cut + +sub new { + my ($class, $args) = @_; + + $args->{metadata} = $metadata; + $args->{metadata}->{class} = $class; + + my $self = $class->SUPER::new($args); + + return $self; +} + +=head2 retrieve_data + +Just return priority. It overrides the one in Koha::Plugin::Base for the test. + +=cut + +sub retrieve_data { + my ( $self, $key ) = @_; + + if ( $key eq 'priority' ) { + return 1; + } + +} + +=head2 checkpw + + ($result, cardnumber, $userid) = + checkpw($self, $dbh, $userid, $password, $query, $type, $no_set_userenv); + + Authenticate a patron using login/password. + +=cut + +sub checkpw { + my ( $self, $dbh, $userid, $password, $query, $type, $no_set_userenv ) = @_; + + if ( $userid = 'test' && $password eq 'test' ) { + return (1, 'test', 'test'); + } + + return (0, '', ''); +} + +1; --- a/t/db_dependent/Auth.t +++ a/t/db_dependent/Auth.t @@ -10,7 +10,7 @@ use CGI qw ( -utf8 ); use Test::MockObject; use Test::MockModule; use List::MoreUtils qw/all any none/; -use Test::More tests => 21; +use Test::More tests => 27; use Test::Warn; use t::lib::Mocks; use t::lib::TestBuilder; @@ -20,8 +20,11 @@ use C4::Members; use Koha::AuthUtils qw/hash_password/; use Koha::Database; use Koha::Patrons; +use File::Basename; BEGIN { + push( @INC, dirname(__FILE__) . '/..' ); + use_ok('C4::Auth'); } @@ -32,6 +35,7 @@ my $dbh = C4::Context->dbh; # FIXME: SessionStorage defaults to mysql, but it seems to break transaction # handling t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); +t::lib::Mocks::mock_preference( 'UseKohaPlugins', 0 ); $schema->storage->txn_begin; @@ -88,6 +92,16 @@ my $hash2 = hash_password('password'); is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' ); ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' ); isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' ); + + t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 ); + my @result = checkpw( $dbh, $patron->{userid}, 'wrong_password', undef, undef, 1 ); + is( $result[0], 0, 'With TestAuth plugin, checkpw returns 0' ); + is( $result[1], undef, 'With TestAuth plugin, checkpw returns empty cardnumber' ); + is( $result[2], undef, 'With TestAuth plugin, checkpw returns empty userid' ); + @result = checkpw( $dbh, 'test', 'test', undef, undef, 1 ); + is( $result[0], 1, 'With TestAuth plugin, checkpw returns 1' ); + is( $result[1], 'test', 'With TestAuth plugin, checkpw returns test cardnumber' ); + is( $result[2], 'test', 'With TestAuth plugin, checkpw returns test userid' ); } # get_template_and_user tests --