Debian Codegraph @

lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Api/Menu/App.pm

raw
1 package Lemonldap::NG::Manager::Api::Menu::App;
2
3 our $VERSION = '2.23.0';
4
5 package Lemonldap::NG::Manager::Api;
6
7 use strict;
8 use utf8;
9 use Mouse;
10 use Lemonldap::NG::Manager::Conf::Parser;
11
12 extends 'Lemonldap::NG::Manager::Api::Common';
13
14 sub getMenuApp {
15 my ( $self, $req ) = @_;
16
17 my $catConfKey = $req->params('confKey')
18 or return $self->sendError( $req, 'Category confKey is missing', 400 );
19
20 my $appConfKey = $req->params('appConfKey');
21
22 # Get latest configuration
23 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
24
25 # Check if catConfKey is defined
26 return $self->sendError( $req,
27 "Menu category '$catConfKey' not found", 404 )
28 unless ( defined $conf->{applicationList}->{$catConfKey} );
29
30 if ( defined $appConfKey ) {
31
32 # Return one application referenced with this appConfKey
33 $self->logger->debug(
34 "[API] Menu application $appConfKey from category $catConfKey configuration requested"
35 );
36
37 my $menuApp =
38 $self->_getMenuAppByConfKey( $conf, $catConfKey, $appConfKey );
39
40 # Return 404 if not found
41 return $self->sendError(
42 $req,
43 "Menu application '$appConfKey' from category '$catConfKey' not found",
44 404
45 ) unless ( defined $menuApp );
46
47 return $self->sendJSONresponse( $req, $menuApp );
48
49 }
50 else {
51
52 # Return all applications for this category
53 $self->logger->debug(
54 "[API] Menu applications from category $catConfKey configuration requested"
55 );
56
57 my $cat = $conf->{applicationList}->{$catConfKey};
58
59 my @menuApps =
60 map {
61 $self->_isCatApp( $cat->{$_} )
62 ? $self->_getMenuAppByConfKey( $conf, $catConfKey, $_ )
63 : ()
64 }
65 keys %{$cat};
66
67 return $self->sendJSONresponse( $req, [@menuApps] );
68
69 }
70 }
71
72 sub findMenuAppByConfKey {
73 my ( $self, $req ) = @_;
74
75 my $catConfKey = $req->params('confKey')
76 or return $self->sendError( $req, 'Category confKey is missing', 400 );
77
78 my $pattern = (
79 defined $req->params('uPattern')
80 ? $req->params('uPattern')
81 : ( defined $req->params('pattern') ? $req->params('pattern') : undef )
82 );
83
84 return $self->sendError( $req, 'Invalid input: pattern is missing', 400 )
85 unless ( defined $pattern );
86
87 unless ( $pattern = $self->_getRegexpFromPattern($pattern) ) {
88 return $self->sendError( $req, 'Invalid input: pattern is invalid',
89 400 );
90 }
91
92 $self->logger->debug(
93 "[API] Find Menu Applications from category $catConfKey by confKey regexp $pattern requested"
94 );
95
96 # Get latest configuration
97 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
98
99 # Check if catConfKey is defined
100 return $self->sendError( $req,
101 "Menu category '$catConfKey' not found", 404 )
102 unless ( defined $conf->{applicationList}->{$catConfKey} );
103
104 my $cat = $conf->{applicationList}->{$catConfKey};
105
106 my @menuApps =
107 map {
108 $self->_isCatApp( $cat->{$_} )
109 && $_ =~ $pattern
110 ? $self->_getMenuAppByConfKey( $conf, $catConfKey, $_ )
111 : ()
112 }
113 keys %{$cat};
114
115 return $self->sendJSONresponse( $req, [@menuApps] );
116 }
117
118 sub addMenuApp {
119 my ( $self, $req ) = @_;
120 my $add = $req->jsonBodyToObj;
121
122 my $catConfKey = $req->params('confKey')
123 or return $self->sendError( $req, 'Category confKey is missing', 400 );
124
125 return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
126 unless ($add);
127
128 return $self->sendError( $req, 'Invalid input: confKey is missing', 400 )
129 unless ( defined $add->{confKey} );
130
131 return $self->sendError( $req, 'Invalid input: confKey is not a string',
132 400 )
133 if ( ref $add->{confKey} );
134
135 return $self->sendError( $req,
136 'Invalid input: confKey contains invalid characters', 400 )
137 unless ( $add->{confKey} =~ '^\w[\w\.\-]*$' );
138
139 return $self->sendError( $req, 'Invalid input: name is missing', 400 )
140 unless ( defined $add->{options} && defined $add->{options}{name} );
141
142 return $self->sendError( $req, 'Invalid input: name is not a string', 400 )
143 if ( ref $add->{options}{name} );
144
145 $self->logger->debug(
146 "[API] Add Menu Application from category $catConfKey with confKey $add->{confKey} requested"
147 );
148
149 # Get latest configuration
150 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
151
152 # Check if catConfKey is defined
153 return $self->sendError( $req,
154 "Menu category '$catConfKey' not found", 404 )
155 unless ( defined $conf->{applicationList}->{$catConfKey} );
156
157 return $self->sendError(
158 $req,
159 "Invalid input: A Menu Application with confKey $add->{confKey} already exists in category $catConfKey",
160 409
161 )
162 if (
163 defined $self->_getMenuAppByConfKey( $conf, $catConfKey,
164 $add->{confKey} ) );
165
166 my $res =
167 $self->_pushMenuApp( $conf, $catConfKey, $add->{confKey}, $add, 1 );
168
169 return $self->sendError( $req, $res->{msg}, 400 )
170 unless ( $res->{res} eq 'ok' );
171
172 return $self->sendJSONresponse(
173 $req,
174 { message => "Successful operation" },
175 code => 201
176 );
177 }
178
179 sub updateMenuApp {
180 my ( $self, $req ) = @_;
181
182 my $catConfKey = $req->params('confKey')
183 or return $self->sendError( $req, 'Category confKey is missing', 400 );
184
185 my $appConfKey = $req->params('appConfKey')
186 or return $self->sendError( $req, 'Application confKey is missing', 400 );
187
188 my $update = $req->jsonBodyToObj;
189
190 return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
191 unless ($update);
192
193 $self->logger->debug(
194 "[API] Menu application $appConfKey from category $catConfKey configuration update requested"
195 );
196
197 # Get latest configuration
198 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
199
200 # Return 404 if not found
201
202 return $self->sendError( $req,
203 "Menu category '$catConfKey' not found", 404 )
204 unless ( defined $self->_getMenuCatByConfKey( $conf, $catConfKey ) );
205
206 return $self->sendError(
207 $req,
208 "Menu application '$appConfKey' from category '$catConfKey' not found",
209 404
210 )
211 unless (
212 defined $self->_getMenuAppByConfKey( $conf, $catConfKey, $appConfKey )
213 );
214
215 my $res =
216 $self->_pushMenuApp( $conf, $catConfKey, $appConfKey, $update, 0 );
217
218 return $self->sendError( $req, $res->{msg}, 400 )
219 unless ( $res->{res} eq 'ok' );
220
221 return $self->sendJSONresponse( $req, undef, code => 204 );
222 }
223
224 sub replaceMenuApp {
225 my ( $self, $req ) = @_;
226 my $catConfKey = $req->params('confKey')
227 or return $self->sendError( $req, 'Category confKey is missing', 400 );
228
229 my $appConfKey = $req->params('appConfKey')
230 or return $self->sendError( $req, 'Application confKey is missing', 400 );
231
232 my $replace = $req->jsonBodyToObj;
233
234 return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
235 unless ($replace);
236
237 return $self->sendError( $req, 'Invalid input: confKey is missing', 400 )
238 unless ( defined $replace->{confKey} );
239
240 return $self->sendError( $req, 'Invalid input: confKey is not a string',
241 400 )
242 if ( ref $replace->{confKey} );
243
244 return $self->sendError( $req,
245 'Invalid input: confKey contains invalid characters', 400 )
246 unless ( $replace->{confKey} =~ '^\w[\w\.\-]*$' );
247
248 return $self->sendError( $req, 'Invalid input: name is missing', 400 )
249 unless ( defined $replace->{options}
250 && defined $replace->{options}{name} );
251
252 return $self->sendError( $req, 'Invalid input: name is not a string', 400 )
253 if ( ref $replace->{options}{name} );
254
255 $self->logger->debug(
256 "[API] Menu application $appConfKey from category $catConfKey configuration replace requested"
257 );
258
259 # Get latest configuration
260 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
261
262 # Return 404 if not found
263
264 return $self->sendError( $req,
265 "Menu category '$catConfKey' not found", 404 )
266 unless ( defined $self->_getMenuCatByConfKey( $conf, $catConfKey ) );
267
268 return $self->sendError(
269 $req,
270 "Menu application '$appConfKey' from category '$catConfKey' not found",
271 404
272 )
273 unless (
274 defined $self->_getMenuAppByConfKey( $conf, $catConfKey, $appConfKey )
275 );
276
277 my $res =
278 $self->_pushMenuApp( $conf, $catConfKey, $appConfKey, $replace, 1 );
279 return $self->sendError( $req, $res->{msg}, 400 )
280 unless ( $res->{res} eq 'ok' );
281
282 return $self->sendJSONresponse( $req, undef, code => 204 );
283 }
284
285 sub deleteMenuApp {
286 my ( $self, $req ) = @_;
287
288 my $catConfKey = $req->params('confKey')
289 or return $self->sendError( $req, 'Category confKey is missing', 400 );
290
291 my $appConfKey = $req->params('appConfKey')
292 or return $self->sendError( $req, 'Application confKey is missing', 400 );
293
294 $self->logger->debug(
295 "[API] Menu Application $appConfKey from category $catConfKey configuration delete requested"
296 );
297
298 # Get latest configuration
299 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
300
301 return $self->sendError( $req,
302 "Menu category '$catConfKey' not found", 404 )
303 unless ( defined $self->_getMenuCatByConfKey( $conf, $catConfKey ) );
304
305 my $delete = $self->_getMenuAppByConfKey( $conf, $catConfKey, $appConfKey );
306
307 # Return 404 if not found
308
309 return $self->sendError( $req,
310 "Menu category '$appConfKey' not found", 404 )
311 unless ( defined $delete );
312
313 delete $conf->{applicationList}->{$catConfKey}->{$appConfKey};
314
315 # Save configuration
316 $self->_saveApplyConf($conf);
317
318 return $self->sendJSONresponse( $req, undef, code => 204 );
319 }
320
321 sub _isCatApp {
322 my ( $self, $candidate ) = @_;
323
324 # Check if candidate is a hash, has "type" defined and if "type" equals "application".
325 return
326 ref $candidate eq ref {}
327 && defined $candidate->{type}
328 && $candidate->{type} eq 'application';
329 }
330
331 sub _getMenuAppByConfKey {
332 my ( $self, $conf, $catConfKey, $appConfKey ) = @_;
333
334 # Check if catConfKey is defined
335 return undef unless ( defined $conf->{applicationList}->{$catConfKey} );
336
337 # Check if appConfKey is defined
338 return undef
339 unless ( defined $conf->{applicationList}->{$catConfKey}->{$appConfKey} );
340
341 my $cat = $conf->{applicationList}->{$catConfKey};
342
343 my $menuApp = { confKey => $appConfKey };
344
345 $menuApp->{order} = $cat->{$appConfKey}->{order}
346 if ( defined $cat->{$appConfKey}->{order} );
347
348 # Get options
349 my $options = {};
350 for my $configOption ( keys %{ $cat->{$appConfKey}->{options} } ) {
351 $options->{ $self->_translateOptionConfToApi($configOption) } =
352 $cat->{$appConfKey}->{options}->{$configOption};
353 }
354
355 $menuApp->{options} = $options;
356
357 return $menuApp;
358 }
359
360 sub _pushMenuApp {
361 my ( $self, $conf, $catConfKey, $appConfKey, $push, $replace ) = @_;
362
363 if ($replace) {
364 $conf->{applicationList}->{$catConfKey}->{$appConfKey} = {};
365 $conf->{applicationList}->{$catConfKey}->{$appConfKey}->{type} =
366 "application";
367 $conf->{applicationList}->{$catConfKey}->{$appConfKey}->{options} = {};
368 $conf->{applicationList}->{$catConfKey}->{$appConfKey}->{options}
369 ->{display} = "auto";
370 $conf->{applicationList}->{$catConfKey}->{$appConfKey}->{options}
371 ->{logo} = "network.png";
372 }
373
374 $conf->{applicationList}->{$catConfKey}->{$appConfKey}->{order} =
375 $push->{order}
376 if ( defined $push->{order} );
377
378 if ( defined $push->{options} ) {
379 foreach ( keys %{ $push->{options} } ) {
380 $conf->{applicationList}->{$catConfKey}->{$appConfKey}->{options}
381 ->{$_} = $push->{options}->{$_};
382 }
383 }
384
385 # Test new configuration
386 my $parser = Lemonldap::NG::Manager::Conf::Parser->new( {
387 refConf => $self->_confAcc->getConf( { noCache => 1 } ),
388 newConf => $conf,
389 req => {},
390 }
391 );
392 unless ( $parser->testNewConf( $self->p ) ) {
393 return {
394 res => 'ko',
395 code => 400,
396 msg => "Configuration error: "
397 . join( ". ", map { $_->{message} } @{ $parser->errors } ),
398 };
399 }
400
401 # Save configuration
402 $self->_saveApplyConf($conf);
403
404 return { res => 'ok' };
405 }
406
407 1;