Bug 29523: Cache the restricted branches list

This patch introduces a very localised cache of the restricted branches
list in the logged in patron object.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Martin Renvoize 2022-03-03 14:40:20 +00:00 committed by Tomas Cohen Arazi
parent 1477ffc752
commit 2c502d320e
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
2 changed files with 13 additions and 8 deletions

View file

@ -1787,13 +1787,8 @@ sub can_see_things_from {
$can = 1;
} elsif ( $self->has_permission( { $permission => $subpermission } ) ) {
$can = 1;
} elsif ( my $library_groups = $self->library->library_groups ) {
while ( my $library_group = $library_groups->next ) {
if ( $library_group->parent->has_child( $branchcode ) ) {
$can = 1;
last;
}
}
} elsif ( my @branches = $self->libraries_where_can_see_patrons ) {
$can = ( any { $_ eq $branchcode } @branches ) ? 1 : 0;
}
return $can;
}
@ -1844,6 +1839,9 @@ sub libraries_where_can_see_things {
my $subpermission = $params->{subpermission};
my $group_feature = $params->{group_feature};
return $self->{"_restricted_branchcodes:$permission:$subpermission:$group_feature"}
if exists( $self->{"_restricted_branchcodes:$permission:$subpermission:$group_feature"} );
my $userenv = C4::Context->userenv;
return () unless $userenv; # For tests, but userenv should be defined in tests...
@ -1877,7 +1875,9 @@ sub libraries_where_can_see_things {
@restricted_branchcodes = grep { defined $_ } @restricted_branchcodes;
@restricted_branchcodes = uniq(@restricted_branchcodes);
@restricted_branchcodes = sort(@restricted_branchcodes);
return @restricted_branchcodes;
$self->{"_restricted_branchcodes:$permission:$subpermission:$group_feature"} = \@restricted_branchcodes;
return @{ $self->{"_restricted_branchcodes:$permission:$subpermission:$group_feature"} };
}
=head3 has_permission

View file

@ -1350,11 +1350,13 @@ subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited
plan tests => 6;
t::lib::Mocks::mock_userenv({ patron => $patron_11_1 });
$patron_11_1 = Koha::Patrons->find( $patron_11_1->borrowernumber );
is( $patron_11_1->can_see_patron_infos( $patron_11_2 ), 1, q|patron_11_1 can see patron_11_2, from its library| );
is( $patron_11_1->can_see_patron_infos( $patron_12 ), 1, q|patron_11_1 can see patron_12, from its group| );
is( $patron_11_1->can_see_patron_infos( $patron_21 ), 1, q|patron_11_1 can see patron_11_2, from another group| );
t::lib::Mocks::mock_userenv({ patron => $patron_11_2 });
$patron_11_2 = Koha::Patrons->find( $patron_11_2->borrowernumber );
is( $patron_11_2->can_see_patron_infos( $patron_11_1 ), 1, q|patron_11_2 can see patron_11_1, from its library| );
is( $patron_11_2->can_see_patron_infos( $patron_12 ), 1, q|patron_11_2 can see patron_12, from its group| );
is( $patron_11_2->can_see_patron_infos( $patron_21 ), 0, q|patron_11_2 can NOT see patron_21, from another group| );
@ -1363,15 +1365,18 @@ subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited
plan tests => 6;
t::lib::Mocks::mock_userenv({ patron => $patron_11_1 });
$patron_11_1 = Koha::Patrons->find( $patron_11_1->borrowernumber );
my $total_number_of_patrons = $nb_of_patrons + 4; #we added four in these tests
is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons' );
is( Koha::Patrons->search_limited->count, $total_number_of_patrons, 'patron_11_1 is allowed to see all patrons' );
t::lib::Mocks::mock_userenv({ patron => $patron_11_2 });
$patron_11_2 = Koha::Patrons->find( $patron_11_2->borrowernumber );
is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
is( Koha::Patrons->search_limited->count, 3, 'patron_12_1 is not allowed to see patrons from other groups, only patron_11_1, patron_11_2 and patron_12' );
t::lib::Mocks::mock_userenv({ patron => $patron_21 });
$patron_21 = Koha::Patrons->find( $patron_21->borrowernumber );
is( Koha::Patrons->search->count, $total_number_of_patrons, 'Non-limited search should return all patrons');
is( Koha::Patrons->search_limited->count, 1, 'patron_21 is not allowed to see patrons from other groups, only himself' );
};