Bug 17600: Standardize our EXPORT_OK
[srvgit] / 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 use Koha::Logger;
25
26 use Clone qw( clone );
27 use Try::Tiny qw( catch try );
28
29 =head1 NAME
30
31 Koha::REST::Plugin::PluginRoutes
32
33 =head1 API
34
35 =head2 Helper methods
36
37 =head3 register
38
39 =cut
40
41 sub register {
42     my ( $self, $app, $config ) = @_;
43
44     my $spec      = $config->{spec};
45     my $validator = $config->{validator};
46
47     my @plugins;
48
49     if ( C4::Context->config("enable_plugins") )
50     {
51         $self->{'swagger-v2-schema'} = $app->home->rel_file("api/swagger-v2-schema.json");
52
53         # plugin needs to define a namespace
54         @plugins = Koha::Plugins->new()->GetPlugins(
55             {
56                 method => 'api_namespace',
57             }
58         );
59
60         foreach my $plugin ( @plugins ) {
61             $spec = $self->inject_routes( $spec, $plugin, $validator );
62         }
63
64     }
65
66     return $spec;
67 }
68
69 =head3 inject_routes
70
71 =cut
72
73 sub inject_routes {
74     my ( $self, $spec, $plugin, $validator ) = @_;
75
76     return merge_spec( $spec, $plugin ) unless $validator;
77
78     return try {
79
80         my $backup_spec = merge_spec( clone($spec), $plugin );
81         if ( $self->spec_ok( $backup_spec, $validator ) ) {
82             $spec = merge_spec( $spec, $plugin );
83         }
84         else {
85             Koha::Exceptions::Plugin->throw(
86                 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
87             );
88         }
89
90         return $spec;
91     }
92     catch {
93         my $error = $_;
94         my $class = ref $plugin;
95         my $logger = Koha::Logger->get({ interface => 'api' });
96         $logger->error("Plugin $class route injection failed: $error");
97         return $spec;
98     };
99 }
100
101 =head3 merge_spec
102
103 =cut
104
105 sub merge_spec {
106     my ( $spec, $plugin ) = @_;
107
108     if($plugin->can('api_routes')) {
109         my $plugin_spec = $plugin->api_routes;
110
111         foreach my $route ( keys %{ $plugin_spec } ) {
112             my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
113             if ( exists $spec->{ $THE_route } ) {
114                 # Route exists, overwriting is forbidden
115                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
116                     "Attempted to overwrite $THE_route"
117                 );
118             }
119
120             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
121         }
122     }
123
124     if($plugin->can('static_routes')) {
125         my $plugin_spec = $plugin->static_routes;
126
127         foreach my $route ( keys %{ $plugin_spec } ) {
128
129             my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
130             if ( exists $spec->{ $THE_route } ) {
131                 # Route exists, overwriting is forbidden
132                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
133                     "Attempted to overwrite $THE_route"
134                 );
135             }
136
137             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
138         }
139     }
140     return $spec;
141 }
142
143 =head3 spec_ok
144
145 =cut
146
147 sub spec_ok {
148     my ( $self, $spec, $validator ) = @_;
149
150     my $schema = $self->{'swagger-v2-schema'};
151
152     return try {
153         $validator->load_and_validate_schema(
154             $spec,
155             {
156                 allow_invalid_ref => 1,
157                 schema => ( $schema ) ? $schema : undef,
158             }
159         );
160         return 1;
161     }
162     catch {
163         return 0;
164     }
165 }
166
167 1;