From 8926c671e780755706484460e424050d7fa9389d Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Wed, 31 Dec 2014 10:20:59 +1300 Subject: [PATCH] Bug 13502: Code introcduced in 1861 wrongly assumes a null userid is unique To test 1/ Create a borrower with '' as their userid, you may have to edit a row in the db to do this 2/ Run perl t/db_dependent/Circulation/CheckIfIssuedToPatron.t 3/ Notice some tests fail and you see DBD::mysql::st execute failed: Duplicate entry '' for key 'userid' at /home/chrisc/git/catalyst-koha/C4/SQLHelper.pm line 184. 4/ Apply the patch 5/ Run the tests again, notice they now pass Signed-off-by: Jonathan Druart Signed-off-by: Tomas Cohen Arazi Signed-off-by: Katrin Fischer Signed-off-by: Mason James --- C4/Members.pm | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index f6cf4a75bd..c15dbb4e4b 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -863,19 +863,21 @@ sub AddMember { =cut sub Check_Userid { - my ($uid,$member) = @_; - my $dbh = C4::Context->dbh; - my $sth = - $dbh->prepare( - "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?"); - $sth->execute( $uid, $member ); - if ( (( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref )) or - (( $uid ne '' ) && ( $uid eq C4::Context->config('user') )) ) { - return 0; - } - else { - return 1; - } + my ( $uid, $borrowernumber ) = @_; + + return 0 unless ($uid); # userid is a unique column, we should assume NULL is not unique + + return 0 if ( $uid eq C4::Context->config('user') ); + + my $rs = Koha::Database->new()->schema()->resultset('Borrower'); + + my $params; + $params->{userid} = $uid; + $params->{borrowernumber} = { '!=' => $borrowernumber } if ($borrowernumber); + + my $count = $rs->count( $params ); + + return $count ? 0 : 1; } =head2 Generate_Userid -- 2.39.5