Bug 29542: Prevent access to private list to non authorized users
[koha.git] / virtualshelves / shelves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Team
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Auth qw( get_template_and_user );
23 use C4::Biblio qw( GetMarcBiblio );
24 use C4::Koha qw(
25     GetNormalizedEAN
26     GetNormalizedISBN
27     GetNormalizedOCLCNumber
28     GetNormalizedUPC
29 );
30 use C4::Items qw( GetItemsLocationInfo );
31 use C4::Members;
32 use C4::Output qw( pagination_bar output_html_with_http_headers );
33 use C4::XSLT qw( XSLTParse4Display );
34
35 use Koha::Biblios;
36 use Koha::Biblioitems;
37 use Koha::Items;
38 use Koha::ItemTypes;
39 use Koha::CsvProfiles;
40 use Koha::Patrons;
41 use Koha::Virtualshelves;
42
43 use constant ANYONE => 2;
44
45 my $query = CGI->new;
46
47 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
48     {   template_name   => "virtualshelves/shelves.tt",
49         query           => $query,
50         type            => "intranet",
51         flagsrequired   => { catalogue => 1 },
52     }
53 );
54
55 my $op       = $query->param('op')      || 'list';
56 my $referer  = $query->param('referer') || $op;
57 my $public   = $query->param('public') ? 1 : 0;
58 my ( $shelf, $shelfnumber, @messages );
59
60 if ( $op eq 'add_form' ) {
61     # Only pass default
62     $shelf = { allow_change_from_owner => 1 };
63 } elsif ( $op eq 'edit_form' ) {
64     $shelfnumber = $query->param('shelfnumber');
65     $shelf       = Koha::Virtualshelves->find($shelfnumber);
66
67     if ( $shelf ) {
68         $public = $shelf->public;
69         my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
70         $template->param( owner => $patron, );
71         unless ( $shelf->can_be_managed( $loggedinuser ) ) {
72             push @messages, { type => 'alert', code => 'unauthorized_on_update' };
73             $op = 'list';
74         }
75     } else {
76         push @messages, { type => 'alert', code => 'does_not_exist' };
77     }
78 } elsif ( $op eq 'add' ) {
79     my $allow_changes_from = $query->param('allow_changes_from');
80     eval {
81         $shelf = Koha::Virtualshelf->new(
82             {   shelfname          => scalar $query->param('shelfname'),
83                 sortfield          => scalar $query->param('sortfield'),
84                 public             => $public,
85                 allow_change_from_owner => $allow_changes_from > 0,
86                 allow_change_from_others => $allow_changes_from == ANYONE,
87                 owner              => scalar $query->param('owner'),
88             }
89         );
90         $shelf->store;
91         $shelfnumber = $shelf->shelfnumber;
92     };
93     if ($@) {
94         push @messages, { type => 'alert', code => ref($@), msg => $@ };
95     } elsif ( not $shelf ) {
96         push @messages, { type => 'alert', code => 'error_on_insert' };
97
98     } else {
99         push @messages, { type => 'message', code => 'success_on_insert' };
100         $op = 'view';
101     }
102 } elsif ( $op eq 'edit' ) {
103     $shelfnumber = $query->param('shelfnumber');
104     $shelf       = Koha::Virtualshelves->find($shelfnumber);
105
106     if ( $shelf ) {
107         $op = $referer;
108         my $sortfield = $query->param('sortfield');
109         $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
110         if ( $shelf->can_be_managed( $loggedinuser ) ) {
111             $shelf->shelfname( scalar $query->param('shelfname') );
112             $shelf->sortfield( $sortfield );
113             my $allow_changes_from = $query->param('allow_changes_from');
114             $shelf->allow_change_from_owner( $allow_changes_from > 0 );
115             $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
116             $shelf->public( scalar $query->param('public') );
117             eval { $shelf->store };
118
119             if ($@) {
120                 push @messages, { type => 'alert', code => 'error_on_update' };
121                 $op = 'edit_form';
122             } else {
123                 push @messages, { type => 'message', code => 'success_on_update' };
124             }
125         } else {
126             push @messages, { type => 'alert', code => 'unauthorized_on_update' };
127         }
128     } else {
129         push @messages, { type => 'alert', code => 'does_not_exist' };
130     }
131 } elsif ( $op eq 'delete' ) {
132     $shelfnumber = $query->param('shelfnumber');
133     $shelf       = Koha::Virtualshelves->find($shelfnumber);
134     if ($shelf) {
135         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
136             eval { $shelf->delete; };
137             if ($@) {
138                 push @messages, { type => 'alert', code => ref($@), msg => $@ };
139             } else {
140                 push @messages, { type => 'message', code => 'success_on_delete' };
141             }
142         } else {
143             push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
144         }
145     } else {
146         push @messages, { type => 'alert', code => 'does_not_exist' };
147     }
148     $op = 'list';
149 } elsif ( $op eq 'add_biblio' ) {
150     $shelfnumber = $query->param('shelfnumber');
151     $shelf = Koha::Virtualshelves->find($shelfnumber);
152     if ($shelf) {
153         if( my $barcodes = $query->param('barcodes') ) {
154             if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
155                 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
156                 foreach my $barcode (@barcodes){
157                     $barcode =~ s/\r$//; # strip any naughty return chars
158                     next if $barcode eq '';
159                     my $item = Koha::Items->find({barcode => $barcode});
160                     if ( $item ) {
161                         my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
162                         if ($@) {
163                             push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
164                         } elsif ( $added ) {
165                             push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
166                         } else {
167                             push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
168                         }
169                     } else {
170                         push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
171                     }
172                 }
173             } else {
174                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
175             }
176         }
177         if ( my $biblionumbers = $query->param('biblionumbers') ) {
178             if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
179                 my @biblionumbers = split /\n/, $biblionumbers;
180                 foreach my $biblionumber (@biblionumbers) {
181                     $biblionumber =~ s/\r$//; # strip any naughty return chars
182                     next if $biblionumber eq '';
183                     my $biblio = Koha::Biblios->find($biblionumber);
184                     if (defined $biblio) {
185                         my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
186                         if ($@) {
187                             push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
188                         } elsif ( $added ) {
189                             push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
190                         } else {
191                             push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
192                         }
193                     } else {
194                         push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
195                     }
196                 }
197             } else {
198                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
199             }
200         }
201     } else {
202         push @messages, { type => 'alert', code => 'does_not_exist' };
203     }
204     $op = $referer;
205 } elsif ( $op eq 'remove_biblios' ) {
206     $shelfnumber = $query->param('shelfnumber');
207     $shelf = Koha::Virtualshelves->find($shelfnumber);
208     my @biblionumbers = $query->multi_param('biblionumber');
209     if ($shelf) {
210         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
211             my $number_of_biblios_removed = eval {
212                 $shelf->remove_biblios(
213                     {
214                         biblionumbers => \@biblionumbers,
215                         borrowernumber => $loggedinuser,
216                     }
217                 );
218             };
219             if ($@) {
220                 push @messages, { type => 'alert', code => ref($@), msg => $@ };
221             } elsif ( $number_of_biblios_removed ) {
222                 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
223             } else {
224                 push @messages, { type => 'alert', code => 'no_biblio_removed' };
225             }
226         } else {
227             push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
228         }
229     } else {
230         push @messages, { type => 'alert', code => 'does_not_exist' };
231     }
232     $op = $referer;
233 }
234
235 if ( $op eq 'view' ) {
236     $shelfnumber ||= $query->param('shelfnumber');
237     $shelf = Koha::Virtualshelves->find($shelfnumber);
238     if ( $shelf ) {
239         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
240             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
241             $sortfield = 'title' unless grep { $_ eq $sortfield } qw( title author copyrightdate itemcallnumber dateadded );
242             my $direction = $query->param('direction') || 'asc';
243             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
244             my ( $rows, $page );
245             unless ( $query->param('print') ) {
246                 $rows = C4::Context->preference('numSearchResults') || 20;
247                 $page = ( $query->param('page') ? $query->param('page') : 1 );
248             }
249
250             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
251             my $contents = $shelf->get_contents->search(
252                 {},
253                 {
254                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
255                     page     => $page,
256                     rows     => $rows,
257                     order_by => { "-$direction" => $order_by },
258                 }
259             );
260
261             my @items;
262             while ( my $content = $contents->next ) {
263                 my $this_item;
264                 my $biblionumber = $content->biblionumber;
265                 my $record       = GetMarcBiblio({ biblionumber => $biblionumber });
266
267                 $this_item->{XSLTBloc} = XSLTParse4Display(
268                     {
269                         biblionumber => $biblionumber,
270                         record       => $record,
271                         xsl_syspref  => 'XSLTListsDisplay',
272                         fix_amps     => 1,
273                     }
274                 );
275
276                 my $marcflavour = C4::Context->preference("marcflavour");
277                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
278                 $itemtype = Koha::ItemTypes->find( $itemtype );
279                 my $biblio = Koha::Biblios->find( $content->biblionumber );
280                 $this_item->{title}             = $biblio->title;
281                 $this_item->{subtitle}          = $biblio->subtitle;
282                 $this_item->{medium}            = $biblio->medium;
283                 $this_item->{part_number}       = $biblio->part_number;
284                 $this_item->{part_name}         = $biblio->part_name;
285                 $this_item->{author}            = $biblio->author;
286                 $this_item->{dateadded}         = $content->dateadded;
287                 $this_item->{imageurl}          = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
288                 $this_item->{description}       = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
289                 $this_item->{notforloan}        = $itemtype->notforloan if $itemtype;
290                 $this_item->{'coins'}           = $biblio->get_coins;
291                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
292                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
293                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
294                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
295
296                 unless ( defined $this_item->{size} ) {
297
298                     #TT has problems with size
299                     $this_item->{size} = q||;
300                 }
301
302                 # Getting items infos for location display
303                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
304                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
305                 $this_item->{biblionumber} = $biblionumber;
306                 push @items, $this_item;
307             }
308
309             my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
310                 {
311                     borrowernumber => $loggedinuser,
312                     add_allowed    => 1,
313                     public         => 0,
314                 }
315             );
316             my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
317                 {
318                     borrowernumber => $loggedinuser,
319                     add_allowed    => 1,
320                     public         => 1,
321                 }
322             );
323
324             $template->param(
325                 add_to_some_private_shelves => $some_private_shelves,
326                 add_to_some_public_shelves  => $some_public_shelves,
327                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
328                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
329                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
330                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
331                 sortfield          => $sortfield,
332                 itemsloop          => \@items,
333                 sortfield          => $sortfield,
334                 direction          => $direction,
335             );
336             if ( $page ) {
337                 my $pager = $contents->pager;
338                 $template->param(
339                     pagination_bar => pagination_bar(
340                         q||, $pager->last_page - $pager->first_page + 1,
341                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
342                     ),
343                 );
344             }
345         } else {
346             push @messages, { type => 'error', code => 'unauthorized_on_view' };
347             undef $shelf;
348         }
349     } else {
350         push @messages, { type => 'alert', code => 'does_not_exist' };
351     }
352 }
353
354 $template->param(
355     op       => $op,
356     referer  => $referer,
357     shelf    => $shelf,
358     messages => \@messages,
359     public   => $public,
360     print    => scalar $query->param('print') || 0,
361     csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
362 );
363
364 output_html_with_http_headers $query, $cookie, $template->output;