]> git.koha-community.org Git - koha.git/blob - Koha/REST/Plugin/PluginRoutes.pm
Bug 22835: Serve plugin static files through API
[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 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
8 # version.
9 #
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.
13 #
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.
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->preference('UseKohaPlugins')
49         && C4::Context->config("enable_plugins") )
50     {
51         @plugins = Koha::Plugins->new()->GetPlugins(
52             {
53                 method => 'api_namespace',
54             }
55         );
56         # plugin needs to define a namespace
57         #@plugins = grep { $_->api_namespace } @plugins;
58     }
59
60     foreach my $plugin ( @plugins ) {
61         $spec = inject_routes( $spec, $plugin, $validator );
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 try {
75
76         my $backup_spec = merge_spec( clone($spec), $plugin );
77         if ( spec_ok( $backup_spec, $validator ) ) {
78             $spec = merge_spec( $spec, $plugin );
79         }
80         else {
81             Koha::Exceptions::Plugin->throw(
82                 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
83             );
84         }
85
86         return $spec;
87     }
88     catch {
89         warn "$_";
90         return $spec;
91     };
92 }
93
94 =head3 merge_spec
95
96 =cut
97
98 sub merge_spec {
99     my ( $spec, $plugin ) = @_;
100
101     if($plugin->can('api_routes')) {
102         my $plugin_spec = $plugin->api_routes;
103
104         foreach my $route ( keys %{ $plugin_spec } ) {
105             my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
106             if ( exists $spec->{ $THE_route } ) {
107                 # Route exists, overwriting is forbidden
108                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
109                     "Attempted to overwrite $THE_route"
110                 );
111             }
112
113             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
114         }
115     }
116
117     if($plugin->can('static_routes')) {
118         my $plugin_spec = $plugin->static_routes;
119
120         foreach my $route ( keys %{ $plugin_spec } ) {
121
122             my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
123             if ( exists $spec->{ $THE_route } ) {
124                 # Route exists, overwriting is forbidden
125                 Koha::Exceptions::Plugin::ForbiddenAction->throw(
126                     "Attempted to overwrite $THE_route"
127                 );
128             }
129
130             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
131         }
132     }
133     return $spec;
134 }
135
136 =head3 spec_ok
137
138 =cut
139
140 sub spec_ok {
141     my ( $spec, $validator ) = @_;
142
143     return try {
144         $validator->load_and_validate_schema(
145             $spec,
146             {
147                 allow_invalid_ref => 1,
148             }
149         );
150         return 1;
151     }
152     catch {
153         return 0;
154     }
155 }
156
157 1;