View | Details | Raw Unified | Return to bug 27358
Collapse All | Expand All

(-)a/Koha/Item.pm (+17 lines)
Lines 1159-1164 sub to_api_mapping { Link Here
1159
    };
1159
    };
1160
}
1160
}
1161
1161
1162
=head3 api_privileged_attrs
1163
1164
This method returns the list of privileged access-only attributes. This is used
1165
by $item->to_api($params) to render the object correctly, based on the passed I<$params>.
1166
1167
=cut
1168
1169
sub api_privileged_attrs {
1170
    return [
1171
        'checked_out_date',
1172
        'checkouts_count',
1173
        'holds_count',
1174
        'internal_notes',
1175
        'extended_subfields',
1176
    ];
1177
}
1178
1162
=head3 itemtype
1179
=head3 itemtype
1163
1180
1164
    my $itemtype = $item->itemtype;
1181
    my $itemtype = $item->itemtype;
(-)a/Koha/Object.pm (-9 / +34 lines)
Lines 538-543 sub prefetch_whitelist { Link Here
538
                    ...
538
                    ...
539
                }
539
                }
540
            },
540
            },
541
            public => 0|1,
541
            ...
542
            ...
542
         ]
543
         ]
543
        }
544
        }
Lines 572-578 sub to_api { Link Here
572
        }
573
        }
573
    }
574
    }
574
575
575
    my $embeds = $params->{embed};
576
    # Remove forbidden attributes if required
577
    if (    $params->{public}
578
        and $self->can('api_privileged_attrs') )
579
    {
580
        foreach my $privileged_attr ( @{ $self->api_privileged_attrs } ) {
581
            delete $json_object->{$privileged_attr};
582
        }
583
    }
584
585
    # Make sure we duplicate the $params variable to avoid
586
    # breaking calls in a loop (Koha::Objects->to_api)
587
    $params    = {%$params};
588
    my $embeds = delete $params->{embed};
576
589
577
    if ($embeds) {
590
    if ($embeds) {
578
        foreach my $embed ( keys %{$embeds} ) {
591
        foreach my $embed ( keys %{$embeds} ) {
Lines 591-610 sub to_api { Link Here
591
                if ( defined $children and ref($children) eq 'ARRAY' ) {
604
                if ( defined $children and ref($children) eq 'ARRAY' ) {
592
                    my @list = map {
605
                    my @list = map {
593
                        $self->_handle_to_api_child(
606
                        $self->_handle_to_api_child(
594
                            { child => $_, next => $next, curr => $curr } )
607
                            {
608
                                child  => $_,
609
                                next   => $next,
610
                                curr   => $curr,
611
                                params => $params
612
                            }
613
                        )
595
                    } @{$children};
614
                    } @{$children};
596
                    $json_object->{$curr} = \@list;
615
                    $json_object->{$curr} = \@list;
597
                }
616
                }
598
                else {
617
                else {
599
                    $json_object->{$curr} = $self->_handle_to_api_child(
618
                    $json_object->{$curr} = $self->_handle_to_api_child(
600
                        { child => $children, next => $next, curr => $curr } );
619
                        {
620
                            child  => $children,
621
                            next   => $next,
622
                            curr   => $curr,
623
                            params => $params
624
                        }
625
                    );
601
                }
626
                }
602
            }
627
            }
603
        }
628
        }
604
    }
629
    }
605
630
606
607
608
    return $json_object;
631
    return $json_object;
609
}
632
}
610
633
Lines 849-857 sub _type { } Link Here
849
sub _handle_to_api_child {
872
sub _handle_to_api_child {
850
    my ($self, $args ) = @_;
873
    my ($self, $args ) = @_;
851
874
852
    my $child = $args->{child};
875
    my $child  = $args->{child};
853
    my $next  = $args->{next};
876
    my $next   = $args->{next};
854
    my $curr  = $args->{curr};
877
    my $curr   = $args->{curr};
878
    my $params = $args->{params};
855
879
856
    my $res;
880
    my $res;
857
881
Lines 861-867 sub _handle_to_api_child { Link Here
861
            if defined $next and blessed $child and !$child->can('to_api');
885
            if defined $next and blessed $child and !$child->can('to_api');
862
886
863
        if ( blessed $child ) {
887
        if ( blessed $child ) {
864
            $res = $child->to_api({ embed => $next });
888
            $params->{embed} = $next;
889
            $res = $child->to_api($params);
865
        }
890
        }
866
        else {
891
        else {
867
            $res = $child;
892
            $res = $child;
(-)a/t/db_dependent/Koha/Object.t (-1 / +21 lines)
Lines 223-229 subtest 'TO_JSON tests' => sub { Link Here
223
223
224
subtest "to_api() tests" => sub {
224
subtest "to_api() tests" => sub {
225
225
226
    plan tests => 28;
226
    plan tests => 29;
227
227
228
    $schema->storage->txn_begin;
228
    $schema->storage->txn_begin;
229
229
Lines 373-378 subtest "to_api() tests" => sub { Link Here
373
        'Koha::Exceptions::Object::MethodNotCoveredByTests',
373
        'Koha::Exceptions::Object::MethodNotCoveredByTests',
374
        'Unknown method exception thrown if is_count not specified';
374
        'Unknown method exception thrown if is_count not specified';
375
375
376
    subtest 'unprivileged request tests' => sub {
377
378
        my @privileged_attrs = @{ Koha::Item->api_privileged_attrs };
379
380
        plan tests => scalar @privileged_attrs * 2;
381
382
        # Create a sample item
383
        my $item = $builder->build_sample_item();
384
385
        my $unprivileged_representation = $item->to_api({ public => 1 });
386
        my $privileged_representation   = $item->to_api;
387
388
        foreach my $privileged_attr ( @privileged_attrs ) {
389
            ok( exists $privileged_representation->{$privileged_attr},
390
                "Attribute $privileged_attr' is present" );
391
            ok( !exists $unprivileged_representation->{$privileged_attr},
392
                "Attribute '$privileged_attr' is not present" );
393
        }
394
    };
395
376
    $schema->storage->txn_rollback;
396
    $schema->storage->txn_rollback;
377
};
397
};
378
398
(-)a/t/db_dependent/Koha/Objects.t (-2 / +29 lines)
Lines 313-319 subtest '->search() tests' => sub { Link Here
313
313
314
subtest "to_api() tests" => sub {
314
subtest "to_api() tests" => sub {
315
315
316
    plan tests => 18;
316
    plan tests => 19;
317
317
318
    $schema->storage->txn_begin;
318
    $schema->storage->txn_begin;
319
319
Lines 396-401 subtest "to_api() tests" => sub { Link Here
396
        $i++;
396
        $i++;
397
    }
397
    }
398
398
399
    subtest 'unprivileged request tests' => sub {
400
401
        my @privileged_attrs = @{ Koha::Item->api_privileged_attrs };
402
403
        # Create sample items
404
        my $biblio = $builder->build_sample_biblio;
405
        my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->id });
406
        my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->id });
407
        my $item_3 = $builder->build_sample_item({ biblionumber => $biblio->id });
408
409
        plan tests => scalar @privileged_attrs * 2 * $biblio->items->count;
410
411
        my $items_unprivileged_representation = $biblio->items->to_api({ public => 1 });
412
        my $items_privileged_representation   = $biblio->items->to_api();
413
414
        for (my $i = 0; $i < $biblio->items->count; $i++) {
415
            my $privileged_representation   = $items_privileged_representation->[$i];
416
            my $unprivileged_representation = $items_unprivileged_representation->[$i];
417
            foreach my $privileged_attr ( @privileged_attrs ) {
418
                ok( exists $privileged_representation->{$privileged_attr},
419
                    "Attribute $privileged_attr' is present" );
420
                ok( !exists $unprivileged_representation->{$privileged_attr},
421
                    "Attribute '$privileged_attr' is not present" );
422
            }
423
        }
424
    };
425
426
399
    $schema->storage->txn_rollback;
427
    $schema->storage->txn_rollback;
400
};
428
};
401
429
402
- 

Return to bug 27358