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

(-)a/C4/Auth_with_ldap.pm (-2 / +3 lines)
Lines 23-33 use Carp; Link Here
23
23
24
use C4::Debug;
24
use C4::Debug;
25
use C4::Context;
25
use C4::Context;
26
use C4::Members qw(AddMember changepassword);
26
use C4::Members qw(AddMember);
27
use C4::Members::Attributes;
27
use C4::Members::Attributes;
28
use C4::Members::AttributeTypes;
28
use C4::Members::AttributeTypes;
29
use C4::Members::Messaging;
29
use C4::Members::Messaging;
30
use C4::Auth qw(checkpw_internal);
30
use C4::Auth qw(checkpw_internal);
31
use Koha::Patrons;
31
use Koha::AuthUtils qw(hash_password);
32
use Koha::AuthUtils qw(hash_password);
32
use List::MoreUtils qw( any );
33
use List::MoreUtils qw( any );
33
use Net::LDAP;
34
use Net::LDAP;
Lines 334-340 sub _do_changepassword { Link Here
334
    my $digest = hash_password($password);
335
    my $digest = hash_password($password);
335
336
336
    $debug and print STDERR "changing local password for borrowernumber=$borrowerid to '$digest'\n";
337
    $debug and print STDERR "changing local password for borrowernumber=$borrowerid to '$digest'\n";
337
    changepassword($userid, $borrowerid, $digest);
338
    Koha::Patrons->find($borrowerid)->update_password( $userid, $digest );
338
339
339
    my ($ok, $cardnum) = checkpw_internal(C4::Context->dbh, $userid, $password);
340
    my ($ok, $cardnum) = checkpw_internal(C4::Context->dbh, $userid, $password);
340
    return $cardnum if $ok;
341
    return $cardnum if $ok;
(-)a/C4/Members.pm (-29 lines)
Lines 813-847 sub Generate_Userid { Link Here
813
   return $newuid;
813
   return $newuid;
814
}
814
}
815
815
816
sub changepassword {
817
    my ( $uid, $member, $digest ) = @_;
818
    my $dbh = C4::Context->dbh;
819
820
#Make sure the userid chosen is unique and not theirs if non-empty. If it is not,
821
#Then we need to tell the user and have them create a new one.
822
    my $resultcode;
823
    my $sth =
824
      $dbh->prepare(
825
        "SELECT * FROM borrowers WHERE userid=? AND borrowernumber != ?");
826
    $sth->execute( $uid, $member );
827
    if ( ( $uid ne '' ) && ( my $row = $sth->fetchrow_hashref ) ) {
828
        $resultcode=0;
829
    }
830
    else {
831
        #Everything is good so we can update the information.
832
        $sth =
833
          $dbh->prepare(
834
            "update borrowers set userid=?, password=? where borrowernumber=?");
835
        $sth->execute( $uid, $digest, $member );
836
        $resultcode=1;
837
    }
838
    
839
    logaction("MEMBERS", "CHANGE PASS", $member, "") if C4::Context->preference("BorrowersLog");
840
    return $resultcode;    
841
}
842
843
844
845
=head2 fixup_cardnumber
816
=head2 fixup_cardnumber
846
817
847
Warning: The caller is responsible for locking the members table in write
818
Warning: The caller is responsible for locking the members table in write
(-)a/Koha/Patron.pm (+21 lines)
Lines 21-26 use Modern::Perl; Link Here
21
21
22
use Carp;
22
use Carp;
23
23
24
use C4::Context;
25
use C4::Log;
24
use Koha::Database;
26
use Koha::Database;
25
use Koha::Patrons;
27
use Koha::Patrons;
26
use Koha::Patron::Images;
28
use Koha::Patron::Images;
Lines 95-100 sub siblings { Link Here
95
    );
97
    );
96
}
98
}
97
99
100
=head2 update_password
101
102
my $updated = $patron->update_password( $userid, $password );
103
104
Update the userid and the password of a patron.
105
If the userid already exists, returns and let DBIx::Class warns
106
This will add an entry to action_logs if BorrowersLog is set.
107
108
=cut
109
110
sub update_password {
111
    my ( $self, $userid, $password ) = @_;
112
    eval { $self->userid($userid)->store; };
113
    return if $@; # Make sure the userid is not already in used by another patron
114
    $self->password($password)->store;
115
    logaction( "MEMBERS", "CHANGE PASS", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog");
116
    return 1;
117
}
118
98
=head3 type
119
=head3 type
99
120
100
=cut
121
=cut
(-)a/members/member-password.pl (-1 / +1 lines)
Lines 66-72 if ( $newpassword && !scalar(@errors) ) { Link Here
66
    my $digest = Koha::AuthUtils::hash_password( $input->param('newpassword') );
66
    my $digest = Koha::AuthUtils::hash_password( $input->param('newpassword') );
67
    my $uid    = $input->param('newuserid');
67
    my $uid    = $input->param('newuserid');
68
    my $dbh    = C4::Context->dbh;
68
    my $dbh    = C4::Context->dbh;
69
    if ( changepassword( $uid, $member, $digest ) ) {
69
    if ( Koha::Patrons->find( $member )->update_password($uid, $digest) ) {
70
        $template->param( newpassword => $newpassword );
70
        $template->param( newpassword => $newpassword );
71
        if ( $destination eq 'circ' ) {
71
        if ( $destination eq 'circ' ) {
72
            print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
72
            print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$cardnumber");
(-)a/opac/opac-password-recovery.pl (-2 / +2 lines)
Lines 5-15 use CGI; Link Here
5
5
6
use C4::Auth;
6
use C4::Auth;
7
use C4::Koha;
7
use C4::Koha;
8
use C4::Members qw(changepassword);
9
use C4::Output;
8
use C4::Output;
10
use C4::Context;
9
use C4::Context;
11
use Koha::Patron::Password::Recovery
10
use Koha::Patron::Password::Recovery
12
  qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
11
  qw(SendPasswordRecoveryEmail ValidateBorrowernumber GetValidLinkInfo CompletePasswordRecovery);
12
use Koha::Patrons;
13
use Koha::AuthUtils qw(hash_password);
13
use Koha::AuthUtils qw(hash_password);
14
use Koha::Patrons;
14
use Koha::Patrons;
15
my $query = new CGI;
15
my $query = new CGI;
Lines 135-141 elsif ( $query->param('passwordReset') ) { Link Here
135
        && ( $password eq $repeatPassword )
135
        && ( $password eq $repeatPassword )
136
        && ( length($password) >= $minPassLength ) )
136
        && ( length($password) >= $minPassLength ) )
137
    {    #apply changes
137
    {    #apply changes
138
        changepassword( $username, $borrower_number, hash_password($password) );
138
        Koha::Patrons->find($borrower_number)->update_password( $username, hash_password($password) );
139
        CompletePasswordRecovery($uniqueKey);
139
        CompletePasswordRecovery($uniqueKey);
140
        $template->param(
140
        $template->param(
141
            password_reset_done => 1,
141
            password_reset_done => 1,
(-)a/t/db_dependent/Auth.t (-1 / +2 lines)
Lines 17-22 use C4::Auth qw(checkpw); Link Here
17
use C4::Members;
17
use C4::Members;
18
use Koha::AuthUtils qw/hash_password/;
18
use Koha::AuthUtils qw/hash_password/;
19
use Koha::Database;
19
use Koha::Database;
20
use Koha::Patrons;
20
21
21
BEGIN {
22
BEGIN {
22
    use_ok('C4::Auth');
23
    use_ok('C4::Auth');
Lines 32-38 my $hash2 = hash_password('password'); Link Here
32
33
33
{ # tests no_set_userenv parameter
34
{ # tests no_set_userenv parameter
34
    my $patron = $builder->build( { source => 'Borrower' } );
35
    my $patron = $builder->build( { source => 'Borrower' } );
35
    changepassword( $patron->{userid}, $patron->{borrowernumber}, $hash1 );
36
    Koha::Patrons->find( $patron->{borrowernumber} )->update_password( $patron->{userid}, $hash1 );
36
    my $library = $builder->build(
37
    my $library = $builder->build(
37
        {
38
        {
38
            source => 'Branch',
39
            source => 'Branch',
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +30 lines)
Lines 19-31 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 6;
23
use Test::Warn;
23
24
24
use Koha::Patron;
25
use Koha::Patron;
25
use Koha::Patrons;
26
use Koha::Patrons;
26
use Koha::Database;
27
use Koha::Database;
27
28
28
use t::lib::TestBuilder;
29
use t::lib::TestBuilder;
30
use t::lib::Mocks;
29
31
30
my $schema = Koha::Database->new->schema;
32
my $schema = Koha::Database->new->schema;
31
$schema->storage->txn_begin;
33
$schema->storage->txn_begin;
Lines 40-45 my $new_patron_1 = Koha::Patron->new( Link Here
40
        categorycode => $category->{categorycode},
42
        categorycode => $category->{categorycode},
41
        surname => 'surname for patron1',
43
        surname => 'surname for patron1',
42
        firstname => 'firstname for patron1',
44
        firstname => 'firstname for patron1',
45
        userid => 'a_nonexistent_userid_1',
43
    }
46
    }
44
)->store;
47
)->store;
45
my $new_patron_2  = Koha::Patron->new(
48
my $new_patron_2  = Koha::Patron->new(
Lines 48-53 my $new_patron_2 = Koha::Patron->new( Link Here
48
        categorycode => $category->{categorycode},
51
        categorycode => $category->{categorycode},
49
        surname => 'surname for patron2',
52
        surname => 'surname for patron2',
50
        firstname => 'firstname for patron2',
53
        firstname => 'firstname for patron2',
54
        userid => 'a_nonexistent_userid_2',
51
    }
55
    }
52
)->store;
56
)->store;
53
57
Lines 98-103 subtest 'siblings' => sub { Link Here
98
    $retrieved_guarantee_1->delete;
102
    $retrieved_guarantee_1->delete;
99
};
103
};
100
104
105
subtest 'update_password' => sub {
106
    plan tests => 7;
107
108
    t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
109
    my $original_userid   = $new_patron_1->userid;
110
    my $original_password = $new_patron_1->password;
111
    warning_like { $retrieved_patron_1->update_password( $new_patron_2->userid, 'another_password' ) }
112
    qr{Duplicate entry},
113
      'Koha::Patron->update_password should warn if the userid is already used by another patron';
114
    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   $original_userid,   'Koha::Patron->update_password should not have updated the userid' );
115
    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, $original_password, 'Koha::Patron->update_password should not have updated the userid' );
116
117
    $retrieved_patron_1->update_password( 'another_nonexistent_userid_1', 'another_password' );
118
    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->userid,   'another_nonexistent_userid_1', 'Koha::Patron->update_password should have updated the userid' );
119
    is( Koha::Patrons->find( $new_patron_1->borrowernumber )->password, 'another_password',             'Koha::Patron->update_password should have updated the password' );
120
121
    my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
122
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should have logged' );
123
124
    t::lib::Mocks::mock_preference( 'BorrowersLog', 0 );
125
    $retrieved_patron_1->update_password( 'yet_another_nonexistent_userid_1', 'another_password' );
126
    $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'CHANGE PASS', object => $new_patron_1->borrowernumber } )->count;
127
    is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->update_password should not have logged' );
128
};
129
101
$retrieved_patron_1->delete;
130
$retrieved_patron_1->delete;
102
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
131
is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );
103
132
104
- 

Return to bug 16853