View | Details | Raw Unified | Return to bug 15585
Collapse All | Expand All

(-)a/C4/Passwordrecovery.pm (-6 / +9 lines)
Lines 1-4 Link Here
1
package C4::Passwordrecovery;
1
package Koha::Patron::Password::Recovery;
2
2
3
# Copyright 2014 Solutions InLibro inc.
3
# Copyright 2014 Solutions InLibro inc.
4
#
4
#
Lines 38-48 BEGIN { Link Here
38
38
39
=head1 NAME
39
=head1 NAME
40
40
41
C4::Passwordrecovery - Koha password recovery module
41
Koha::Patron::Password::Recovery - Koha password recovery module
42
42
43
=head1 SYNOPSIS
43
=head1 SYNOPSIS
44
44
45
use C4::Passwordrecovery;
45
use Koha::Patron::Password::Recovery;
46
46
47
=head1 FUNCTIONS
47
=head1 FUNCTIONS
48
48
Lines 110-117 sub SendPasswordRecoveryEmail { Link Here
110
    my $schema = Koha::Database->new->schema;
110
    my $schema = Koha::Database->new->schema;
111
111
112
    # generate UUID
112
    # generate UUID
113
    my @chars = ( "A" .. "Z", "a" .. "z", "0" .. "9" );
113
    my $uuid_str;
114
    my $uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16));
114
    do {
115
        $uuid_str = '$2a$08$'.en_base64(Koha::AuthUtils::generate_salt('weak', 16));
116
    } while ( substr ( $uuid_str, -1, 1 ) eq '.' );
115
117
116
    # insert into database
118
    # insert into database
117
    my $expirydate =
119
    my $expirydate =
Lines 134-140 sub SendPasswordRecoveryEmail { Link Here
134
    }
136
    }
135
137
136
    # create link
138
    # create link
137
    my $uuidLink = C4::Context->preference('OPACBaseURL')
139
    my $opacbase = C4::Context->preference('OPACBaseURL') || '';
140
    my $uuidLink = $opacbase
138
      . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
141
      . "/cgi-bin/koha/opac-password-recovery.pl?uniqueKey=$uuid_str";
139
142
140
    # prepare the email
143
    # prepare the email
(-)a/opac/opac-password-recovery.pl (-6 / +9 lines)
Lines 8-14 use C4::Koha; Link Here
8
use C4::Members qw(changepassword);
8
use C4::Members qw(changepassword);
9
use C4::Output;
9
use C4::Output;
10
use C4::Context;
10
use C4::Context;
11
use C4::Passwordrecovery
11
use Koha::Patron::Password::Recovery
12
  qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
12
  qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
13
use Koha::AuthUtils qw(hash_password);
13
use Koha::AuthUtils qw(hash_password);
14
use Koha::Patrons;
14
use Koha::Patrons;
Lines 71-76 if ( $query->param('sendEmail') || $query->param('resendEmail') ) { Link Here
71
        $username ||= $borrower->userid;
71
        $username ||= $borrower->userid;
72
        my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
72
        my @emails = ( $borrower->email, $borrower->emailpro, $borrower->B_email );
73
73
74
        my $firstNonEmptyEmail = '';
75
        foreach my $address ( @emails ) {
76
            $firstNonEmptyEmail = $address if length $address;
77
            last if $firstNonEmptyEmail;
78
        }
79
74
        # Is the given email one of the borrower's ?
80
        # Is the given email one of the borrower's ?
75
        if ( $email && !( grep { $_ eq $email } @emails ) ) {
81
        if ( $email && !( grep { $_ eq $email } @emails ) ) {
76
            $hasError    = 1;
82
            $hasError    = 1;
Lines 78-89 if ( $query->param('sendEmail') || $query->param('resendEmail') ) { Link Here
78
        }
84
        }
79
85
80
# If we dont have an email yet. Get one of the borrower's email or raise an error.
86
# If we dont have an email yet. Get one of the borrower's email or raise an error.
81
# FIXME: That ugly shift-grep contraption.
87
        elsif ( !$email && !( $email = $firstNonEmptyEmail ) ) {
82
# $email = shift [ grep { length() } @emails ]
83
# It's supposed to get a non-empty string from the @emails array. There's surely a simpler way
84
        elsif ( !$email && !( $email = shift [ grep { length() } @emails ] ) ) {
85
            $hasError           = 1;
88
            $hasError           = 1;
86
            $errNoBorrowerFound = 1;
89
            $errNoBorrowerEmail = 1;
87
        }
90
        }
88
91
89
# Check if a password reset already issued for this borrower AND we are not asking for a new email
92
# Check if a password reset already issued for this borrower AND we are not asking for a new email
(-)a/t/db_dependent/Passwordrecovery.t (-26 / +25 lines)
Lines 24-30 use Koha::Patrons; Link Here
24
24
25
use Test::More tests => 16;
25
use Test::More tests => 16;
26
26
27
use_ok('C4::Passwordrecovery');
27
use_ok('Koha::Patron::Password::Recovery');
28
28
29
my $schema = Koha::Database->new()->schema();
29
my $schema = Koha::Database->new()->schema();
30
$schema->storage->txn_begin();
30
$schema->storage->txn_begin();
Lines 90-123 $schema->resultset('BorrowerPasswordRecovery')->create( Link Here
90
    }
90
    }
91
);
91
);
92
92
93
can_ok( "C4::Passwordrecovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
93
can_ok( "Koha::Patron::Password::Recovery", qw(ValidateBorrowernumber GetValidLinkInfo SendPasswordRecoveryEmail CompletePasswordRecovery) );
94
94
95
################################################
95
############################################################
96
# C4::Passwordrecovery::ValidateBorrowernumber #
96
# Koha::Patron::Password::Recovery::ValidateBorrowernumber #
97
################################################
97
############################################################
98
98
99
ok(   C4::Passwordrecovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
99
ok(   Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber1), "[ValidateBorrowernumber] Borrower has a password recovery entry" );
100
ok( ! C4::Passwordrecovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
100
ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber($borrowernumber2), "[ValidateBorrowernumber] Borrower's number is not found; password recovery entry is expired" );
101
ok( ! C4::Passwordrecovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
101
ok( ! Koha::Patron::Password::Recovery::ValidateBorrowernumber(9999), "[ValidateBorrowernumber] Borrower has no password recovery entry" );
102
102
103
##########################################
103
######################################################
104
# C4::Passwordrecovery::GetValidLinkInfo #
104
# Koha::Patron::Password::Recovery::GetValidLinkInfo #
105
##########################################
105
######################################################
106
106
107
my ($bnum1, $uname1) = C4::Passwordrecovery::GetValidLinkInfo($uuid1);
107
my ($bnum1, $uname1) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid1);
108
my ($bnum2, $uname2) = C4::Passwordrecovery::GetValidLinkInfo($uuid2);
108
my ($bnum2, $uname2) = Koha::Patron::Password::Recovery::GetValidLinkInfo($uuid2);
109
my ($bnum3, $uname3) = C4::Passwordrecovery::GetValidLinkInfo("THISISANINVALIDUUID");
109
my ($bnum3, $uname3) = Koha::Patron::Password::Recovery::GetValidLinkInfo("THISISANINVALIDUUID");
110
110
111
is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
111
is( $bnum1, $borrowernumber1, "[GetValidLinkInfo] Borrower has a valid link" );
112
is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
112
is( $uname1, $userid1, "[GetValidLinkInfo] Borrower's username is fetched when a valid link is found" );
113
ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
113
ok( ! defined($bnum2), "[GetValidLinkInfo] Borrower's link is no longer valid; entry is expired" );
114
ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
114
ok( ! defined($bnum3), "[GetValidLinkInfo] Invalid UUID returns no borrowernumber" );
115
115
116
##################################################
116
##############################################################
117
# C4::Passwordrecovery::CompletePasswordRecovery #
117
# Koha::Patron::Password::Recovery::CompletePasswordRecovery #
118
##################################################
118
##############################################################
119
119
120
ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid1) == 2, "[CompletePasswordRecovery] Completing a password recovery deletes the entry and expired entries" );
120
ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid1) == 2, "[CompletePasswordRecovery] Completing a password recovery deletes the entry and expired entries" );
121
121
122
$schema->resultset('BorrowerPasswordRecovery')->create(
122
$schema->resultset('BorrowerPasswordRecovery')->create(
123
    {
123
    {
Lines 127-148 $schema->resultset('BorrowerPasswordRecovery')->create( Link Here
127
    }
127
    }
128
);
128
);
129
129
130
ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
130
ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 1, "[CompletePasswordRecovery] An expired or invalid UUID purges expired entries" );
131
ok( C4::Passwordrecovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
131
ok( Koha::Patron::Password::Recovery::CompletePasswordRecovery($uuid2) == 0, "[CompletePasswordRecovery] Returns 0 on a clean table" );
132
132
133
###################################################
133
###############################################################
134
# C4::Passwordrecovery::SendPasswordRecoveryEmail #
134
# Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail #
135
###################################################
135
###############################################################
136
136
137
my $borrower = shift [ Koha::Patrons->search( { userid => $userid1 } ) ];
137
my $borrower = shift [ Koha::Patrons->search( { userid => $userid1 } ) ];
138
ok( C4::Passwordrecovery::SendPasswordRecoveryEmail($borrower, $email1, 0) == 1, "[SendPasswordRecoveryEmail] Returns 1 on success" );
138
ok( Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 0) == 1, "[SendPasswordRecoveryEmail] Returns 1 on success" );
139
my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
139
my $letters = C4::Letters::GetQueuedMessages( { borrowernumber => $borrowernumber1, limit => 99 } );
140
ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
140
ok( scalar @$letters == 1, "[SendPasswordRecoveryEmail] There is a letter in the queue for our borrower");
141
141
142
my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
142
my $bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
143
my $tempuuid1 = $bpr->next->uuid;
143
my $tempuuid1 = $bpr->next->uuid;
144
144
145
C4::Passwordrecovery::SendPasswordRecoveryEmail($borrower, $email1, 1);
145
Koha::Patron::Password::Recovery::SendPasswordRecoveryEmail($borrower, $email1, 1);
146
146
147
$bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
147
$bpr = $schema->resultset('BorrowerPasswordRecovery')->search( { borrowernumber => $borrowernumber1 } );
148
my $tempuuid2 = $bpr->next->uuid;
148
my $tempuuid2 = $bpr->next->uuid;
149
- 

Return to bug 15585