From 5cb74b16ef9c545b2dcecc4d3cdd3ad277c187e6 Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Tue, 28 Nov 2017 16:01:09 +0000 Subject: [PATCH] Bug 18507: Shibboleth auto-provisioning - Sync This patch allows to update borrowers informations with Shibboleth attributes upon login. Test plan: 1. In $KOHA_CONF, check that //shibboleth/sync is set to 1 2. Find an existing user and change one of the values mapped with a Shibboleth attribute 3. Log in using Shibboleth 4. Check that the value has been updated with the Shibboleth attribute. --- C4/Auth_with_shibboleth.pm | 19 +++++++++++++++++++ t/Auth_with_shibboleth.t | 21 ++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/C4/Auth_with_shibboleth.pm b/C4/Auth_with_shibboleth.pm index f855ae1..03a94b3 100644 --- a/C4/Auth_with_shibboleth.pm +++ b/C4/Auth_with_shibboleth.pm @@ -27,6 +27,7 @@ use Koha::Patrons; use C4::Members::Messaging; use Carp; use CGI; +use List::MoreUtils qw(any); use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug); @@ -101,6 +102,9 @@ sub checkpw_shib { Koha::Database->new()->schema()->resultset('Borrower') ->find( { $config->{matchpoint} => $match } ); if ( defined($borrower) ) { + if ($config->{'sync'}) { + _sync($borrower->borrowernumber, $config, $match); + } return ( 1, $borrower->get_column('cardnumber'), $borrower->get_column('userid') ); } @@ -128,6 +132,21 @@ sub _autocreate { return ( 1, $patron->cardnumber, $patron->userid ); } +sub _sync { + my ($borrowernumber, $config, $match ) = @_; + my %borrower; + $borrower{'borrowernumber'} = $borrowernumber; + while ( my ( $key, $entry ) = each %{$config->{'mapping'}} ) { + if ( any { /(^psgi|^plack)/i } keys %ENV ) { + $borrower{$key} = ( $entry->{'is'} && $ENV{"HTTP_" . uc($entry->{'is'}) } ) || $entry->{'content'} || ''; + } else { + $borrower{$key} = ( $entry->{'is'} && $ENV{ $entry->{'is'} } ) || $entry->{'content'} || ''; + } + } + my $patron = Koha::Patrons->find( $borrowernumber ); + $patron->set(\%borrower)->store; +} + sub _get_uri { my $protocol = "https://"; diff --git a/t/Auth_with_shibboleth.t b/t/Auth_with_shibboleth.t index 5022b00..73c1ea3 100644 --- a/t/Auth_with_shibboleth.t +++ b/t/Auth_with_shibboleth.t @@ -39,6 +39,7 @@ use Test::DBIx::Class; # Mock Variables my $matchpoint = 'userid'; my $autocreate = 0; +my $sync = 0; my %mapping = ( 'userid' => { 'is' => 'uid' }, 'surname' => { 'is' => 'sn' }, @@ -156,7 +157,7 @@ subtest "get_login_shib tests" => sub { ## checkpw_shib subtest "checkpw_shib tests" => sub { - plan tests => 18; + plan tests => 21; my $shib_login; my ( $retval, $retcard, $retuserid ); @@ -214,6 +215,22 @@ subtest "checkpw_shib tests" => sub { 'Found $new_users surname'; $autocreate = 0; + # sync user + $sync = 1; + $ENV{'city'} = 'AnotherCity'; + warnings_are { + ( $retval, $retcard, $retuserid ) = checkpw_shib($shib_login); + } + [], "good user with sync"; + + ok my $sync_user = ResultSet('Borrower') + ->search( { 'userid' => 'test4321' }, { rows => 1 } ), "sync user found"; + + is_fields [qw/surname dateexpiry address city/], $sync_user->next, + [qw/pika 2017 Address AnotherCity/], + 'Found $sync_user synced city'; + $sync = 0; + # debug on $C4::Auth_with_shibboleth::debug = '1'; @@ -280,6 +297,7 @@ sub mockedConfig { my %shibboleth = ( 'autocreate' => $autocreate, + 'sync' => $sync, 'matchpoint' => $matchpoint, 'mapping' => \%mapping ); @@ -306,6 +324,7 @@ sub mockedSchema { sub reset_config { $matchpoint = 'userid'; $autocreate = 0; + $sync = 0; %mapping = ( 'userid' => { 'is' => 'uid' }, 'surname' => { 'is' => 'sn' }, -- 2.7.4