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 993-1018 sub _handle_to_api_child { Link Here
993
    return $res;
993
    return $res;
994
}
994
}
995
995
996
=head3 accessible
996
=head3 is_accessible
997
997
998
    if ( $object->accessible ) { ... }
998
    if ( $object->is_accessible ) { ... }
999
999
1000
Whether the object should be accessible in the current context (requesting user).
1000
Stub method that is expected to be overloaded (if required) by implementing classes.
1001
It relies on the plural class properly implementing the I<search_limited> method.
1002
1001
1003
=cut
1002
=cut
1004
1003
1005
sub accessible {
1004
sub is_accessible {
1006
    my ($self) = @_;
1005
    my ($self) = @_;
1007
1006
1008
    return $self->_get_objects_class->search_limited(
1007
    return 1;
1009
        {
1010
            map { $_ => $self->$_ }
1011
              $self->_result->result_source->primary_columns
1012
        }
1013
      )->count > 0
1014
      ? 1
1015
      : 0;
1016
}
1008
}
1017
1009
1018
sub DESTROY { }
1010
sub DESTROY { }
(-)a/Koha/Patron.pm (+24 lines)
Lines 2274-2279 sub set_default_messaging_preferences { Link Here
2274
    return $self;
2274
    return $self;
2275
}
2275
}
2276
2276
2277
=head3 is_accessible
2278
2279
    if ( $patron->is_accessible({ user => $logged_in_user }) ) { ... }
2280
2281
This overloaded method validates wether the current I<Koha::Patron> object can be accessed
2282
by the logged in user.
2283
2284
Returns 0 if the I<user> parameter is missing.
2285
2286
=cut
2287
2288
sub is_accessible {
2289
    my ( $self, $params ) = @_;
2290
2291
    # FIXME? It felt tempting to return 0 instead
2292
    # but it would mean needing to explicitly add the 'user'
2293
    # param in all tests...
2294
    return 1
2295
      unless $params->{user};
2296
2297
    my $consumer = $params->{user};
2298
    return $consumer->can_see_patron_infos($self);
2299
}
2300
2277
=head3 to_api
2301
=head3 to_api
2278
2302
2279
    my $json = $patron->to_api;
2303
    my $json = $patron->to_api;
(-)a/Koha/REST/Plugin/Objects.pm (-1 / +5 lines)
Lines 316-326 Returns the API representation of the passed resultset. Link Here
316
            my $embed   = $c->stash('koha.embed');
316
            my $embed   = $c->stash('koha.embed');
317
            my $strings = $c->stash('koha.strings');
317
            my $strings = $c->stash('koha.strings');
318
318
319
            # Grab user
320
            my $user = $c->stash('koha.user');
321
319
            return $object->to_api(
322
            return $object->to_api(
320
                {
323
                {
321
                    embed   => $embed,
324
                    embed   => $embed,
322
                    public  => $public,
325
                    public  => $public,
323
                    strings => $strings
326
                    strings => $strings,
327
                    user    => $user
324
                }
328
                }
325
            );
329
            );
326
        }
330
        }
(-)a/t/db_dependent/Koha/Object.t (-12 / +11 lines)
Lines 385-400 subtest "to_api() tests" => sub { Link Here
385
385
386
    my $patron_api = $patron->to_api(
386
    my $patron_api = $patron->to_api(
387
        {
387
        {
388
            embed => { holds_count => { is_count => 1 } }
388
            embed => { holds_count => { is_count => 1 } },
389
            user  => $patron
389
        }
390
        }
390
    );
391
    );
391
    is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
392
    is( $patron_api->{holds_count}, $patron->holds->count, 'Count embeds are supported and work as expected' );
392
393
393
    throws_ok
394
    throws_ok {
394
        {
395
        $patron->to_api( { embed => { holds_count => {} }, user => $patron } );
395
            $patron->to_api({ embed => { holds_count => {} } });
396
    }
396
        }
397
    'Koha::Exceptions::Object::MethodNotCoveredByTests',
397
        'Koha::Exceptions::Object::MethodNotCoveredByTests',
398
        'Unknown method exception thrown if is_count not specified';
398
        'Unknown method exception thrown if is_count not specified';
399
399
400
    subtest 'unprivileged request tests' => sub {
400
    subtest 'unprivileged request tests' => sub {
Lines 564-571 subtest "to_api() tests" => sub { Link Here
564
564
565
        t::lib::Mocks::mock_userenv( { patron => $patron } );
565
        t::lib::Mocks::mock_userenv( { patron => $patron } );
566
566
567
        is( ref($patron_1->to_api), 'HASH', 'Returns the object hash' );
567
        is( ref($patron_1->to_api({ user => $patron })), 'HASH', 'Returns the object hash' );
568
        is( $patron_2->to_api, undef, 'Not accessible, returns undef' );
568
        is( $patron_2->to_api({ user => $patron }), undef, 'Not accessible, returns undef' );
569
569
570
        $schema->storage->txn_rollback;
570
        $schema->storage->txn_rollback;
571
    };
571
    };
Lines 1156-1162 subtest 'messages() and add_message() tests' => sub { Link Here
1156
    $schema->storage->txn_rollback;
1156
    $schema->storage->txn_rollback;
1157
};
1157
};
1158
1158
1159
subtest 'accessible() tests' => sub {
1159
subtest 'is_accessible() tests' => sub {
1160
1160
1161
    plan tests => 2;
1161
    plan tests => 2;
1162
1162
Lines 1184-1191 subtest 'accessible() tests' => sub { Link Here
1184
1184
1185
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1185
    t::lib::Mocks::mock_userenv( { patron => $patron } );
1186
1186
1187
    ok( $patron_1->accessible,  'Has access to the patron' );
1187
    ok( $patron_1->is_accessible( { user  => $patron } ), 'Has access to the patron' );
1188
    ok( !$patron_2->accessible, 'Does not have access to the patron' );
1188
    ok( !$patron_2->is_accessible( { user => $patron } ), 'Does not have access to the patron' );
1189
1189
1190
    $schema->storage->txn_rollback;
1190
    $schema->storage->txn_rollback;
1191
};
1191
};
1192
- 

Return to bug 29523