Bug 31378: Add authentication provider endpoints
[srvgit] / Koha / REST / V1 / Auth / Provider / Domains.pm
1 package Koha::REST::V1::Auth::Provider::Domains;
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::Controller';
21
22 use Koha::Auth::Provider::Domains;
23 use Koha::Auth::Providers;
24
25 use Koha::Database;
26
27 use Scalar::Util qw(blessed);
28 use Try::Tiny;
29
30 =head1 NAME
31
32 Koha::REST::V1::Auth::Provider::Domains - Controller library for handling
33 authentication provider domains routes.
34
35 =head2 Operations
36
37 =head3 list
38
39 Controller method for listing authentication provider domains.
40
41 =cut
42
43 sub list {
44     my $c = shift->openapi->valid_input or return;
45
46     return try {
47         my $auth_provider_id = $c->validation->param('auth_provider_id');
48         my $provider         = Koha::Auth::Providers->find($auth_provider_id);
49
50         unless ($provider) {
51             return $c->render(
52                 status  => 404,
53                 openapi => {
54                     error      => 'Object not found',
55                     error_code => 'not_found',
56                 }
57             );
58         }
59
60         my $domains_rs = $provider->domains;
61         return $c->render(
62             status  => 200,
63             openapi => $c->objects->search($domains_rs)
64         );
65     } catch {
66         $c->unhandled_exception($_);
67     };
68 }
69
70 =head3 get
71
72 Controller method for retrieving an authentication provider domain.
73
74 =cut
75
76 sub get {
77     my $c = shift->openapi->valid_input or return;
78
79     return try {
80
81         my $auth_provider_id = $c->validation->param('auth_provider_id');
82         my $provider         = Koha::Auth::Providers->find($auth_provider_id);
83
84         unless ($provider) {
85             return $c->render(
86                 status  => 404,
87                 openapi => {
88                     error      => 'Object not found',
89                     error_code => 'not_found',
90                 }
91             );
92         }
93
94         my $domains_rs = $provider->domains;
95
96         my $auth_provider_domain_id = $c->validation->param('auth_provider_domain_id');
97         my $domain                  = $c->objects->find( $domains_rs, $auth_provider_domain_id );
98
99         unless ($domain) {
100             return $c->render(
101                 status  => 404,
102                 openapi => {
103                     error      => 'Object not found',
104                     error_code => 'not_found',
105                 }
106             );
107         }
108
109         return $c->render( status => 200, openapi => $domain );
110     } catch {
111         $c->unhandled_exception($_);
112     }
113 }
114
115 =head3 add
116
117 Controller method for adding an authentication provider.
118
119 =cut
120
121 sub add {
122     my $c = shift->openapi->valid_input or return;
123
124     return try {
125
126         Koha::Database->new->schema->txn_do(
127             sub {
128                 my $domain = Koha::Auth::Provider::Domain->new_from_api( $c->validation->param('body') );
129                 $domain->store;
130
131                 $c->res->headers->location( $c->req->url->to_string . '/' . $domain->id );
132                 return $c->render(
133                     status  => 201,
134                     openapi => $domain->to_api
135                 );
136             }
137         );
138     } catch {
139         if ( blessed($_) and $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
140             return $c->render(
141                 status  => 404,
142                 openapi => {
143                     error      => 'Object not found',
144                     error_code => 'not_found',
145                 }
146             );
147         }
148
149         $c->unhandled_exception($_);
150     };
151 }
152
153 =head3 update
154
155 Controller method for updating an authentication provider domain.
156
157 =cut
158
159 sub update {
160     my $c = shift->openapi->valid_input or return;
161
162     my $auth_provider_id        = $c->validation->param('auth_provider_id');
163     my $auth_provider_domain_id = $c->validation->param('auth_provider_domain_id');
164
165     my $domain = Koha::Auth::Provider::Domains->find(
166         { auth_provider_id => $auth_provider_id, auth_provider_domain_id => $auth_provider_domain_id } );
167
168     unless ($domain) {
169         return $c->render(
170             status  => 404,
171             openapi => {
172                 error      => 'Object not found',
173                 error_code => 'not_found',
174             }
175         );
176     }
177
178     return try {
179
180         Koha::Database->new->schema->txn_do(
181             sub {
182
183                 $domain->set_from_api( $c->validation->param('body') );
184                 $domain->store->discard_changes;
185
186                 return $c->render(
187                     status  => 200,
188                     openapi => $domain->to_api
189                 );
190             }
191         );
192     } catch {
193         $c->unhandled_exception($_);
194     };
195 }
196
197 =head3 delete
198
199 Controller method for deleting an authentication provider.
200
201 =cut
202
203 sub delete {
204     my $c = shift->openapi->valid_input or return;
205
206     my $auth_provider_id        = $c->validation->param('auth_provider_id');
207     my $auth_provider_domain_id = $c->validation->param('auth_provider_domain_id');
208
209     my $domain = Koha::Auth::Provider::Domains->find(
210         { auth_provider_id => $auth_provider_id, auth_provider_domain_id => $auth_provider_domain_id } );
211
212     unless ($domain) {
213         return $c->render(
214             status  => 404,
215             openapi => {
216                 error      => 'Object not found',
217                 error_code => 'not_found',
218             }
219         );
220     }
221
222     return try {
223         $domain->delete;
224         return $c->render(
225             status  => 204,
226             openapi => q{}
227         );
228     } catch {
229         $c->unhandled_exception($_);
230     };
231 }
232
233 1;