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

(-)a/Koha/Library.pm (+19 lines)
Lines 258-263 sub to_api_mapping { Link Here
258
    };
258
    };
259
}
259
}
260
260
261
=head3 api_privileged_attrs
262
263
This method returns the list of privileged access-only attributes. This is used
264
by $library->to_api($params) to render the object correctly, based on the passed I<$params>.
265
266
=cut
267
268
sub api_privileged_attrs {
269
    return [
270
        'illemail',
271
        'reply_to_email',
272
        'return_path_email',
273
        'ip',
274
        'notes',
275
        'marc_org_code'
276
    ];
277
}
278
279
261
=head3 get_hold_libraries
280
=head3 get_hold_libraries
262
281
263
Return all libraries (including self) that belong to the same hold groups
282
Return all libraries (including self) that belong to the same hold groups
(-)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 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::Library->api_privileged_attrs };
379
380
        plan tests => scalar @privileged_attrs * 2;
381
382
        # Create a sample library
383
        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
384
385
        my $unprivileged_representation = $library->to_api({ public => 1 });
386
        my $privileged_representation   = $library->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 / +38 lines)
Lines 315-321 subtest '->search() tests' => sub { Link Here
315
315
316
subtest "to_api() tests" => sub {
316
subtest "to_api() tests" => sub {
317
317
318
    plan tests => 18;
318
    plan tests => 19;
319
319
320
    $schema->storage->txn_begin;
320
    $schema->storage->txn_begin;
321
321
Lines 398-403 subtest "to_api() tests" => sub { Link Here
398
        $i++;
398
        $i++;
399
    }
399
    }
400
400
401
    subtest 'unprivileged request tests' => sub {
402
403
        my @privileged_attrs = @{ Koha::Library->api_privileged_attrs };
404
405
        # Create sample libraries
406
        my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
407
        my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
408
        my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
409
        my $libraries = Koha::Libraries->search(
410
            {
411
                branchcode => {
412
                    '-in' => [
413
                        $library_1->branchcode, $library_2->branchcode,
414
                        $library_3->branchcode
415
                    ]
416
                }
417
            }
418
        );
419
420
        plan tests => scalar @privileged_attrs * 2 * $libraries->count;
421
422
        my $libraries_unprivileged_representation = $libraries->to_api({ public => 1 });
423
        my $libraries_privileged_representation   = $libraries->to_api();
424
425
        for (my $i = 0; $i < $libraries->count; $i++) {
426
            my $privileged_representation   = $libraries_privileged_representation->[$i];
427
            my $unprivileged_representation = $libraries_unprivileged_representation->[$i];
428
            foreach my $privileged_attr ( @privileged_attrs ) {
429
                ok( exists $privileged_representation->{$privileged_attr},
430
                    "Attribute $privileged_attr' is present" );
431
                ok( !exists $unprivileged_representation->{$privileged_attr},
432
                    "Attribute '$privileged_attr' is not present" );
433
            }
434
        }
435
    };
436
437
401
    $schema->storage->txn_rollback;
438
    $schema->storage->txn_rollback;
402
};
439
};
403
440
404
- 

Return to bug 28948