Bug 25131: Skip loading API plugins if Koha is not installed
[koha.git] / Koha / REST / Plugin / PluginRoutes.pm
1 package Koha::REST::Plugin::PluginRoutes;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Plugin';
21
22 use Koha::Exceptions::Plugin;
23 use Koha::Plugins;
24
25 use Clone qw(clone);
26 use Try::Tiny;
27
28 =head1 NAME
29
30 Koha::REST::Plugin::PluginRoutes
31
32 =head1 API
33
34 =head2 Helper methods
35
36 =head3 register
37
38 =cut
39
40 sub register {
41     my ( $self, $app, $config ) = @_;
42
43     my $spec      = $config->{spec};
44     my $validator = $config->{validator};
45
46     my @plugins;
47
48     if ( C4::Context->config("enable_plugins") and
49          ! C4::Context->needs_install ) # Koha is installed
50     {
51         # plugin needs to define a namespace
52         @plugins = Koha::Plugins->new()->GetPlugins(
53             {
54                 method => 'api_namespace',
55             }
56         );
57
58         foreach my $plugin ( @plugins ) {
59             $spec = inject_routes( $spec, $plugin, $validator );
60         }
61
62     }
63
64     return $spec;
65 }
66
67 =head3 inject_routes
68
69 =cut
70
71 sub inject_routes {
72     my ( $spec, $plugin, $validator ) = @_;
73
74     return merge_spec( $spec, $plugin ) unless $validator;
75
76     return try {
77
78         my $backup_spec = merge_spec( clone($spec), $plugin );
79         if ( spec_ok( $backup_spec, $validator ) ) {
80             $spec = merge_spec( $spec, $plugin );
81         }
82         else {
83             Koha::Exceptions::Plugin->throw(
84                 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
85             );
86         }
87
88         return $spec;
89     }
90     catch {
91         warn "$_";
92         return $spec;
93     };
94 }
95
96 =head3 merge_spec
97
98 =cut
99
100 sub merge_spec {
101     my ( $spec, $plugin ) = @_;
102
103     if($plugin->can('api_routes')) {
104         my $plugin_spec = $plugin->api_routes;
105
106         foreach my $route ( keys %{ $plugin_spec } ) {
107             my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
108             if ( exists $spec->{ $THE_route } ) {
109                 # Route exists, overwriting is forbidden
110                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
111                     "Attempted to overwrite $THE_route"
112                 );
113             }
114
115             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
116         }
117     }
118
119     if($plugin->can('static_routes')) {
120         my $plugin_spec = $plugin->static_routes;
121
122         foreach my $route ( keys %{ $plugin_spec } ) {
123
124             my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
125             if ( exists $spec->{ $THE_route } ) {
126                 # Route exists, overwriting is forbidden
127                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
128                     "Attempted to overwrite $THE_route"
129                 );
130             }
131
132             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
133         }
134     }
135     return $spec;
136 }
137
138 =head3 spec_ok
139
140 =cut
141
142 sub spec_ok {
143     my ( $spec, $validator ) = @_;
144
145     return try {
146         $validator->load_and_validate_schema(
147             $spec,
148             {
149                 allow_invalid_ref => 1,
150             }
151         );
152         return 1;
153     }
154     catch {
155         return 0;
156     }
157 }
158
159 1;