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

(-)a/Koha/Library.pm (-51 / +47 lines)
Lines 226-282 sub cash_registers { Link Here
226
    return Koha::Cash::Registers->_new_from_dbic( $rs );
226
    return Koha::Cash::Registers->_new_from_dbic( $rs );
227
}
227
}
228
228
229
=head3 to_api_mapping
230
231
This method returns the mapping for representing a Koha::Library object
232
on the API.
233
234
=cut
235
236
sub to_api_mapping {
237
    return {
238
        branchcode       => 'library_id',
239
        branchname       => 'name',
240
        branchaddress1   => 'address1',
241
        branchaddress2   => 'address2',
242
        branchaddress3   => 'address3',
243
        branchzip        => 'postal_code',
244
        branchcity       => 'city',
245
        branchstate      => 'state',
246
        branchcountry    => 'country',
247
        branchphone      => 'phone',
248
        branchfax        => 'fax',
249
        branchemail      => 'email',
250
        branchillemail   => 'illemail',
251
        branchreplyto    => 'reply_to_email',
252
        branchreturnpath => 'return_path_email',
253
        branchurl        => 'url',
254
        issuing          => undef,
255
        branchip         => 'ip',
256
        branchnotes      => 'notes',
257
        marcorgcode      => 'marc_org_code',
258
    };
259
}
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
280
=head3 get_hold_libraries
229
=head3 get_hold_libraries
281
230
282
Return all libraries (including self) that belong to the same hold groups
231
Return all libraries (including self) that belong to the same hold groups
Lines 317-322 sub validate_hold_sibling { Link Here
317
      ->count > 0;
266
      ->count > 0;
318
}
267
}
319
268
269
=head3 public_read_list
270
271
This method returns the list of publicly readable database fields for both API and UI output purposes
272
273
=cut
274
275
sub public_read_list {
276
    return [
277
        'branchcode',     'branchname',     'branchaddress1',
278
        'branchaddress2', 'branchaddress3', 'branchzip',
279
        'branchcity',     'branchstate',    'branchcountry',
280
        'branchfax',      'branchemail',    'branchurl'
281
    ];
282
}
283
284
=head3 to_api_mapping
285
286
This method returns the mapping for representing a Koha::Library object
287
on the API.
288
289
=cut
290
291
sub to_api_mapping {
292
    return {
293
        branchcode       => 'library_id',
294
        branchname       => 'name',
295
        branchaddress1   => 'address1',
296
        branchaddress2   => 'address2',
297
        branchaddress3   => 'address3',
298
        branchzip        => 'postal_code',
299
        branchcity       => 'city',
300
        branchstate      => 'state',
301
        branchcountry    => 'country',
302
        branchphone      => 'phone',
303
        branchfax        => 'fax',
304
        branchemail      => 'email',
305
        branchillemail   => 'illemail',
306
        branchreplyto    => 'reply_to_email',
307
        branchreturnpath => 'return_path_email',
308
        branchurl        => 'url',
309
        issuing          => undef,
310
        branchip         => 'ip',
311
        branchnotes      => 'notes',
312
        marcorgcode      => 'marc_org_code',
313
    };
314
}
315
320
=head2 Internal methods
316
=head2 Internal methods
321
317
322
=head3 _type
318
=head3 _type
(-)a/Koha/Object.pm (-9 / +11 lines)
Lines 24-29 use Carp qw( croak ); Link Here
24
use Mojo::JSON;
24
use Mojo::JSON;
25
use Scalar::Util qw( blessed looks_like_number );
25
use Scalar::Util qw( blessed looks_like_number );
26
use Try::Tiny qw( catch try );
26
use Try::Tiny qw( catch try );
27
use List::MoreUtils qw( any );
27
28
28
use Koha::Database;
29
use Koha::Database;
29
use Koha::Exceptions::Object;
30
use Koha::Exceptions::Object;
Lines 552-557 sub to_api { Link Here
552
    my ( $self, $params ) = @_;
553
    my ( $self, $params ) = @_;
553
    my $json_object = $self->TO_JSON;
554
    my $json_object = $self->TO_JSON;
554
555
556
    # Remove forbidden attributes if required
557
    # FIXME: We should eventually require public_read_list in all objects and drop the conditional here.
558
    if (    $params->{public}
559
        and $self->can('public_read_list') )
560
    {
561
        for my $field ( keys %{$json_object} ) {
562
            delete $json_object->{$field} unless any { $_ eq $field } @{$self->public_read_list};
563
        }
564
    }
565
555
    my $to_api_mapping = $self->to_api_mapping;
566
    my $to_api_mapping = $self->to_api_mapping;
556
567
557
    # Rename attributes if there's a mapping
568
    # Rename attributes if there's a mapping
Lines 573-587 sub to_api { Link Here
573
        }
584
        }
574
    }
585
    }
575
586
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
587
    # Make sure we duplicate the $params variable to avoid
586
    # breaking calls in a loop (Koha::Objects->to_api)
588
    # breaking calls in a loop (Koha::Objects->to_api)
587
    $params    = {%$params};
589
    $params    = {%$params};
(-)a/t/db_dependent/Koha/Object.t (-7 / +34 lines)
Lines 375-383 subtest "to_api() tests" => sub { Link Here
375
375
376
    subtest 'unprivileged request tests' => sub {
376
    subtest 'unprivileged request tests' => sub {
377
377
378
        my @privileged_attrs = @{ Koha::Library->api_privileged_attrs };
378
        my @all_attrs = Koha::Libraries->columns();
379
        my $public_attrs = { map { $_ => 1 } @{ Koha::Library->public_read_list() } };
380
        my $mapping = Koha::Library->to_api_mapping;
379
381
380
        plan tests => scalar @privileged_attrs * 2;
382
        plan tests => scalar @all_attrs * 2;
381
383
382
        # Create a sample library
384
        # Create a sample library
383
        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
385
        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
Lines 385-395 subtest "to_api() tests" => sub { Link Here
385
        my $unprivileged_representation = $library->to_api({ public => 1 });
387
        my $unprivileged_representation = $library->to_api({ public => 1 });
386
        my $privileged_representation   = $library->to_api;
388
        my $privileged_representation   = $library->to_api;
387
389
388
        foreach my $privileged_attr ( @privileged_attrs ) {
390
        foreach my $attr (@all_attrs) {
389
            ok( exists $privileged_representation->{$privileged_attr},
391
            my $mapped = exists $mapping->{$attr} ? $mapping->{$attr} : $attr;
390
                "Attribute $privileged_attr' is present" );
392
            if ( defined($mapped) ) {
391
            ok( !exists $unprivileged_representation->{$privileged_attr},
393
                ok(
392
                "Attribute '$privileged_attr' is not present" );
394
                    exists $privileged_representation->{$mapped},
395
                    "Attribute '$attr' is present when privileged"
396
                );
397
                if ( exists $public_attrs->{$attr} ) {
398
                    ok(
399
                        exists $unprivileged_representation->{$mapped},
400
                        "Attribute '$attr' is present when public"
401
                    );
402
                }
403
                else {
404
                    ok(
405
                        !exists $unprivileged_representation->{$mapped},
406
                        "Attribute '$attr' is not present when public"
407
                    );
408
                }
409
            }
410
            else {
411
                ok(
412
                    !exists $privileged_representation->{$attr},
413
                    "Unmapped attribute '$attr' is not present when privileged"
414
                );
415
                ok(
416
                    !exists $unprivileged_representation->{$attr},
417
                    "Unmapped attribute '$attr' is not present when public"
418
                );
419
            }
393
        }
420
        }
394
    };
421
    };
395
422
(-)a/t/db_dependent/Koha/Objects.t (-11 / +35 lines)
Lines 400-423 subtest "to_api() tests" => sub { Link Here
400
400
401
    subtest 'unprivileged request tests' => sub {
401
    subtest 'unprivileged request tests' => sub {
402
402
403
        my @privileged_attrs = @{ Koha::Library->api_privileged_attrs };
403
        my @all_attrs = Koha::Libraries->columns();
404
        my $public_attrs = { map { $_ => 1 } @{ Koha::Library->public_read_list() } };
405
        my $mapping = Koha::Library->to_api_mapping;
404
406
405
        # Create sample libraries
407
        # Create sample libraries
406
        my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
408
        my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
407
        my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
409
        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
        my $libraries = Koha::Libraries->search(
410
            {
411
            {
411
                branchcode => {
412
                branchcode => {
412
                    '-in' => [
413
                    '-in' => [
413
                        $library_1->branchcode, $library_2->branchcode,
414
                        $library_1->branchcode, $library_2->branchcode
414
                        $library_3->branchcode
415
                    ]
415
                    ]
416
                }
416
                }
417
            }
417
            }
418
        );
418
        );
419
419
420
        plan tests => scalar @privileged_attrs * 2 * $libraries->count;
420
        plan tests => scalar @all_attrs * 2 * $libraries->count;
421
421
422
        my $libraries_unprivileged_representation = $libraries->to_api({ public => 1 });
422
        my $libraries_unprivileged_representation = $libraries->to_api({ public => 1 });
423
        my $libraries_privileged_representation   = $libraries->to_api();
423
        my $libraries_privileged_representation   = $libraries->to_api();
Lines 425-435 subtest "to_api() tests" => sub { Link Here
425
        for (my $i = 0; $i < $libraries->count; $i++) {
425
        for (my $i = 0; $i < $libraries->count; $i++) {
426
            my $privileged_representation   = $libraries_privileged_representation->[$i];
426
            my $privileged_representation   = $libraries_privileged_representation->[$i];
427
            my $unprivileged_representation = $libraries_unprivileged_representation->[$i];
427
            my $unprivileged_representation = $libraries_unprivileged_representation->[$i];
428
            foreach my $privileged_attr ( @privileged_attrs ) {
428
            foreach my $attr (@all_attrs) {
429
                ok( exists $privileged_representation->{$privileged_attr},
429
                my $mapped = exists $mapping->{$attr} ? $mapping->{$attr} : $attr;
430
                    "Attribute $privileged_attr' is present" );
430
                if ( defined($mapped) ) {
431
                ok( !exists $unprivileged_representation->{$privileged_attr},
431
                    ok(
432
                    "Attribute '$privileged_attr' is not present" );
432
                        exists $privileged_representation->{$mapped},
433
                        "Attribute '$attr' is present when privileged"
434
                    );
435
                    if ( exists $public_attrs->{$attr} ) {
436
                        ok(
437
                            exists $unprivileged_representation->{$mapped},
438
                            "Attribute '$attr' is present when public"
439
                        );
440
                    }
441
                    else {
442
                        ok(
443
                            !exists $unprivileged_representation->{$mapped},
444
                            "Attribute '$attr' is not present when public"
445
                        );
446
                    }
447
                }
448
                else {
449
                    ok(
450
                        !exists $privileged_representation->{$attr},
451
                        "Unmapped attribute '$attr' is not present when privileged"
452
                    );
453
                    ok(
454
                        !exists $unprivileged_representation->{$attr},
455
                        "Unmapped attribute '$attr' is not present when public"
456
                    );
457
                }
433
            }
458
            }
434
        }
459
        }
435
    };
460
    };
436
- 

Return to bug 28948