Bug 31378: Rename Auth Provider to Identity Provider and add Client.t tests
[koha.git] / Koha / REST / V1 / Auth / Identity / Provider / Domains.pm
1 package Koha::REST::V1::Auth::Identity::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::Identity::Provider::Domains;
23 use Koha::Auth::Identity::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::Identity::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 $identity_provider_id = $c->validation->param('identity_provider_id');
48         my $provider         = Koha::Auth::Identity::Providers->find($identity_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 $identity_provider_id = $c->validation->param('identity_provider_id');
82         my $provider         = Koha::Auth::Identity::Providers->find($identity_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 $identity_provider_domain_id = $c->validation->param('identity_provider_domain_id');
97         my $domain                  = $c->objects->find( $domains_rs, $identity_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         my $params = $c->validation->param('body');
126         $params->{identity_provider_id} = $c->validation->param('identity_provider_id');
127         Koha::Database->new->schema->txn_do(
128             sub {
129                 my $domain = Koha::Auth::Identity::Provider::Domain->new_from_api( $params );
130                 $domain->store;
131
132                 $c->res->headers->location( $c->req->url->to_string . '/' . $domain->id );
133                 return $c->render(
134                     status  => 201,
135                     openapi => $domain->to_api
136                 );
137             }
138         );
139     } catch {
140         if ( blessed($_) and $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
141             return $c->render(
142                 status  => 404,
143                 openapi => {
144                     error      => 'Object not found',
145                     error_code => 'not_found',
146                 }
147             );
148         }
149
150         $c->unhandled_exception($_);
151     };
152 }
153
154 =head3 update
155
156 Controller method for updating an authentication provider domain.
157
158 =cut
159
160 sub update {
161     my $c = shift->openapi->valid_input or return;
162
163     my $identity_provider_id        = $c->validation->param('identity_provider_id');
164     my $identity_provider_domain_id = $c->validation->param('identity_provider_domain_id');
165
166     my $domain = Koha::Auth::Identity::Provider::Domains->find(
167         { identity_provider_id => $identity_provider_id, identity_provider_domain_id => $identity_provider_domain_id } );
168
169     unless ($domain) {
170         return $c->render(
171             status  => 404,
172             openapi => {
173                 error      => 'Object not found',
174                 error_code => 'not_found',
175             }
176         );
177     }
178
179     return try {
180
181         Koha::Database->new->schema->txn_do(
182             sub {
183
184                 $domain->set_from_api( $c->validation->param('body') );
185                 $domain->store->discard_changes;
186
187                 return $c->render(
188                     status  => 200,
189                     openapi => $domain->to_api
190                 );
191             }
192         );
193     } catch {
194         $c->unhandled_exception($_);
195     };
196 }
197
198 =head3 delete
199
200 Controller method for deleting an authentication provider.
201
202 =cut
203
204 sub delete {
205     my $c = shift->openapi->valid_input or return;
206
207     my $identity_provider_id        = $c->validation->param('identity_provider_id');
208     my $identity_provider_domain_id = $c->validation->param('identity_provider_domain_id');
209
210     my $domain = Koha::Auth::Identity::Provider::Domains->find(
211         { identity_provider_id => $identity_provider_id, identity_provider_domain_id => $identity_provider_domain_id } );
212
213     unless ($domain) {
214         return $c->render(
215             status  => 404,
216             openapi => {
217                 error      => 'Object not found',
218                 error_code => 'not_found',
219             }
220         );
221     }
222
223     return try {
224         $domain->delete;
225         return $c->render(
226             status  => 204,
227             openapi => q{}
228         );
229     } catch {
230         $c->unhandled_exception($_);
231     };
232 }
233
234 1;