Lines 18-23
package Koha::REST::Plugin::Query;
Link Here
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Mojo::Base 'Mojolicious::Plugin'; |
20 |
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
21 |
use List::MoreUtils qw(any); |
21 |
use Scalar::Util qw(reftype); |
22 |
use Scalar::Util qw(reftype); |
22 |
|
23 |
|
23 |
use Koha::Exceptions; |
24 |
use Koha::Exceptions; |
Lines 143-148
is raised.
Link Here
|
143 |
return $params; |
144 |
return $params; |
144 |
} |
145 |
} |
145 |
); |
146 |
); |
|
|
147 |
|
148 |
=head3 stash_embed |
149 |
|
150 |
$c->stash_embed( $c->match->endpoint->pattern->defaults->{'openapi.op_spec'} ); |
151 |
|
152 |
=cut |
153 |
|
154 |
$app->helper( |
155 |
'stash_embed' => sub { |
156 |
|
157 |
my ( $c, $args ) = @_; |
158 |
|
159 |
my $spec = $args->{spec} // {}; |
160 |
|
161 |
my $embed_spec = $spec->{'x-koha-embed'}; |
162 |
my $embed_header = $c->req->headers->header('x-koha-embed'); |
163 |
|
164 |
Koha::Exceptions::BadParameter->throw("Embedding objects is not allowed on this endpoint.") |
165 |
if $embed_header and !defined $embed_spec; |
166 |
|
167 |
my $THE_embed = {}; |
168 |
foreach my $embed_req ( split /\s*,\s*/, $embed_header ) { |
169 |
my $matches = grep {lc $_ eq lc $embed_req} @{ $embed_spec }; |
170 |
|
171 |
Koha::Exceptions::BadParameter->throw( |
172 |
error => 'Embeding '.$embed_req. ' is not authorised. Check your x-koha-embed headers or remove it.' |
173 |
) unless $matches; |
174 |
|
175 |
_merge_embed( _parse_embed($embed_req), $THE_embed); |
176 |
} |
177 |
|
178 |
$c->stash( 'koha.embed' => $THE_embed ) |
179 |
if $THE_embed; |
180 |
|
181 |
return $c; |
182 |
} |
183 |
); |
146 |
} |
184 |
} |
147 |
|
185 |
|
148 |
=head2 Internal methods |
186 |
=head2 Internal methods |
Lines 198-201
sub _build_order_atom {
Link Here
|
198 |
} |
236 |
} |
199 |
} |
237 |
} |
200 |
|
238 |
|
|
|
239 |
=head3 _parse_embed |
240 |
|
241 |
my $embed = _parse_embed( $string ); |
242 |
|
243 |
Parses I<$string> and outputs data valid for passing to the Kohaa::Object(s)->to_api |
244 |
method. |
245 |
|
246 |
=cut |
247 |
|
248 |
sub _parse_embed { |
249 |
my $string = shift; |
250 |
|
251 |
my $result; |
252 |
my ( $curr, $next ) = split /\s*\.\s*/, $string, 2; |
253 |
|
254 |
if ( $next ) { |
255 |
$result->{$curr} = { children => _parse_embed( $next ) }; |
256 |
} |
257 |
else { |
258 |
$result->{$curr} = {}; |
259 |
} |
260 |
|
261 |
return $result; |
262 |
} |
263 |
|
264 |
=head3 _merge_embed |
265 |
|
266 |
_merge_embed( $parsed_embed, $global_embed ); |
267 |
|
268 |
Merges the hash referenced by I<$parsed_embed> into I<$global_embed>. |
269 |
|
270 |
=cut |
271 |
|
272 |
sub _merge_embed { |
273 |
my ( $structure, $embed ) = @_; |
274 |
|
275 |
my ($root) = keys %{ $structure }; |
276 |
|
277 |
if ( any { $root eq $_ } keys %{ $embed } ) { |
278 |
# Recurse |
279 |
_merge_embed( $structure->{$root}, $embed->{$root} ); |
280 |
} |
281 |
else { |
282 |
# Embed |
283 |
$embed->{$root} = $structure->{$root}; |
284 |
} |
285 |
} |
286 |
|
201 |
1; |
287 |
1; |