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

(-)a/Koha/Exceptions/Authorization.pm (+5 lines)
Lines 12-17 use Exception::Class ( Link Here
12
        description => 'Unauthorized',
12
        description => 'Unauthorized',
13
        fields => ['required_permissions']
13
        fields => ['required_permissions']
14
    },
14
    },
15
    'Koha::Exceptions::Authorization::Embedded' => {
16
        isa => 'Koha::Exceptions::Authorization',
17
        description => 'Embedded documents unauthorized',
18
        fields => ['embedded_item']
19
    },
15
20
16
);
21
);
17
22
(-)a/Koha/Object.pm (-2 / +33 lines)
Lines 380-391 Returns a representation of the object, suitable for API output. Link Here
380
=cut
380
=cut
381
381
382
sub to_api {
382
sub to_api {
383
    my ( $self ) = @_;
383
    my ( $self, $embeds ) = @_;
384
    my $json_object = $self->TO_JSON;
384
    my $json_object = $self->TO_JSON;
385
385
386
    # Rename attributes if there's a mapping
386
    # Rename attributes if there's a mapping
387
    if ( $self->can('to_api_mapping') ) {
387
    if ( $self->can('to_api_mapping') ) {
388
        foreach my $column ( keys %{$self->to_api_mapping} ) {
388
        foreach my $column ( keys %{ $self->to_api_mapping } ) {
389
            my $mapped_column = $self->to_api_mapping->{$column};
389
            my $mapped_column = $self->to_api_mapping->{$column};
390
            if ( exists $json_object->{$column}
390
            if ( exists $json_object->{$column}
391
                && defined $mapped_column )
391
                && defined $mapped_column )
Lines 402-410 sub to_api { Link Here
402
        }
402
        }
403
    }
403
    }
404
404
405
    if ($embeds) {
406
        foreach my $embed (@$embeds) {
407
            my ( $curr, $next ) = split /\s*\.\s*/, $embed, 2;
408
            my @nxembeds;
409
410
            @nxembeds = ($next) if $next;
411
412
            my $children = $self->$curr;
413
            if ( ref $children eq 'ARRAY' ) {
414
                my @list;
415
                my $pos = 0;
416
                foreach my $child (@$children) {
417
                    my $res = $child->to_api( \@nxembeds );
418
                    $res = { $json_object->{$curr}->[$pos], $res }
419
                      if defined $json_object->{$curr}
420
                      && defined $json_object->{$curr}->[$pos];
421
                    push @list, $res;
422
                    $pos++;
423
                }
424
                $json_object->{$curr} = \@list;
425
            }
426
            else {
427
                my $res = $children->to_api( \@nxembeds );
428
                $res = { $json_object->{$curr}, $res }
429
                  if defined $json_object->{$curr};
430
                $json_object->{$curr} = $res;
431
            }
432
        }
433
    }
434
405
    return $json_object;
435
    return $json_object;
406
}
436
}
407
437
438
408
=head3 $object->unblessed_all_relateds
439
=head3 $object->unblessed_all_relateds
409
440
410
my $everything_into_one_hashref = $object->unblessed_all_relateds
441
my $everything_into_one_hashref = $object->unblessed_all_relateds
(-)a/Koha/Objects.pm (-2 / +2 lines)
Lines 315-323 Returns a representation of the objects, suitable for API output . Link Here
315
=cut
315
=cut
316
316
317
sub to_api {
317
sub to_api {
318
    my ($self) = @_;
318
    my ($self, $embeds) = @_;
319
319
320
    return [ map { $_->to_api } $self->as_list ];
320
    return [ map { $_->to_api($embeds) } $self->as_list ];
321
}
321
}
322
322
323
=head3 Koha::Objects->_wrap
323
=head3 Koha::Objects->_wrap
(-)a/Koha/REST/Plugin/Objects.pm (-3 / +4 lines)
Lines 52-58 sub register { Link Here
52
52
53
            my $args = $c->validation->output;
53
            my $args = $c->validation->output;
54
            my $attributes = {};
54
            my $attributes = {};
55
56
            # Extract reserved params
55
            # Extract reserved params
57
            my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
56
            my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($args);
58
57
Lines 82-87 sub register { Link Here
82
                $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
81
                $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
83
            }
82
            }
84
83
84
            my $embeds = $c->stash('koha.embed');
85
85
            # Perform search
86
            # Perform search
86
            my $objects = $objects_set->search( $filtered_params, $attributes );
87
            my $objects = $objects_set->search( $filtered_params, $attributes );
87
88
Lines 94-101 sub register { Link Here
94
95
95
            my @objects_list = map {
96
            my @objects_list = map {
96
                ( defined $to_api )
97
                ( defined $to_api )
97
                  ? $to_api->( $_->TO_JSON )
98
                ? $to_api->( $_->TO_JSON, $embeds )
98
                  : $_->TO_JSON
99
                : $_->to_api($embeds);
99
            } $objects->as_list;
100
            } $objects->as_list;
100
101
101
            return \@objects_list;
102
            return \@objects_list;
(-)a/Koha/REST/V1/Auth.pm (+21 lines)
Lines 137-142 sub authenticate_api_request { Link Here
137
    my $user;
137
    my $user;
138
138
139
    my $spec = $c->match->endpoint->pattern->defaults->{'openapi.op_spec'};
139
    my $spec = $c->match->endpoint->pattern->defaults->{'openapi.op_spec'};
140
141
    my $embed_header = $c->req->headers->header('x-koha-embed');
142
143
    if($embed_header) {
144
        my $embed = $spec->{'x-koha-embed'};
145
146
        Koha::Exceptions::Authorization::Embedded->throw(
147
            error => 'No embedded items are authorised for this endpoint. Remove your x-koha-embed headers.'
148
        ) unless $embed;
149
150
        my @to_embed;
151
        foreach my $eh (split /\s*,\s*/, $embed_header) {
152
            my $matches = grep {lc $_ eq lc $eh} @$embed;
153
            Koha::Exceptions::Authorization::Embedded->throw(
154
                error => 'Embeding '.$eh. ' is not authorised. Check your x-koha-embed headers or remove it.'
155
            ) unless $matches;
156
            push @to_embed, $eh;
157
        }
158
        $c->stash('koha.embed' => \@to_embed);
159
    }
160
140
    my $authorization = $spec->{'x-koha-authorization'};
161
    my $authorization = $spec->{'x-koha-authorization'};
141
162
142
    my $authorization_header = $c->req->headers->authorization;
163
    my $authorization_header = $c->req->headers->authorization;
(-)a/t/db_dependent/Koha/Object.t (-2 / +39 lines)
Lines 215-221 subtest 'TO_JSON tests' => sub { Link Here
215
215
216
subtest "to_api() tests" => sub {
216
subtest "to_api() tests" => sub {
217
217
218
    plan tests => 11;
218
    plan tests => 18;
219
219
220
    $schema->storage->txn_begin;
220
    $schema->storage->txn_begin;
221
221
Lines 262-267 subtest "to_api() tests" => sub { Link Here
262
    my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
262
    my $illrequest = $builder->build_object({ class => 'Koha::Illrequests' });
263
    is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' );
263
    is_deeply( $illrequest->to_api, $illrequest->TO_JSON, 'If no to_api_method present, return TO_JSON' );
264
264
265
    my $item_class = Test::MockModule->new('Koha::Item');
266
    $item_class->mock( 'to_api_mapping',
267
        sub {
268
            return {
269
                itemnumber       => 'item_id'
270
            };
271
        }
272
    );
273
274
    my $hold_class = Test::MockModule->new('Koha::Hold');
275
    $hold_class->mock( 'to_api_mapping',
276
        sub {
277
            return {
278
                reserve_id       => 'hold_id'
279
            };
280
        }
281
    );
282
283
    my $biblio = $builder->build_sample_biblio();
284
    my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
285
    my $hold = $builder->build_object({ class => 'Koha::Holds', value => { itemnumber => $item->itemnumber } });
286
287
    my @embeds = ('items');
288
289
    my $biblio_api = $biblio->to_api(\@embeds);
290
291
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
292
    is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item matches');
293
    ok(!exists $biblio_api->{items}->[0]->{holds}, 'No holds info should be embedded yet');
294
295
    @embeds = ('items.holds');
296
    $biblio_api = $biblio->to_api(\@embeds);
297
298
    ok(exists $biblio_api->{items}, 'Items where embedded in biblio results');
299
    is($biblio_api->{items}->[0]->{item_id}, $item->itemnumber, 'Item still matches');
300
    ok(exists $biblio_api->{items}->[0]->{holds}, 'Holds info should be embedded');
301
    is($biblio_api->{items}->[0]->{holds}->[0]->{hold_id}, $hold->reserve_id, 'Hold matches');
302
265
    $schema->storage->txn_rollback;
303
    $schema->storage->txn_rollback;
266
};
304
};
267
305
268
- 

Return to bug 24228