@@ -, +, @@ prove t/db_dependent/Koha/Patrons.t --- Koha/Patrons.pm | 39 ++++++++++++++++++ t/db_dependent/Koha/Patrons.t | 78 ++++++++++++++++++++++++++++++++++- 2 files changed, 116 insertions(+), 1 deletion(-) --- a/Koha/Patrons.pm +++ a/Koha/Patrons.pm @@ -520,6 +520,45 @@ sub filter_by_amount_owed { return $self->search( $where, $attrs ); } +=head3 filter_by_have_subpermission + + my $patrons = Koha::Patrons->search->filter_by_have_subpermission('suggestions.suggestions_manage'); + +Filter patrons who have a given subpermission + +=cut + +sub filter_by_have_subpermission { + my ($self, $subpermission) = @_; + + my ($p, $sp) = split '\.', $subpermission; + + my $perm = Koha::Database->new()->schema()->resultset('Userflag')->find({flag => $p}); + + Koha::Exceptions::ObjectNotFound->throw( sprintf( "Permission %s not found", $p ) ) + unless $perm; + + my $bit = $perm->bit; + + return $self->search( + { + -and => [ + -or => [ + \"me.flags & (1 << $bit)", + { 'me.flags' => 1 }, + { + -and => [ + { 'user_permissions.module_bit' => $bit }, + { 'user_permissions.code' => $sp } + ] + } + ] + ] + }, + { prefetch => 'user_permissions' } + ); +} + =head3 _type =cut --- a/t/db_dependent/Koha/Patrons.t +++ a/t/db_dependent/Koha/Patrons.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 43; +use Test::More tests => 44; use Test::Warn; use Test::Exception; use Test::MockModule; @@ -2346,4 +2346,80 @@ subtest 'filter_by_amount_owed' => sub { }; +subtest 'filter_by_have_subpermission' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $patron_1 = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 1, branchcode => $library->branchcode } + } + ); + + my $patron_2 = $builder->build_object( # 4096 = 1 << 12 for suggestions + { + class => 'Koha::Patrons', + value => { flags => 4096, branchcode => $library->branchcode } + } + ); + + my $patron_3 = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0, branchcode => $library->branchcode } + } + ); + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $patron_3->borrowernumber, + module_bit => 11, + code => 'order_manage', + }, + } + ); + + is_deeply( + [ + Koha::Patrons->search( { branchcode => $library->branchcode } ) + ->filter_by_have_subpermission('suggestions.suggestions_manage') + ->get_column('borrowernumber') + ], + [ $patron_1->borrowernumber, $patron_2->borrowernumber ], + 'Superlibrarian and patron with suggestions.suggestions_manage' + ); + + is_deeply( + [ + Koha::Patrons->search( { branchcode => $library->branchcode } ) + ->filter_by_have_subpermission('acquisition.order_manage') + ->get_column('borrowernumber') + ], + [ $patron_1->borrowernumber, $patron_3->borrowernumber ], + 'Superlibrarian and patron with acquisition.order_manage' + ); + + is_deeply( + [ + Koha::Patrons->search( { branchcode => $library->branchcode } ) + ->filter_by_have_subpermission('parameters.manage_cities') + ->get_column('borrowernumber') + ], + [ $patron_1->borrowernumber ], + 'Only Superlibrarian is returned' + ); + + throws_ok { + Koha::Patrons->search( { branchcode => $library->branchcode } ) + ->filter_by_have_subpermission('dont_exist.subperm'); + } 'Koha::Exceptions::ObjectNotFound'; + + + $schema->storage->txn_rollback; +}; + $schema->storage->txn_rollback; --