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 2026-2031 sub get_extended_attribute { Link Here
2026
    return $attribute->next;
2026
    return $attribute->next;
2027
}
2027
}
2028
2028
2029
=head3 is_accessible
2030
2031
    if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... }
2032
2033
This overloaded method validates wether the current I<Koha::Patron> object can be accessed
2034
by the logged in user.
2035
2036
Returns 0 if the I<user> parameter is missing.
2037
2038
=cut
2039
2040
sub is_accessible {
2041
    my ( $self, $params ) = @_;
2042
2043
    # FIXME? It felt tempting to return 0 instead
2044
    # but it would mean needing to explicitly add the 'user'
2045
    # param in all tests...
2046
    return 1
2047
      unless $params->{user};
2048
2049
    my $consumer = $params->{user};
2050
    return $consumer->can_see_patron_infos($self);
2051
}
2052
2029
=head3 to_api
2053
=head3 to_api
2030
2054
2031
    my $json = $patron->to_api;
2055
    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 561-568 subtest "to_api() tests" => sub { Link Here
561
562
562
        t::lib::Mocks::mock_userenv( { patron => $patron } );
563
        t::lib::Mocks::mock_userenv( { patron => $patron } );
563
564
564
        is( ref($patron_1->to_api), 'HASH', 'Returns the object hash' );
565
        is( ref($patron_1->to_api({ user => $patron })), 'HASH', 'Returns the object hash' );
565
        is( $patron_2->to_api, undef, 'Not accessible, returns undef' );
566
        is( $patron_2->to_api({ user => $patron }), undef, 'Not accessible, returns undef' );
566
567
567
        $schema->storage->txn_rollback;
568
        $schema->storage->txn_rollback;
568
    };
569
    };
Lines 1144-1150 subtest 'messages() and add_message() tests' => sub { Link Here
1144
    $schema->storage->txn_rollback;
1145
    $schema->storage->txn_rollback;
1145
};
1146
};
1146
1147
1147
subtest 'accessible() tests' => sub {
1148
subtest 'is_accessible() tests' => sub {
1148
1149
1149
    plan tests => 2;
1150
    plan tests => 2;
1150
1151
Lines 1172-1179 subtest 'accessible() tests' => sub { Link Here
1172
1173
1173
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1174
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1174
1175
1175
    ok( $patron_1->accessible,  'Has access to the patron' );
1176
    ok( $patron_1->is_accessible({ user => $patron }),  'Has access to the patron' );
1176
    ok( !$patron_2->accessible, 'Does not have access to the patron' );
1177
    ok( !$patron_2->is_accessible({ user => $patron }), 'Does not have access to the patron' );
1177
1178
1178
    $schema->storage->txn_rollback;
1179
    $schema->storage->txn_rollback;
1179
};
1180
};
1180
- 

Return to bug 29523