Bug 30982: Make code more re-usable
[koha.git] / Koha / BackgroundJobs.pm
1 package Koha::BackgroundJobs;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use base qw(Koha::Objects);
20 use Koha::BackgroundJob;
21
22 =head1 NAME
23
24 Koha::BackgroundJobs - Koha BackgroundJob Object set class
25
26 =head1 API
27
28 =head2 Class Methods
29
30 =cut
31
32 =head2 search_limited
33
34   my $background_jobs = Koha::BackgroundJobs->search_limited( $params, $attributes );
35
36 Returns all background jobs the logged in user should be allowed to see
37
38 =cut
39
40 sub search_limited {
41     my ( $self, $params, $attributes ) = @_;
42
43     # Assume permission if context has no user
44     my $can_manage_background_jobs = 1;
45
46     my $logged_in_user;
47     my $userenv = C4::Context->userenv;
48     if ( $userenv and $userenv->{number} ) {
49         $logged_in_user = Koha::Patrons->find( $userenv->{number} );
50         $can_manage_background_jobs = $logged_in_user->has_permission(
51             { parameters => 'manage_background_jobs' } );
52     }
53
54     return $can_manage_background_jobs
55       ? $self->search( $params, $attributes )
56       : $self->search( { borrowernumber => $logged_in_user->borrowernumber } )
57       ->search( $params, $attributes );
58 }
59
60 =head3 _type
61
62 =cut
63
64 sub _type {
65     return 'BackgroundJob';
66 }
67
68 =head3 object_class
69
70 =cut
71
72 sub object_class {
73     return 'Koha::BackgroundJob';
74 }
75
76 1;