}
sub checkpw {
- my ( $dbh, $userid, $password, $query, $type ) = @_;
+ my ( $dbh, $userid, $password, $query, $type, $no_set_userenv ) = @_;
$type = 'opac' unless $type;
if ($ldap) {
$debug and print STDERR "## checkpw - checking LDAP\n";
}
# INTERNAL AUTH
- return checkpw_internal(@_)
+ return checkpw_internal( $dbh, $userid, $password, $no_set_userenv);
}
sub checkpw_internal {
- my ( $dbh, $userid, $password ) = @_;
+ my ( $dbh, $userid, $password, $no_set_userenv ) = @_;
$password = Encode::encode( 'UTF-8', $password )
if Encode::is_utf8($password);
if ( checkpw_hash( $password, $stored_hash ) ) {
C4::Context->set_userenv( "$borrowernumber", $userid, $cardnumber,
- $firstname, $surname, $branchcode, $branchname, $flags );
+ $firstname, $surname, $branchcode, $branchname, $flags ) unless $no_set_userenv;
return 1, $cardnumber, $userid;
}
}
if ( checkpw_hash( $password, $stored_hash ) ) {
C4::Context->set_userenv( $borrowernumber, $userid, $cardnumber,
- $firstname, $surname, $branchcode, $branchname, $flags );
+ $firstname, $surname, $branchcode, $branchname, $flags ) unless $no_set_userenv;
return 1, $cardnumber, $userid;
}
}
my $dbh = C4::Context->dbh;
my $ret = 0;
- ($ret) = checkpw( $dbh, $self->{userid}, $pwd );
+ ($ret) = checkpw( $dbh, $self->{userid}, $pwd, undef, undef, 1 ); # dbh, userid, query, type, no_set_userenv
return $ret;
}
use CGI qw ( -utf8 );
use Test::MockModule;
use List::MoreUtils qw/all any none/;
-use Test::More tests => 13;
+use Test::More tests => 18;
use Test::Warn;
use t::lib::Mocks;
+use t::lib::TestBuilder;
+
+use C4::Auth qw(checkpw);
use C4::Members;
use Koha::AuthUtils qw/hash_password/;
+use Koha::Database;
BEGIN {
- use_ok('C4::Auth');
+ use_ok('C4::Auth');
}
-my $dbh = C4::Context->dbh;
-
-# Start transaction
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
-
+my $schema = Koha::Database->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
+my $dbh = C4::Context->dbh;
# get_template_and_user tests
ok(C4::Auth::checkpw_hash('password', $hash1), 'password validates with first hash');
ok(C4::Auth::checkpw_hash('password', $hash2), 'password validates with second hash');
-$dbh->rollback;
+my $patron = $builder->build( { source => 'Borrower' } );
+changepassword( $patron->{userid}, $patron->{borrowernumber}, $hash1 );
+my $library = $builder->build(
+ {
+ source => 'Branch',
+ }
+);
+
+C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, 'Library 1', 0, '', '');
+is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv gives correct branch' );
+ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 1 ), 'checkpw returns true' );
+is( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is preserved if no_set_userenv is true' );
+ok( checkpw( $dbh, $patron->{userid}, 'password', undef, undef, 0 ), 'checkpw still returns true' );
+isnt( C4::Context->userenv->{branch}, $library->{branchcode}, 'Userenv branch is overwritten if no_set_userenv is false' );