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

(-)a/Koha/Object.pm (-14 / +6 lines)
Lines 552-558 Returns a representation of the object, suitable for API output. Link Here
552
sub to_api {
552
sub to_api {
553
    my ( $self, $params ) = @_;
553
    my ( $self, $params ) = @_;
554
554
555
    return unless $self->accessible;
555
    return unless $self->is_accessible($params);
556
556
557
    my $json_object = $self->TO_JSON;
557
    my $json_object = $self->TO_JSON;
558
558
Lines 938-963 sub _handle_to_api_child { Link Here
938
    return $res;
938
    return $res;
939
}
939
}
940
940
941
=head3 accessible
941
=head3 is_accessible
942
942
943
    if ( $object->accessible ) { ... }
943
    if ( $object->is_accessible ) { ... }
944
944
945
Whether the object should be accessible in the current context (requesting user).
945
Stub method that is expected to be overloaded (if required) by implementing classes.
946
It relies on the plural class properly implementing the I<search_limited> method.
947
946
948
=cut
947
=cut
949
948
950
sub accessible {
949
sub is_accessible {
951
    my ($self) = @_;
950
    my ($self) = @_;
952
951
953
    return $self->_get_objects_class->search_limited(
952
    return 1;
954
        {
955
            map { $_ => $self->$_ }
956
              $self->_result->result_source->primary_columns
957
        }
958
      )->count > 0
959
      ? 1
960
      : 0;
961
}
953
}
962
954
963
sub DESTROY { }
955
sub DESTROY { }
(-)a/Koha/Patron.pm (+24 lines)
Lines 1868-1873 sub get_extended_attribute { Link Here
1868
    return $attribute->next;
1868
    return $attribute->next;
1869
}
1869
}
1870
1870
1871
=head3 is_accessible
1872
1873
    if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... }
1874
1875
This overloaded method validates wether the current I<Koha::Patron> object can be accessed
1876
by the logged in user.
1877
1878
Returns 0 if the I<user> parameter is missing.
1879
1880
=cut
1881
1882
sub is_accessible {
1883
    my ( $self, $params ) = @_;
1884
1885
    # FIXME? It felt tempting to return 0 instead
1886
    # but it would mean needing to explicitly add the 'user'
1887
    # param in all tests...
1888
    return 1
1889
      unless $params->{user};
1890
1891
    my $consumer = $params->{user};
1892
    return $consumer->can_see_patron_infos($self);
1893
}
1894
1871
=head3 to_api
1895
=head3 to_api
1872
1896
1873
    my $json = $patron->to_api;
1897
    my $json = $patron->to_api;
(-)a/Koha/REST/Plugin/Objects.pm (-2 / +2 lines)
Lines 66-72 the requested object. It passes through any embeds if specified. Link Here
66
66
67
            return unless $object;
67
            return unless $object;
68
68
69
            return $object->to_api({ embed => $embed });
69
            return $object->to_api({ embed => $embed, user => $c->stash('koha.user') });
70
        }
70
        }
71
    );
71
    );
72
72
Lines 202-208 shouldn't be called twice in it. Link Here
202
                }
202
                }
203
            );
203
            );
204
204
205
            return $objects->to_api({ embed => $embed, public => $is_public });
205
            return $objects->to_api({ embed => $embed, public => $is_public, user => $c->stash('koha.user') });
206
        }
206
        }
207
    );
207
    );
208
}
208
}
(-)a/t/db_dependent/Koha/Object.t (-8 / +8 lines)
Lines 361-374 subtest "to_api() tests" => sub { Link Here
361
361
362
    my $patron_api = $patron->to_api(
362
    my $patron_api = $patron->to_api(
363
        {
363
        {
364
            embed => { holds_count => { is_count => 1 } }
364
            embed => { holds_count => { is_count => 1 } },
365
            user  => $patron
365
        }
366
        }
366
    );
367
    );
367
    is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
368
    is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
368
369
369
    throws_ok
370
    throws_ok
370
        {
371
        {
371
            $patron->to_api({ embed => { holds_count => {} } });
372
            $patron->to_api({ embed => { holds_count => {} }, user => $patron });
372
        }
373
        }
373
        'Koha::Exceptions::Object::MethodNotCoveredByTests',
374
        'Koha::Exceptions::Object::MethodNotCoveredByTests',
374
        'Unknown method exception thrown if is_count not specified';
375
        'Unknown method exception thrown if is_count not specified';
Lines 448-455 subtest "to_api() tests" => sub { Link Here
448
449
449
        t::lib::Mocks::mock_userenv( { patron => $patron } );
450
        t::lib::Mocks::mock_userenv( { patron => $patron } );
450
451
451
        is( ref($patron_1->to_api), 'HASH', 'Returns the object hash' );
452
        is( ref($patron_1->to_api({ user => $patron })), 'HASH', 'Returns the object hash' );
452
        is( $patron_2->to_api, undef, 'Not accessible, returns undef' );
453
        is( $patron_2->to_api({ user => $patron }), undef, 'Not accessible, returns undef' );
453
454
454
        $schema->storage->txn_rollback;
455
        $schema->storage->txn_rollback;
455
    };
456
    };
Lines 1031-1037 subtest 'messages() and add_message() tests' => sub { Link Here
1031
    $schema->storage->txn_rollback;
1032
    $schema->storage->txn_rollback;
1032
};
1033
};
1033
1034
1034
subtest 'accessible() tests' => sub {
1035
subtest 'is_accessible() tests' => sub {
1035
1036
1036
    plan tests => 2;
1037
    plan tests => 2;
1037
1038
Lines 1059-1066 subtest 'accessible() tests' => sub { Link Here
1059
1060
1060
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1061
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1061
1062
1062
    ok( $patron_1->accessible,  'Has access to the patron' );
1063
    ok( $patron_1->is_accessible({ user => $patron }),  'Has access to the patron' );
1063
    ok( !$patron_2->accessible, 'Does not have access to the patron' );
1064
    ok( !$patron_2->is_accessible({ user => $patron }), 'Does not have access to the patron' );
1064
1065
1065
    $schema->storage->txn_rollback;
1066
    $schema->storage->txn_rollback;
1066
};
1067
};
1067
- 

Return to bug 29523