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 988-1013 sub _handle_to_api_child { Link Here
988
    return $res;
988
    return $res;
989
}
989
}
990
990
991
=head3 accessible
991
=head3 is_accessible
992
992
993
    if ( $object->accessible ) { ... }
993
    if ( $object->is_accessible ) { ... }
994
994
995
Whether the object should be accessible in the current context (requesting user).
995
Stub method that is expected to be overloaded (if required) by implementing classes.
996
It relies on the plural class properly implementing the I<search_limited> method.
997
996
998
=cut
997
=cut
999
998
1000
sub accessible {
999
sub is_accessible {
1001
    my ($self) = @_;
1000
    my ($self) = @_;
1002
1001
1003
    return $self->_get_objects_class->search_limited(
1002
    return 1;
1004
        {
1005
            map { $_ => $self->$_ }
1006
              $self->_result->result_source->primary_columns
1007
        }
1008
      )->count > 0
1009
      ? 1
1010
      : 0;
1011
}
1003
}
1012
1004
1013
sub DESTROY { }
1005
sub DESTROY { }
(-)a/Koha/Patron.pm (+24 lines)
Lines 2067-2072 sub get_extended_attribute { Link Here
2067
    return $attribute->next;
2067
    return $attribute->next;
2068
}
2068
}
2069
2069
2070
=head3 is_accessible
2071
2072
    if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... }
2073
2074
This overloaded method validates wether the current I<Koha::Patron> object can be accessed
2075
by the logged in user.
2076
2077
Returns 0 if the I<user> parameter is missing.
2078
2079
=cut
2080
2081
sub is_accessible {
2082
    my ( $self, $params ) = @_;
2083
2084
    # FIXME? It felt tempting to return 0 instead
2085
    # but it would mean needing to explicitly add the 'user'
2086
    # param in all tests...
2087
    return 1
2088
      unless $params->{user};
2089
2090
    my $consumer = $params->{user};
2091
    return $consumer->can_see_patron_infos($self);
2092
}
2093
2070
=head3 to_api
2094
=head3 to_api
2071
2095
2072
    my $json = $patron->to_api;
2096
    my $json = $patron->to_api;
(-)a/Koha/REST/Plugin/Objects.pm (-1 / +5 lines)
Lines 285-295 Returns the API representation of the passed resultset. Link Here
285
            my $embed   = $c->stash('koha.embed');
285
            my $embed   = $c->stash('koha.embed');
286
            my $strings = $c->stash('koha.strings');
286
            my $strings = $c->stash('koha.strings');
287
287
288
            # Grab user
289
            my $user = $c->stash('koha.user');
290
288
            return $object->to_api(
291
            return $object->to_api(
289
                {
292
                {
290
                    embed   => $embed,
293
                    embed   => $embed,
291
                    public  => $public,
294
                    public  => $public,
292
                    strings => $strings
295
                    strings => $strings,
296
                    user    => $user
293
                }
297
                }
294
            );
298
            );
295
        }
299
        }
(-)a/t/db_dependent/Koha/Object.t (-8 / +8 lines)
Lines 386-399 subtest "to_api() tests" => sub { Link Here
386
386
387
    my $patron_api = $patron->to_api(
387
    my $patron_api = $patron->to_api(
388
        {
388
        {
389
            embed => { holds_count => { is_count => 1 } }
389
            embed => { holds_count => { is_count => 1 } },
390
            user  => $patron
390
        }
391
        }
391
    );
392
    );
392
    is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
393
    is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
393
394
394
    throws_ok
395
    throws_ok
395
        {
396
        {
396
            $patron->to_api({ embed => { holds_count => {} } });
397
            $patron->to_api({ embed => { holds_count => {} }, user => $patron });
397
        }
398
        }
398
        'Koha::Exceptions::Object::MethodNotCoveredByTests',
399
        'Koha::Exceptions::Object::MethodNotCoveredByTests',
399
        'Unknown method exception thrown if is_count not specified';
400
        'Unknown method exception thrown if is_count not specified';
Lines 565-572 subtest "to_api() tests" => sub { Link Here
565
566
566
        t::lib::Mocks::mock_userenv( { patron => $patron } );
567
        t::lib::Mocks::mock_userenv( { patron => $patron } );
567
568
568
        is( ref($patron_1->to_api), 'HASH', 'Returns the object hash' );
569
        is( ref($patron_1->to_api({ user => $patron })), 'HASH', 'Returns the object hash' );
569
        is( $patron_2->to_api, undef, 'Not accessible, returns undef' );
570
        is( $patron_2->to_api({ user => $patron }), undef, 'Not accessible, returns undef' );
570
571
571
        $schema->storage->txn_rollback;
572
        $schema->storage->txn_rollback;
572
    };
573
    };
Lines 1148-1154 subtest 'messages() and add_message() tests' => sub { Link Here
1148
    $schema->storage->txn_rollback;
1149
    $schema->storage->txn_rollback;
1149
};
1150
};
1150
1151
1151
subtest 'accessible() tests' => sub {
1152
subtest 'is_accessible() tests' => sub {
1152
1153
1153
    plan tests => 2;
1154
    plan tests => 2;
1154
1155
Lines 1176-1183 subtest 'accessible() tests' => sub { Link Here
1176
1177
1177
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1178
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1178
1179
1179
    ok( $patron_1->accessible,  'Has access to the patron' );
1180
    ok( $patron_1->is_accessible({ user => $patron }),  'Has access to the patron' );
1180
    ok( !$patron_2->accessible, 'Does not have access to the patron' );
1181
    ok( !$patron_2->is_accessible({ user => $patron }), 'Does not have access to the patron' );
1181
1182
1182
    $schema->storage->txn_rollback;
1183
    $schema->storage->txn_rollback;
1183
};
1184
};
1184
- 

Return to bug 29523