Bug 20780: Add handling for AcqItemSetSubfieldsWhenRecieved in EDI
[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         # plugin needs to define a namespace
52         @plugins = Koha::Plugins->new()->GetPlugins(
53             {
54                 method => 'api_namespace',
55             }
56         );
57     }
58
59     foreach my $plugin ( @plugins ) {
60         $spec = inject_routes( $spec, $plugin, $validator );
61     }
62
63     return $spec;
64 }
65
66 =head3 inject_routes
67
68 =cut
69
70 sub inject_routes {
71     my ( $spec, $plugin, $validator ) = @_;
72
73     return try {
74
75         my $backup_spec = merge_spec( clone($spec), $plugin );
76         if ( spec_ok( $backup_spec, $validator ) ) {
77             $spec = merge_spec( $spec, $plugin );
78         }
79         else {
80             Koha::Exceptions::Plugin->throw(
81                 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
82             );
83         }
84
85         return $spec;
86     }
87     catch {
88         warn "$_";
89         return $spec;
90     };
91 }
92
93 =head3 merge_spec
94
95 =cut
96
97 sub merge_spec {
98     my ( $spec, $plugin ) = @_;
99
100     if($plugin->can('api_routes')) {
101         my $plugin_spec = $plugin->api_routes;
102
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"
109                 );
110             }
111
112             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
113         }
114     }
115
116     if($plugin->can('static_routes')) {
117         my $plugin_spec = $plugin->static_routes;
118
119         foreach my $route ( keys %{ $plugin_spec } ) {
120
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"
126                 );
127             }
128
129             $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
130         }
131     }
132     return $spec;
133 }
134
135 =head3 spec_ok
136
137 =cut
138
139 sub spec_ok {
140     my ( $spec, $validator ) = @_;
141
142     return try {
143         $validator->load_and_validate_schema(
144             $spec,
145             {
146                 allow_invalid_ref => 1,
147             }
148         );
149         return 1;
150     }
151     catch {
152         return 0;
153     }
154 }
155
156 1;