Bug 15585 - Move C4::Passwordrecovery to the new namespace Koha::Patron::Password::Reset

As promised, here is the long-awaited sequel to #8753.

What has changed :

    - The Koha::Patron::Password::Reset is now used in place of C4::Passwordrecovery
    - That ugly shift-grep contraption is no more (goodbye old friend)
    - The generated unique key won't end in a dot anymore

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Brendan Gallagher brendan@bywatersolutions.com
This commit is contained in:
charles 2016-01-27 15:17:32 -05:00 committed by Brendan Gallagher
parent 515e2d7c9d
commit db0ecc3cc5
3 changed files with 43 additions and 37 deletions

View File

@ -1,4 +1,4 @@
package C4::Passwordrecovery;
package Koha::Patron::Password::Recovery;
# Copyright 2014 Solutions InLibro inc.
#
@ -38,11 +38,11 @@ BEGIN {
=head1 NAME
C4::Passwordrecovery - Koha password recovery module
Koha::Patron::Password::Recovery - Koha password recovery module
=head1 SYNOPSIS
use C4::Passwordrecovery;
use Koha::Patron::Password::Recovery;
=head1 FUNCTIONS
@ -110,8 +110,10 @@ sub SendPasswordRecoveryEmail {
my $schema = Koha::Database->new->schema;
# generate UUID
my @chars = ( "A" .. "Z", "a" .. "z", "0" .. "9" );
my $uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16));
my $uuid_str;
do {
$uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16));
} while ( substr ( $uuid_str, -1, 1 ) eq '.' );
# insert into database
my $expirydate =
@ -134,7 +136,8 @@ sub SendPasswordRecoveryEmail {
}
# create link
my $uuidLink = C4::Context->preference('OPACBaseURL')
my $opacbase = C4::Context->preference('OPACBaseURL') || '';
my $uuidLink = $opacbase
. "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
# prepare the email

View File

@ -8,7 +8,7 @@ use C4::Koha;
use C4::Members qw(changepassword);
use C4::Output;
use C4::Context;
use C4::Passwordrecovery
use Koha::Patron::Password::Recovery
qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
use Koha::AuthUtils qw(hash_password);
use Koha::Patrons;
@ -71,6 +71,12 @@ if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
$username ||= $borrower->userid;
my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
my $firstNonEmptyEmail = '';
foreach my $address ( @emails ) {
$firstNonEmptyEmail = $address if length $address;
last if $firstNonEmptyEmail;
}
# Is the given email one of the borrower's ?
if ( $email && !( grep { $_ eq $email } @emails ) ) {
$hasError = 1;
@ -78,12 +84,9 @@ if ( $query->param('sendEmail') || $query->param('resendEmail') ) {
}
# If we dont have an email yet. Get one of the borrower's email or raise an error.
# FIXME: That ugly shift-grep contraption.
# $email = shift [ grep { length() } @emails ]
# It's supposed to get a non-empty string from the @emails array. There's surely a simpler way
elsif ( !$email && !( $email = shift [ grep { length() } @emails ] ) ) {
elsif ( !$email && !( $email = $firstNonEmptyEmail ) ) {
$hasError = 1;
$errNoBorrowerFound = 1;
$errNoBorrowerEmail = 1;
}
# Check if a password reset already issued for this borrower AND we are not asking for a new email

View File

@ -24,7 +24,7 @@ use Koha::Patrons;
use Test::More tests => 16;
use_ok('C4::Passwordrecovery');
use_ok('Koha::Patron::Password::Recovery');
my $schema = Koha::Database->new()->schema();
$schema->storage->txn_begin();
@ -90,34 +90,34 @@ $schema->resultset('BorrowerPasswordRecovery')->create(
}
);
can_ok( "C4::Passwordrecovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
################################################
# C4::Passwordrecovery::ValidateBorrowernumber #
################################################
############################################################
# Koha::Patron::Password::Recovery::ValidateBorrowernumber #
############################################################
ok( C4::Passwordrecovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
ok( ! C4::Passwordrecovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
ok( ! C4::Passwordrecovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
ok( Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
##########################################
# C4::Passwordrecovery::GetValidLinkInfo #
##########################################
######################################################
# Koha::Patron::Password::Recovery::GetValidLinkInfo #
######################################################
my ($bnum1, $uname1) = C4::Passwordrecovery::GetValidLinkInfo($uuid1);
my ($bnum2, $uname2) = C4::Passwordrecovery::GetValidLinkInfo($uuid2);
my ($bnum3, $uname3) = C4::Passwordrecovery::GetValidLinkInfo("THISISANINVALIDUUID");
my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
##################################################
# C4::Passwordrecovery::CompletePasswordRecovery #
##################################################
##############################################################
# Koha::Patron::Password::Recovery::CompletePasswordRecovery #
##############################################################
ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid1) == 2, "[CompletePasswordRecovery] Completing a password recovery deletes the entry and expired entries" );
ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1) == 2, "[CompletePasswordRecovery] Completing a password recovery deletes the entry and expired entries" );
$schema->resultset('BorrowerPasswordRecovery')->create(
{
@ -127,22 +127,22 @@ $schema->resultset('BorrowerPasswordRecovery')->create(
}
);
ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
###################################################
# C4::Passwordrecovery::SendPasswordRecoveryEmail #
###################################################
###############################################################
# Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
###############################################################
my $borrower = shift [ Koha::Patrons->search( { userid => $userid1 } ) ];
ok( C4::Passwordrecovery::SendPasswordRecoveryEmail($borrower, $email1, 0) == 1, "[SendPasswordRecoveryEmail] Returns 1 on success" );
ok( Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0) == 1, "[SendPasswordRecoveryEmail] Returns 1 on success" );
my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
my $tempuuid1 = $bpr->next->uuid;
C4::Passwordrecovery::SendPasswordRecoveryEmail($borrower, $email1, 1);
Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1);
$bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
my $tempuuid2 = $bpr->next->uuid;