blib/lib/Lemonldap/NG/Manager/Api/Menu/Cat.pm

filter: Statementstmt 97.0% 97 / 100 Branchbran 68.0% 49 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 6 totaltotal 83.1% 158 / 190 CC 37 SCAR 19.0
CC
37
Cov
85%
replaceMenuCat
11.4
addMenuCat
9.6
findMenuCatByConfKey
6.1
CRAP 6.7
« blib/lib/Lemonldap/NG/Manager/Api/Providers/CasApp.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Viewer.pm »
1 package Lemonldap::NG::Manager::Api::Menu::Cat;
2  
3 28 our $VERSION = '2.23.0';
4  
5 package Lemonldap::NG::Manager::Api;
6  
7 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Lemonldap::NG::Manager::Conf::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 28 extends 'Lemonldap::NG::Manager::Api::Common';
13  
14 5 ▶sub getMenuCatByConfKey {
Subroutine getMenuCatByConfKey: called CC=3 SCAR=11.0
Pod undocumented
15 5 my ( $self, $req ) = @_;
16  
17 5 ▶ my $confKey = $req->params('confKey')
Branch: 1 branch
BranchTrueFalseTotal
my $confKey = $req->params("confKey") or return $self->sendError($req, "confKey is missing", 400) 5 0 5
18 or return $self->sendError( $req, 'confKey is missing', 400 );
19  
20 5 $self->logger->debug(
21 "[API] Menu Category $confKey configuration requested");
22  
23 # Get latest configuration
24 5 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
25  
26 5 my $menuCat = $self->_getMenuCatByConfKey( $conf, $confKey );
27  
28 # Return 404 if not found
29 5 ▶ return $self->sendError( $req, "Menu category '$confKey' not found", 404 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $menuCat 1 4 5
30 unless ( defined $menuCat );
31  
32 4 return $self->sendJSONresponse( $req, $menuCat );
33 }
34  
35 6 ▶sub findMenuCatByConfKey {
Subroutine findMenuCatByConfKey: called CC=6 SCAR=18.1
Pod undocumented
36 6 my ( $self, $req ) = @_;
37  
38 6 ▶ my $pattern = (
Branch: 2 branches
BranchTrueFalseTotal
defined $req->params('uPattern') ? : 0 6 6
defined $req->params('pattern') ? : 6 0 6
39 defined $req->params('uPattern')
40 ? $req->params('uPattern')
41 : ( defined $req->params('pattern') ? $req->params('pattern') : undef )
42 );
43  
44 6 ▶ return $self->sendError( $req, 'Invalid input: pattern is missing', 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 6 6
45 unless ( defined $pattern );
46  
47 6 ▶ unless ( $pattern = $self->_getRegexpFromPattern($pattern) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern = $self->_getRegexpFromPattern($pattern)) 2 4 6
48 2 return $self->sendError( $req, 'Invalid input: pattern is invalid',
49 400 );
50 }
51  
52 $self->logger->debug(
53 4 "[API] Find Menu Categories by confKey regexp $pattern requested");
54  
55 # Get latest configuration
56 4 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
57  
58 my @menuCats =
59 20 ▶ map { $_ =~ $pattern ? $self->_getMenuCatByConfKey( $conf, $_ ) : () }
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /$pattern/ ? : 4 16 20
60 4 keys %{ $conf->{applicationList} };
61  
62 4 return $self->sendJSONresponse( $req, [@menuCats] );
63 }
64  
65 4 ▶sub addMenuCat {
Subroutine addMenuCat: called CC=9 SCAR=22.7
Pod undocumented
66 4 my ( $self, $req ) = @_;
67 4 my $add = $req->jsonBodyToObj;
68  
69 4 ▶ return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $add 0 4 4
70 unless ($add);
71  
72 return $self->sendError( $req, 'Invalid input: confKey is missing', 400 )
73 4 ▶ unless ( defined $add->{confKey} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $add->{"confKey"} 0 4 4
74  
75 return $self->sendError( $req, 'Invalid input: confKey is not a string',
76 400 )
77 4 ▶ if ( ref $add->{confKey} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $add->{"confKey"} 0 4 4
78  
79 return $self->sendError( $req,
80 'Invalid input: confKey contains invalid characters', 400 )
81 4 ▶ unless ( $add->{confKey} =~ '^\w[\w\.\-]*$' );
Branch: 1 branch
BranchTrueFalseTotal
unless $add->{"confKey"} =~ /^\w[\w\.\-]*$/ 1 3 4
82  
83 return $self->sendError( $req, 'Invalid input: catname is missing', 400 )
84 3 ▶ unless ( defined $add->{catname} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $add->{"catname"} 0 3 3
85  
86 return $self->sendError( $req, 'Invalid input: catname is not a string',
87 400 )
88 3 ▶ if ( ref $add->{catname} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $add->{"catname"} 0 3 3
89  
90 3 $self->logger->debug(
91 "[API] Add Menu Category with confKey $add->{confKey} requested");
92  
93 # Get latest configuration
94 3 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
95  
96 return $self->sendError(
97 $req,
98 "Invalid input: A Menu Category with confKey $add->{confKey} already exists",
99 409
100 3 ▶ ) if ( defined $self->_getMenuCatByConfKey( $conf, $add->{confKey} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_getMenuCatByConfKey($conf, $add->{"confKey"}) 1 2 3
101  
102 2 my $res = $self->_pushMenuCat( $conf, $add->{confKey}, $add, 1 );
103  
104 return $self->sendError( $req, $res->{msg}, 400 )
105 2 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 2 2
106  
107 2 return $self->sendJSONresponse(
108 $req,
109 { message => "Successful operation" },
110 code => 201
111 );
112 }
113  
114 2 ▶sub updateMenuCat {
Subroutine updateMenuCat: called CC=5 SCAR=16.3
Pod undocumented
115 2 my ( $self, $req ) = @_;
116 2 ▶ my $confKey = $req->params('confKey')
Branch: 1 branch
BranchTrueFalseTotal
my $confKey = $req->params("confKey") or return $self->sendError($req, "confKey is missing", 400) 2 0 2
117 or return $self->sendError( $req, 'confKey is missing', 400 );
118  
119 2 my $update = $req->jsonBodyToObj;
120  
121 2 ▶ return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $update 0 2 2
122 unless ($update);
123  
124 2 $self->logger->debug(
125 "[API] Menu Category $confKey configuration update requested");
126  
127 # Get latest configuration
128 2 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
129  
130 2 my $current = $self->_getMenuCatByConfKey( $conf, $confKey );
131  
132 # Return 404 if not found
133  
134 2 ▶ return $self->sendError( $req, "Menu category '$confKey' not found", 404 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $current 1 1 2
135 unless ( defined $current );
136  
137 1 my $res = $self->_pushMenuCat( $conf, $confKey, $update, 0 );
138  
139 return $self->sendError( $req, $res->{msg}, 400 )
140 1 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 1 1
141  
142 1 return $self->sendJSONresponse( $req, undef, code => 204 );
143 }
144  
145 2 ▶sub replaceMenuCat {
Subroutine replaceMenuCat: called CC=10 SCAR=24.4
Pod undocumented
146 2 my ( $self, $req ) = @_;
147 2 ▶ my $confKey = $req->params('confKey')
Branch: 1 branch
BranchTrueFalseTotal
my $confKey = $req->params("confKey") or return $self->sendError($req, "confKey is missing", 400) 2 0 2
148 or return $self->sendError( $req, 'confKey is missing', 400 );
149  
150 2 my $replace = $req->jsonBodyToObj;
151  
152 2 ▶ return $self->sendError( $req, "Invalid input: " . $req->error, 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless $replace 0 2 2
153 unless ($replace);
154  
155 return $self->sendError( $req, 'Invalid input: confKey is missing', 400 )
156 2 ▶ unless ( defined $replace->{confKey} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replace->{"confKey"} 0 2 2
157  
158 return $self->sendError( $req, 'Invalid input: confKey is not a string',
159 400 )
160 2 ▶ if ( ref $replace->{confKey} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $replace->{"confKey"} 0 2 2
161  
162 return $self->sendError( $req,
163 'Invalid input: confKey contains invalid characters', 400 )
164 2 ▶ unless ( $replace->{confKey} =~ '^\w[\w\.\-]*$' );
Branch: 1 branch
BranchTrueFalseTotal
unless $replace->{"confKey"} =~ /^\w[\w\.\-]*$/ 0 2 2
165  
166 return $self->sendError( $req, 'Invalid input: catname is missing', 400 )
167 2 ▶ unless ( defined $replace->{catname} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $replace->{"catname"} 0 2 2
168  
169 return $self->sendError( $req, 'Invalid input: catname is not a string',
170 400 )
171 2 ▶ if ( ref $replace->{catname} );
Branch: 1 branch
BranchTrueFalseTotal
if ref $replace->{"catname"} 0 2 2
172  
173 2 $self->logger->debug(
174 "[API] Menu Category $confKey configuration replace requested");
175  
176 # Get latest configuration
177 2 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
178  
179 # Return 404 if not found
180  
181 2 ▶ return $self->sendError( $req, "Menu category '$confKey' not found", 404 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_getMenuCatByConfKey($conf, $confKey) 1 1 2
182 unless ( defined $self->_getMenuCatByConfKey( $conf, $confKey ) );
183  
184 1 my $res = $self->_pushMenuCat( $conf, $confKey, $replace, 1 );
185 return $self->sendError( $req, $res->{msg}, 400 )
186 1 ▶ unless ( $res->{res} eq 'ok' );
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"res"} eq "ok" 0 1 1
187  
188 1 return $self->sendJSONresponse( $req, undef, code => 204 );
189 }
190  
191 3 ▶sub deleteMenuCat {
Subroutine deleteMenuCat: called CC=3 SCAR=11.0
Pod undocumented
192 3 my ( $self, $req ) = @_;
193 3 ▶ my $confKey = $req->params('confKey')
Branch: 1 branch
BranchTrueFalseTotal
my $confKey = $req->params("confKey") or return $self->sendError($req, "confKey is missing", 400) 3 0 3
194 or return $self->sendError( $req, 'confKey is missing', 400 );
195  
196 3 $self->logger->debug(
197 "[API] Menu Category $confKey configuration delete requested");
198  
199 # Get latest configuration
200 3 my $conf = $self->_confAcc->getConf( { noCache => 1 } );
201  
202 3 my $delete = $self->_getMenuCatByConfKey( $conf, $confKey );
203  
204 # Return 404 if not found
205  
206 3 ▶ return $self->sendError( $req, "Menu category '$confKey' not found", 404 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delete 1 2 3
207 unless ( defined $delete );
208  
209 2 delete $conf->{applicationList}->{$confKey};
210  
211 # Save configuration
212 2 $self->_saveApplyConf($conf);
213  
214 2 return $self->sendJSONresponse( $req, undef, code => 204 );
215 }
216  
217 27 ▶sub _getMenuCatByConfKey {
Subroutine _getMenuCatByConfKey: called CC=3 SCAR=11.0
218 27 my ( $self, $conf, $confKey ) = @_;
219  
220 # Check if confKey is defined
221 27 ▶ return undef unless ( defined $conf->{applicationList}->{$confKey} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conf->{"applicationList"}{$confKey} 8 19 27
222  
223 my $menuCat = {
224 confKey => $confKey,
225 catname => $conf->{applicationList}->{$confKey}->{catname}
226 19 };
227  
228 $menuCat->{order} = $conf->{applicationList}->{$confKey}->{order}
229 19 ▶ if ( defined $conf->{applicationList}->{$confKey}->{order} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"applicationList"}{$confKey}{"order"} 14 5 19
230  
231 19 return $menuCat;
232 }
233  
234 4 ▶sub _pushMenuCat {
Subroutine _pushMenuCat: called CC=5 SCAR=16.4
235 4 my ( $self, $conf, $confKey, $push, $replace ) = @_;
236  
237 4 ▶ if ($replace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replace) 3 1 4
238 3 $conf->{applicationList}->{$confKey} = {};
239 3 $conf->{applicationList}->{$confKey}->{type} = "category";
240 }
241  
242 $conf->{applicationList}->{$confKey}->{order} = $push->{order}
243 4 ▶ if ( defined $push->{order} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $push->{"order"} 3 1 4
244  
245 $conf->{applicationList}->{$confKey}->{catname} = $push->{catname}
246 4 ▶ if ( defined $push->{catname} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $push->{"catname"} 3 1 4
247  
248 # Test new configuration
249 4 my $parser = Lemonldap::NG::Manager::Conf::Parser->new( {
250 refConf => $self->_confAcc->getConf( { noCache => 1 } ),
251 newConf => $conf,
252 req => {},
253 }
254 );
255 4 ▶ unless ( $parser->testNewConf( $self->p ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parser->testNewConf($self->p)) 0 4 4
256 return {
257 res => 'ko',
258 code => 400,
259 msg => "Configuration error: "
260 0 . join( ". ", map { $_->{message} } @{ $parser->errors } ),
261 };
262 }
263  
264 # Save configuration
265 4 $self->_saveApplyConf($conf);
266  
267 4 return { res => 'ok' };
268 }
269  
270 28 1;
« blib/lib/Lemonldap/NG/Manager/Api/Providers/CasApp.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Viewer.pm »