From e1224f9b9b10ba19049ffd765e77d860116efba8 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 8 Mar 2022 12:10:30 +0100 Subject: [PATCH] Bug 32030: rename filter_by_have_subpermission with filter_by_have_permission Signed-off-by: Jonathan Field Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi --- Koha/Patrons.pm | 26 ++++++++++++++++---------- Koha/REST/V1/Acquisitions/Baskets.pm | 2 +- Koha/REST/V1/Acquisitions/Funds.pm | 4 ++-- Koha/REST/V1/Suggestions.pm | 2 +- t/db_dependent/Koha/Patrons.t | 22 ++++++++++++++++------ 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/Koha/Patrons.pm b/Koha/Patrons.pm index caf6d4a251..24e3d9de8a 100644 --- a/Koha/Patrons.pm +++ b/Koha/Patrons.pm @@ -520,15 +520,17 @@ sub filter_by_amount_owed { return $self->search( $where, $attrs ); } -=head3 filter_by_have_subpermission +=head3 filter_by_have_permission - my $patrons = Koha::Patrons->search->filter_by_have_subpermission('suggestions.suggestions_manage'); + my $patrons = Koha::Patrons->search->filter_by_have_permission('suggestions.suggestions_manage'); -Filter patrons who have a given subpermission + my $patrons = Koha::Patrons->search->filter_by_have_permission('suggestions'); + +Filter patrons who have a given subpermission or the whole permission. =cut -sub filter_by_have_subpermission { +sub filter_by_have_permission { my ($self, $subpermission) = @_; my ($p, $sp) = split '\.', $subpermission; @@ -546,12 +548,16 @@ sub filter_by_have_subpermission { -or => [ \"me.flags & (1 << $bit)", { 'me.flags' => 1 }, - { - -and => [ - { 'user_permissions.module_bit' => $bit }, - { 'user_permissions.code' => $sp } - ] - } + ( + $sp + ? { + -and => [ + { 'user_permissions.module_bit' => $bit }, + { 'user_permissions.code' => $sp } + ] + } + : () + ) ] ] }, diff --git a/Koha/REST/V1/Acquisitions/Baskets.pm b/Koha/REST/V1/Acquisitions/Baskets.pm index a521b631cc..da6152fbb3 100644 --- a/Koha/REST/V1/Acquisitions/Baskets.pm +++ b/Koha/REST/V1/Acquisitions/Baskets.pm @@ -46,7 +46,7 @@ sub list_managers { return try { - my $patrons_rs = Koha::Patrons->search->filter_by_have_subpermission('acquisition.order_manage'); + my $patrons_rs = Koha::Patrons->search->filter_by_have_permission('acquisition.order_manage'); my $patrons = $c->objects->search( $patrons_rs ); return $c->render( diff --git a/Koha/REST/V1/Acquisitions/Funds.pm b/Koha/REST/V1/Acquisitions/Funds.pm index 59f9f3577f..0d9e1a00a4 100644 --- a/Koha/REST/V1/Acquisitions/Funds.pm +++ b/Koha/REST/V1/Acquisitions/Funds.pm @@ -66,7 +66,7 @@ sub list_owners { return try { - my $patrons_rs = Koha::Patrons->search->filter_by_have_subpermission('acquisition.budget_modify'); + my $patrons_rs = Koha::Patrons->search->filter_by_have_permission('acquisition.budget_modify'); my $patrons = $c->objects->search( $patrons_rs ); return $c->render( @@ -90,7 +90,7 @@ sub list_users { return try { - my $patrons_rs = Koha::Patrons->search->filter_by_have_subpermission('acquisition.budget_modify'); + my $patrons_rs = Koha::Patrons->search->filter_by_have_permission('acquisition.budget_modify'); my $patrons = $c->objects->search( $patrons_rs ); return $c->render( diff --git a/Koha/REST/V1/Suggestions.pm b/Koha/REST/V1/Suggestions.pm index d7a63f2c4e..4bc27709d1 100644 --- a/Koha/REST/V1/Suggestions.pm +++ b/Koha/REST/V1/Suggestions.pm @@ -237,7 +237,7 @@ sub list_managers { return try { - my $patrons_rs = Koha::Patrons->search->filter_by_have_subpermission('suggestions.suggestions_manage'); + my $patrons_rs = Koha::Patrons->search->filter_by_have_permission('suggestions.suggestions_manage'); my $patrons = $c->objects->search( $patrons_rs ); return $c->render( diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index 627f0f9f4d..8cf874d35e 100755 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -2463,8 +2463,8 @@ subtest 'filter_by_amount_owed' => sub { }; -subtest 'filter_by_have_subpermission' => sub { - plan tests => 4; +subtest 'filter_by_have_permission' => sub { + plan tests => 5; $schema->storage->txn_begin; @@ -2503,7 +2503,7 @@ subtest 'filter_by_have_subpermission' => sub { is_deeply( [ Koha::Patrons->search( { branchcode => $library->branchcode } ) - ->filter_by_have_subpermission('suggestions.suggestions_manage') + ->filter_by_have_permission('suggestions.suggestions_manage') ->get_column('borrowernumber') ], [ $patron_1->borrowernumber, $patron_2->borrowernumber ], @@ -2513,7 +2513,7 @@ subtest 'filter_by_have_subpermission' => sub { is_deeply( [ Koha::Patrons->search( { branchcode => $library->branchcode } ) - ->filter_by_have_subpermission('acquisition.order_manage') + ->filter_by_have_permission('acquisition.order_manage') ->get_column('borrowernumber') ], [ $patron_1->borrowernumber, $patron_3->borrowernumber ], @@ -2523,16 +2523,26 @@ subtest 'filter_by_have_subpermission' => sub { is_deeply( [ Koha::Patrons->search( { branchcode => $library->branchcode } ) - ->filter_by_have_subpermission('parameters.manage_cities') + ->filter_by_have_permission('parameters.manage_cities') ->get_column('borrowernumber') ], [ $patron_1->borrowernumber ], 'Only Superlibrarian is returned' ); + is_deeply( + [ + Koha::Patrons->search( { branchcode => $library->branchcode } ) + ->filter_by_have_permission('suggestions') + ->get_column('borrowernumber') + ], + [ $patron_1->borrowernumber, $patron_2->borrowernumber ], + 'Superlibrarian and patron with suggestions' + ); + throws_ok { Koha::Patrons->search( { branchcode => $library->branchcode } ) - ->filter_by_have_subpermission('dont_exist.subperm'); + ->filter_by_have_permission('dont_exist.subperm'); } 'Koha::Exceptions::ObjectNotFound'; -- 2.39.5