From f54b171584ff15b58d8c71b574914c8580c3bbc7 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 29 Jun 2022 15:14:41 +0100 Subject: [PATCH] Bug 30982: Make code more re-usable Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/BackgroundJobs.pm | 28 ++++++++++++++++++++++++++++ Koha/REST/V1/BackgroundJobs.pm | 16 ++-------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/Koha/BackgroundJobs.pm b/Koha/BackgroundJobs.pm index d2ff22347f..194123020d 100644 --- a/Koha/BackgroundJobs.pm +++ b/Koha/BackgroundJobs.pm @@ -29,6 +29,34 @@ Koha::BackgroundJobs - Koha BackgroundJob Object set class =cut +=head2 search_limited + + my $background_jobs = Koha::BackgroundJobs->search_limited( $params, $attributes ); + +Returns all background jobs the logged in user should be allowed to see + +=cut + +sub search_limited { + my ( $self, $params, $attributes ) = @_; + + # Assume permission if context has no user + my $can_manage_background_jobs = 1; + + my $logged_in_user; + my $userenv = C4::Context->userenv; + if ( $userenv and $userenv->{number} ) { + $logged_in_user = Koha::Patrons->find( $userenv->{number} ); + $can_manage_background_jobs = $logged_in_user->has_permission( + { parameters => 'manage_background_jobs' } ); + } + + return $can_manage_background_jobs + ? $self->search( $params, $attributes ) + : $self->search( { borrowernumber => $logged_in_user->borrowernumber } ) + ->search( $params, $attributes ); +} + =head3 _type =cut diff --git a/Koha/REST/V1/BackgroundJobs.pm b/Koha/REST/V1/BackgroundJobs.pm index 55f121b403..251d5657f3 100644 --- a/Koha/REST/V1/BackgroundJobs.pm +++ b/Koha/REST/V1/BackgroundJobs.pm @@ -35,24 +35,13 @@ sub list { my $c = shift->openapi->valid_input or return; return try { - my $patron = $c->stash('koha.user'); - - my $can_manage_background_jobs = - $patron->has_permission( { parameters => 'manage_background_jobs' } ); - - my $background_jobs_set = - $can_manage_background_jobs - ? Koha::BackgroundJobs->new - : Koha::BackgroundJobs->search( - { borrowernumber => $patron->borrowernumber } ); - - my $background_jobs = $c->objects->search( $background_jobs_set ); + my $background_jobs_set = Koha::BackgroundJobs->new; + my $background_jobs = $c->objects->search($background_jobs_set); return $c->render( status => 200, openapi => $background_jobs ); } catch { $c->unhandled_exception($_); }; - } sub get { @@ -90,5 +79,4 @@ sub get { }; } - 1; -- 2.39.5