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

(-)a/Koha/Patron.pm (-30 / +1 lines)
Lines 276-282 sub store { Link Here
276
                    $self->userid($stored_userid);
276
                    $self->userid($stored_userid);
277
                }
277
                }
278
278
279
                # Password must be updated using $self->update_password
279
                # Password must be updated using $self->set_password
280
                $self->password($self_from_storage->password);
280
                $self->password($self_from_storage->password);
281
281
282
                if ( C4::Context->preference('FeeOnChangePatronCategory')
282
                if ( C4::Context->preference('FeeOnChangePatronCategory')
Lines 639-673 sub is_going_to_expire { Link Here
639
    return 0;
639
    return 0;
640
}
640
}
641
641
642
=head3 update_password
643
644
my $updated = $patron->update_password( $userid, $password );
645
646
Update the userid and the password of a patron.
647
If the userid already exists, returns and let DBIx::Class warns
648
This will add an entry to action_logs if BorrowersLog is set.
649
650
=cut
651
652
sub update_password {
653
    my ( $self, $userid, $password ) = @_;
654
    eval { $self->userid($userid)->store; };
655
    return if $@; # Make sure the userid is not already in used by another patron
656
657
    return 0 if $password eq '****' or $password eq '';
658
659
    my $digest = Koha::AuthUtils::hash_password($password);
660
    $self->update(
661
        {
662
            password       => $digest,
663
            login_attempts => 0,
664
        }
665
    );
666
667
    logaction( "MEMBERS", "CHANGE PASS", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog");
668
    return $digest;
669
}
670
671
=head3 set_password
642
=head3 set_password
672
643
673
    $patron->set_password({ password => $plain_text_password [, skip_validation => 1 ] });
644
    $patron->set_password({ password => $plain_text_password [, skip_validation => 1 ] });
(-)a/t/db_dependent/Koha/Patrons.t (-27 / +1 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 34;
22
use Test::More tests => 33;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 208-238 subtest 'has_overdues' => sub { Link Here
208
    $issue->delete();
208
    $issue->delete();
209
};
209
};
210
210
211
subtest 'update_password' => sub {
212
    plan tests => 7;
213
214
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
215
    my $original_userid   = $new_patron_1->userid;
216
    my $original_password = $new_patron_1->password;
217
    warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
218
    qr{Duplicate entry},
219
      'Koha::Patron->update_password should warn if the userid is already used by another patron';
220
    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   $original_userid,   'Koha::Patron->update_password should not have updated the userid' );
221
    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
222
223
    my $digest = $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
224
    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
225
    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $digest,             'Koha::Patron->update_password should have updated the password' );
226
227
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
228
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
229
230
    t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
231
    $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
232
    $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
233
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
234
};
235
236
subtest 'is_expired' => sub {
211
subtest 'is_expired' => sub {
237
    plan tests => 4;
212
    plan tests => 4;
238
    my $patron = $builder->build({ source => 'Borrower' });
213
    my $patron = $builder->build({ source => 'Borrower' });
239
- 

Return to bug 21992