@@ -, +, @@ $ kshell k$ prove t/db_dependent/api/v1/auth_authenticate_api_request.t --- Koha/Patron.pm | 27 ++++++++++++++++++++++++++ t/db_dependent/Koha/Patron.t | 37 +++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) --- a/Koha/Patron.pm +++ a/Koha/Patron.pm @@ -1364,6 +1364,33 @@ sub can_see_patrons_from { return $can; } +=head3 can_log_into + +my $can_log_into = $patron->can_log_into( $library ); + +Given a I object, it returns a boolean representing +the fact the patron can log into a the library. + +=cut + +sub can_log_into { + my ( $self, $library ) = @_; + + my $can = 0; + + if ( C4::Context->preference('IndependentBranches') ) { + $can = 1 + if $self->is_superlibrarian + or $self->branchcode eq $library->id; + } + else { + # no restrictions + $can = 1; + } + + return $can; +} + =head3 libraries_where_can_see_patrons my $libraries = $patron-libraries_where_can_see_patrons; --- a/t/db_dependent/Koha/Patron.t +++ a/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 8; use Test::Exception; use Test::Warn; @@ -662,3 +662,38 @@ subtest 'extended_attributes' => sub { $schema->storage->txn_rollback; }; + +subtest 'can_log_into() tests' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { + flags => undef + } + } + ); + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + + t::lib::Mocks::mock_preference('IndependentBranches', 1); + + ok( $patron->can_log_into( $patron->library ), 'Patron can log into its own library' ); + ok( !$patron->can_log_into( $library ), 'Patron cannot log into different library, IndependentBranches on' ); + + # make it a superlibrarian + $patron->set({ flags => 1 })->store->discard_changes; + ok( $patron->can_log_into( $library ), 'Superlibrarian can log into different library, IndependentBranches on' ); + + t::lib::Mocks::mock_preference('IndependentBranches', 0); + + # No special permissions + $patron->set({ flags => undef })->store->discard_changes; + ok( $patron->can_log_into( $patron->library ), 'Patron can log into its own library' ); + ok( $patron->can_log_into( $library ), 'Patron can log into any library' ); + + $schema->storage->txn_rollback; +}; --