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

(-)a/C4/Auth.pm (-32 / +11 lines)
Lines 2012-2018 sub checkpw { Link Here
2012
    # INTERNAL AUTH
2012
    # INTERNAL AUTH
2013
    if ($check_internal_as_fallback) {
2013
    if ($check_internal_as_fallback) {
2014
        @return = checkpw_internal( $userid, $password, $no_set_userenv );
2014
        @return = checkpw_internal( $userid, $password, $no_set_userenv );
2015
        push( @return, $patron );
2016
        $passwd_ok = 1 if $return[0] > 0;    # 1 or 2
2015
        $passwd_ok = 1 if $return[0] > 0;    # 1 or 2
2017
    }
2016
    }
2018
2017
Lines 2041-2083 sub checkpw_internal { Link Here
2041
    my ( $userid, $password, $no_set_userenv ) = @_;
2040
    my ( $userid, $password, $no_set_userenv ) = @_;
2042
2041
2043
    $password = Encode::encode( 'UTF-8', $password )
2042
    $password = Encode::encode( 'UTF-8', $password )
2044
      if Encode::is_utf8($password);
2043
        if Encode::is_utf8($password);
2045
2044
2046
    my $dbh = C4::Context->dbh;
2045
    my $patron = Koha::Patrons->find( { userid => $userid } ) || Koha::Patrons->find( { cardnumber => $userid } );
2047
    my $sth =
2048
      $dbh->prepare(
2049
        "select password,cardnumber,borrowernumber,userid,firstname,surname,borrowers.branchcode,branches.branchname,flags from borrowers join branches on borrowers.branchcode=branches.branchcode where userid=?"
2050
      );
2051
    $sth->execute($userid);
2052
    if ( $sth->rows ) {
2053
        my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname,
2054
            $surname, $branchcode, $branchname, $flags )
2055
          = $sth->fetchrow;
2056
2057
        if ( checkpw_hash( $password, $stored_hash ) ) {
2058
2046
2059
            C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber,
2047
    if ($patron) {
2060
                $firstname, $surname, $branchcode, $branchname, $flags ) unless $no_set_userenv;
2048
        if ( checkpw_hash( $password, $patron->password ) ) {
2061
            return 1, $cardnumber, $userid;
2049
            my $borrowernumber = $patron->id;
2062
        }
2063
    }
2064
    $sth =
2065
      $dbh->prepare(
2066
        "select password,cardnumber,borrowernumber,userid,firstname,surname,borrowers.branchcode,branches.branchname,flags from borrowers join branches on borrowers.branchcode=branches.branchcode where cardnumber=?"
2067
      );
2068
    $sth->execute($userid);
2069
    if ( $sth->rows ) {
2070
        my ( $stored_hash, $cardnumber, $borrowernumber, $userid, $firstname,
2071
            $surname, $branchcode, $branchname, $flags )
2072
          = $sth->fetchrow;
2073
2050
2074
        if ( checkpw_hash( $password, $stored_hash ) ) {
2051
            C4::Context->set_userenv(
2052
                "$borrowernumber", $patron->userid, $patron->cardnumber,
2053
                $patron->firstname, $patron->surname, $patron->branchcode, $patron->library->branchname, $patron->flags
2054
            ) unless $no_set_userenv;
2075
2055
2076
            C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber,
2056
            return ( 1, $patron->cardnumber, $patron->userid, $patron );
2077
                $firstname, $surname, $branchcode, $branchname, $flags ) unless $no_set_userenv;
2078
            return 1, $cardnumber, $userid;
2079
        }
2057
        }
2080
    }
2058
    }
2059
2081
    return 0;
2060
    return 0;
2082
}
2061
}
2083
2062
(-)a/C4/Auth_with_ldap.pm (-35 / +16 lines)
Lines 202-223 sub checkpw_ldap { Link Here
202
    # But we still have work to do.  See perldoc below for detailed breakdown.
202
    # But we still have work to do.  See perldoc below for detailed breakdown.
203
203
204
    my (%borrower);
204
    my (%borrower);
205
	my ($borrowernumber,$cardnumber,$local_userid,$savedpw) = exists_local($userid);
205
    my $patron = Koha::Patrons->find( { userid => $userid } ) || Koha::Patrons->find( { cardnumber => $userid } );
206
206
207
    my $patron;
207
    if (( $patron and $config{update}   ) or
208
    if (( $borrowernumber and $config{update}   ) or
208
        (!$patron and $config{replicate})   ) {
209
        (!$borrowernumber and $config{replicate})   ) {
209
        my $cardnumber = $patron ? $patron->cardnumber : q{};
210
        %borrower = ldap_entry_2_hash($userldapentry,$cardnumber);
210
        %borrower = ldap_entry_2_hash($userldapentry,$cardnumber);
211
        #warn "checkpw_ldap received \%borrower w/ " . keys(%borrower), " keys: ", join(' ', keys %borrower), "\n";
212
    }
211
    }
213
212
214
    if ($borrowernumber) {
213
    if ($patron) {
215
        if ($config{update}) { # A1, B1
214
        if ( $config{update} ) {    # A1, B1
216
            my $c2 = &update_local($local_userid,$password,$borrowernumber,\%borrower) || '';
215
            my $c2 = &update_local( $patron->userid, $password, $patron->id, \%borrower ) || '';
217
            ($cardnumber eq $c2) or warn "update_local returned cardnumber '$c2' instead of '$cardnumber'";
216
            ( $patron->cardnumber eq $c2 )
218
        } else { # C1, D1
217
                or warn "update_local returned cardnumber '$c2' instead of '$patron->cardnumber'";
219
            # maybe update just the password?
218
        } else {                    # C1, D1
220
		return(1, $cardnumber, $local_userid);
219
                                    # maybe update just the password?
220
            return ( 1, $patron->cardnumber, $patron->userid, $patron );
221
        }
221
        }
222
    } elsif ($config{replicate}) { # A2, C2
222
    } elsif ($config{replicate}) { # A2, C2
223
        my @columns = Koha::Patrons->columns;
223
        my @columns = Koha::Patrons->columns;
Lines 227-236 sub checkpw_ldap { Link Here
227
            }
227
            }
228
        )->store;
228
        )->store;
229
        die "Insert of new patron failed" unless $patron;
229
        die "Insert of new patron failed" unless $patron;
230
        $borrowernumber = $patron->borrowernumber;
231
        C4::Members::Messaging::SetMessagingPreferencesFromDefaults(
230
        C4::Members::Messaging::SetMessagingPreferencesFromDefaults(
232
            {
231
            {
233
                borrowernumber => $borrowernumber,
232
                borrowernumber => $patron->id,
234
                categorycode   => $borrower{'categorycode'}
233
                categorycode   => $borrower{'categorycode'}
235
            }
234
            }
236
        );
235
        );
Lines 268-274 sub checkpw_ldap { Link Here
268
   } else {
267
   } else {
269
        return 0;   # B2, D2
268
        return 0;   # B2, D2
270
    }
269
    }
271
    if (C4::Context->preference('ExtendedPatronAttributes') && $borrowernumber && ($config{update} ||$config{replicate})) {
270
    if (C4::Context->preference('ExtendedPatronAttributes') && $patron->id && ($config{update} ||$config{replicate})) {
272
        my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
271
        my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
273
        my $attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id);
272
        my $attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id);
274
        while ( my $attribute_type = $attribute_types->next ) {
273
        while ( my $attribute_type = $attribute_types->next ) {
Lines 276-282 sub checkpw_ldap { Link Here
276
            unless (exists($borrower{$code}) && $borrower{$code} !~ m/^\s*$/ ) {
275
            unless (exists($borrower{$code}) && $borrower{$code} !~ m/^\s*$/ ) {
277
                next;
276
                next;
278
            }
277
            }
279
            $patron = Koha::Patrons->find($borrowernumber);
278
            $patron = Koha::Patrons->find($patron->id);
280
            if ( $patron ) { # Should not be needed, but we are in C4::Auth LDAP...
279
            if ( $patron ) { # Should not be needed, but we are in C4::Auth LDAP...
281
                eval {
280
                eval {
282
                    my $attribute = Koha::Patron::Attribute->new({code => $code, attribute => $borrower{$code}});
281
                    my $attribute = Koha::Patron::Attribute->new({code => $code, attribute => $borrower{$code}});
Lines 288-294 sub checkpw_ldap { Link Here
288
            }
287
            }
289
        }
288
        }
290
    }
289
    }
291
    return(1, $cardnumber, $userid, $patron);
290
    return ( 1, $patron->cardnumber, $userid, $patron );
292
}
291
}
293
292
294
# Pass LDAP entry object and local cardnumber (userid).
293
# Pass LDAP entry object and local cardnumber (userid).
Lines 344-366 sub ldap_entry_2_hash { Link Here
344
	return %borrower;
343
	return %borrower;
345
}
344
}
346
345
347
sub exists_local {
348
	my $arg = shift;
349
	my $dbh = C4::Context->dbh;
350
	my $select = "SELECT borrowernumber,cardnumber,userid,password FROM borrowers ";
351
352
	my $sth = $dbh->prepare("$select WHERE userid=?");	# was cardnumber=?
353
	$sth->execute($arg);
354
    #warn "Userid '$arg' exists_local? %s\n", $sth->rows;
355
	($sth->rows == 1) and return $sth->fetchrow;
356
357
	$sth = $dbh->prepare("$select WHERE cardnumber=?");
358
	$sth->execute($arg);
359
    #warn "Cardnumber '$arg' exists_local? %s\n", $sth->rows;
360
	($sth->rows == 1) and return $sth->fetchrow;
361
	return 0;
362
}
363
364
# This function performs a password update, given the userid, borrowerid,
346
# This function performs a password update, given the userid, borrowerid,
365
# and digested password. It will verify that things are correct and return the
347
# and digested password. It will verify that things are correct and return the
366
# borrowers cardnumber. The idea is that it is used to keep the local
348
# borrowers cardnumber. The idea is that it is used to keep the local
367
- 

Return to bug 36161