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

(-)a/Koha/Item.pm (+17 lines)
Lines 1040-1045 sub to_api_mapping { Link Here
1040
    };
1040
    };
1041
}
1041
}
1042
1042
1043
=head3 api_privileged_attrs
1044
1045
This method returns the list of privileged access-only attributes. This is used
1046
by $item->to_api($params) to render the object correctly, based on the passed I<$params>.
1047
1048
=cut
1049
1050
sub api_privileged_attrs {
1051
    return [
1052
        'checked_out_date',
1053
        'checkouts_count',
1054
        'holds_count',
1055
        'internal_notes',
1056
        'extended_subfields',
1057
    ];
1058
}
1059
1043
=head3 itemtype
1060
=head3 itemtype
1044
1061
1045
    my $itemtype = $item->itemtype;
1062
    my $itemtype = $item->itemtype;
(-)a/Koha/Object.pm (-9 / +34 lines)
Lines 539-544 sub prefetch_whitelist { Link Here
539
                    ...
539
                    ...
540
                }
540
                }
541
            },
541
            },
542
            public => 0|1,
542
            ...
543
            ...
543
         ]
544
         ]
544
        }
545
        }
Lines 573-579 sub to_api { Link Here
573
        }
574
        }
574
    }
575
    }
575
576
576
    my $embeds = $params->{embed};
577
    # Remove forbidden attributes if required
578
    if (    $params->{public}
579
        and $self->can('api_privileged_attrs') )
580
    {
581
        foreach my $privileged_attr ( @{ $self->api_privileged_attrs } ) {
582
            delete $json_object->{$privileged_attr};
583
        }
584
    }
585
586
    # Make sure we duplicate the $params variable to avoid
587
    # breaking calls in a loop (Koha::Objects->to_api)
588
    $params    = {%$params};
589
    my $embeds = delete $params->{embed};
577
590
578
    if ($embeds) {
591
    if ($embeds) {
579
        foreach my $embed ( keys %{$embeds} ) {
592
        foreach my $embed ( keys %{$embeds} ) {
Lines 592-611 sub to_api { Link Here
592
                if ( defined $children and ref($children) eq 'ARRAY' ) {
605
                if ( defined $children and ref($children) eq 'ARRAY' ) {
593
                    my @list = map {
606
                    my @list = map {
594
                        $self->_handle_to_api_child(
607
                        $self->_handle_to_api_child(
595
                            { child => $_, next => $next, curr => $curr } )
608
                            {
609
                                child  => $_,
610
                                next   => $next,
611
                                curr   => $curr,
612
                                params => $params
613
                            }
614
                        )
596
                    } @{$children};
615
                    } @{$children};
597
                    $json_object->{$curr} = \@list;
616
                    $json_object->{$curr} = \@list;
598
                }
617
                }
599
                else {
618
                else {
600
                    $json_object->{$curr} = $self->_handle_to_api_child(
619
                    $json_object->{$curr} = $self->_handle_to_api_child(
601
                        { child => $children, next => $next, curr => $curr } );
620
                        {
621
                            child  => $children,
622
                            next   => $next,
623
                            curr   => $curr,
624
                            params => $params
625
                        }
626
                    );
602
                }
627
                }
603
            }
628
            }
604
        }
629
        }
605
    }
630
    }
606
631
607
608
609
    return $json_object;
632
    return $json_object;
610
}
633
}
611
634
Lines 850-858 sub _type { } Link Here
850
sub _handle_to_api_child {
873
sub _handle_to_api_child {
851
    my ($self, $args ) = @_;
874
    my ($self, $args ) = @_;
852
875
853
    my $child = $args->{child};
876
    my $child  = $args->{child};
854
    my $next  = $args->{next};
877
    my $next   = $args->{next};
855
    my $curr  = $args->{curr};
878
    my $curr   = $args->{curr};
879
    my $params = $args->{params};
856
880
857
    my $res;
881
    my $res;
858
882
Lines 862-868 sub _handle_to_api_child { Link Here
862
            if defined $next and blessed $child and !$child->can('to_api');
886
            if defined $next and blessed $child and !$child->can('to_api');
863
887
864
        if ( blessed $child ) {
888
        if ( blessed $child ) {
865
            $res = $child->to_api({ embed => $next });
889
            $params->{embed} = $next;
890
            $res = $child->to_api($params);
866
        }
891
        }
867
        else {
892
        else {
868
            $res = $child;
893
            $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