1 package Koha::REST::Plugin::PluginRoutes;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Plugin';
22 use Koha::Exceptions::Plugin;
30 Koha::REST::Plugin::PluginRoutes
41 my ( $self, $app, $config ) = @_;
43 my $spec = $config->{spec};
44 my $validator = $config->{validator};
48 if ( C4::Context->preference('UseKohaPlugins')
49 && C4::Context->config("enable_plugins") )
51 # plugin needs to define a namespace
52 @plugins = Koha::Plugins->new()->GetPlugins(
54 method => 'api_namespace',
59 foreach my $plugin ( @plugins ) {
60 $spec = inject_routes( $spec, $plugin, $validator );
71 my ( $spec, $plugin, $validator ) = @_;
75 my $backup_spec = merge_spec( clone($spec), $plugin );
76 if ( spec_ok( $backup_spec, $validator ) ) {
77 $spec = merge_spec( $spec, $plugin );
80 Koha::Exceptions::Plugin->throw(
81 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
98 my ( $spec, $plugin ) = @_;
100 if($plugin->can('api_routes')) {
101 my $plugin_spec = $plugin->api_routes;
103 foreach my $route ( keys %{ $plugin_spec } ) {
104 my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
105 if ( exists $spec->{ $THE_route } ) {
106 # Route exists, overwriting is forbidden
107 Koha::Exceptions::Plugin::ForbiddenAction->throw(
108 "Attempted to overwrite $THE_route"
112 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
116 if($plugin->can('static_routes')) {
117 my $plugin_spec = $plugin->static_routes;
119 foreach my $route ( keys %{ $plugin_spec } ) {
121 my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
122 if ( exists $spec->{ $THE_route } ) {
123 # Route exists, overwriting is forbidden
124 Koha::Exceptions::Plugin::ForbiddenAction->throw(
125 "Attempted to overwrite $THE_route"
129 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
140 my ( $spec, $validator ) = @_;
143 $validator->load_and_validate_schema(
146 allow_invalid_ref => 1,