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

(-)a/Koha/Object.pm (-4 / +34 lines)
Lines 405-411 sub TO_JSON { Link Here
405
        elsif ( _numeric_column_type( $columns_info->{$col}->{data_type} )
405
        elsif ( _numeric_column_type( $columns_info->{$col}->{data_type} )
406
            and looks_like_number( $unblessed->{$col} )
406
            and looks_like_number( $unblessed->{$col} )
407
        ) {
407
        ) {
408
409
            # TODO: Remove once the solution for
408
            # TODO: Remove once the solution for
410
            # https://github.com/perl5-dbi/DBD-mysql/issues/212
409
            # https://github.com/perl5-dbi/DBD-mysql/issues/212
411
            # is ported to whatever distro we support by that time
410
            # is ported to whatever distro we support by that time
Lines 415-421 sub TO_JSON { Link Here
415
        elsif ( _decimal_column_type( $columns_info->{$col}->{data_type} )
414
        elsif ( _decimal_column_type( $columns_info->{$col}->{data_type} )
416
            and looks_like_number( $unblessed->{$col} )
415
            and looks_like_number( $unblessed->{$col} )
417
        ) {
416
        ) {
418
419
            # TODO: Remove once the solution for
417
            # TODO: Remove once the solution for
420
            # https://github.com/perl5-dbi/DBD-mysql/issues/212
418
            # https://github.com/perl5-dbi/DBD-mysql/issues/212
421
            # is ported to whatever distro we support by that time
419
            # is ported to whatever distro we support by that time
Lines 552-559 Returns a representation of the object, suitable for API output. Link Here
552
sub to_api {
550
sub to_api {
553
    my ( $self, $params ) = @_;
551
    my ( $self, $params ) = @_;
554
552
555
    return unless $self->is_accessible($params);
556
557
    my $json_object = $self->TO_JSON;
553
    my $json_object = $self->TO_JSON;
558
554
559
    # Make sure we duplicate the $params variable to avoid
555
    # Make sure we duplicate the $params variable to avoid
Lines 585-590 sub to_api { Link Here
585
        }
581
        }
586
    }
582
    }
587
583
584
    # Remove forbidden attributes if required (including their coded values)
585
    if ( !$self->is_accessible($params) ) {
586
        for my $field ( keys %{$json_object} ) {
587
            unless ( any { $_ eq $field } @{ $self->unredact_list } ) {
588
                $json_object->{$field} = undef;
589
            }
590
        }
591
592
        if ($strings) {
593
            foreach my $field ( keys %{$string_map} ) {
594
                unless ( any { $_ eq $field } @{ $self->unredact_list } ) {
595
                    $string_map->{$field} = undef;
596
                }
597
            }
598
        }
599
    }
600
588
    my $to_api_mapping = $self->to_api_mapping;
601
    my $to_api_mapping = $self->to_api_mapping;
589
602
590
    # Rename attributes and coded values if there's a mapping
603
    # Rename attributes and coded values if there's a mapping
Lines 717-722 sub public_read_list Link Here
717
    return [];
730
    return [];
718
}
731
}
719
732
733
=head3 unredact_list
734
735
    my @unredact_list = @{$object->unredact_list};
736
737
Generic method that returns the list of database columns that are allowed to
738
be passed to render objects on the API when the user making the request should
739
not ordinarily have unrestricted access to the data (as returned by the is_accesible method).
740
741
Note: this only returns an empty I<arrayref>. Each class should have its
742
own implementation.
743
744
=cut
745
746
sub unredact_list {
747
    return [];
748
}
749
720
=head3 from_api_mapping
750
=head3 from_api_mapping
721
751
722
    my $mapping = $object->from_api_mapping;
752
    my $mapping = $object->from_api_mapping;
(-)a/Koha/Patron.pm (-1 / +12 lines)
Lines 2279-2285 sub set_default_messaging_preferences { Link Here
2279
2279
2280
    if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... }
2280
    if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... }
2281
2281
2282
This overloaded method validates wether the current I<Koha::Patron> object can be accessed
2282
This overloaded method validates whether the current I<Koha::Patron> object can be accessed
2283
by the logged in user.
2283
by the logged in user.
2284
2284
2285
Returns 0 if the I<user> parameter is missing.
2285
Returns 0 if the I<user> parameter is missing.
Lines 2297-2302 sub is_accessible { Link Here
2297
    return $consumer->can_see_patron_infos($self);
2297
    return $consumer->can_see_patron_infos($self);
2298
}
2298
}
2299
2299
2300
=head3 unredact_list
2301
2302
This method returns the list of database fields that should be visible, even for restricted users,
2303
for both API and UI output purposes
2304
2305
=cut
2306
2307
sub unredact_list {
2308
    return ['branchcode'];
2309
}
2310
2300
=head3 to_api
2311
=head3 to_api
2301
2312
2302
    my $json = $patron->to_api;
2313
    my $json = $patron->to_api;
(-)a/t/db_dependent/Koha/Object.t (-4 / +5 lines)
Lines 554-560 subtest "to_api() tests" => sub { Link Here
554
            }
554
            }
555
        );
555
        );
556
556
557
558
        my $patron_1 = $builder->build_object(
557
        my $patron_1 = $builder->build_object(
559
            { class => 'Koha::Patrons', value => { branchcode => $library_1->id } }
558
            { class => 'Koha::Patrons', value => { branchcode => $library_1->id } }
560
        );
559
        );
Lines 564-571 subtest "to_api() tests" => sub { Link Here
564
563
565
        t::lib::Mocks::mock_userenv( { patron => $patron } );
564
        t::lib::Mocks::mock_userenv( { patron => $patron } );
566
565
567
        is( ref($patron_1->to_api({ user => $patron })), 'HASH', 'Returns the object hash' );
566
        is(
568
        is( $patron_2->to_api({ user => $patron }), undef, 'Not accessible, returns undef' );
567
            $patron_1->to_api( { user => $patron } )->{firstname}, $patron_1->firstname,
568
            'Returns unredacted object hash'
569
        );
570
        is( $patron_2->to_api( { user => $patron } )->{firstname}, undef, 'Returns redacted object hash' );
569
571
570
        $schema->storage->txn_rollback;
572
        $schema->storage->txn_rollback;
571
    };
573
    };
572
- 

Return to bug 29523