From 557f8fe02d19bb67b8ce0ba3f477ca6fa0fcbdbe Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 20 Apr 2021 14:21:04 -0300 Subject: [PATCH] Bug 28157: Add Koha::Patron->can_log_into This patch adds a new method that tells if the patron is allowed to be logged into certain library. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/api/v1/auth_authenticate_api_request.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: David Nind --- Koha/Patron.pm | 27 +++++++++++++++++++++++++++ t/db_dependent/Koha/Patron.t | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 8606ab0bb8..d47b3f77d2 100644 --- a/Koha/Patron.pm +++ b/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; diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index c5a2cbfd90..cd6c30f086 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/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; +}; -- 2.11.0