blib/lib/Lemonldap/NG/Manager/Conf.pm

filter: Statementstmt 74.0% 151 / 204 Branchbran 51.0% 50 / 98 Conditioncond 60.7% 17 / 28 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 81.8% 18 / 22 Podpod 0.0% 0 / 11 totaltotal 64.1% 243 / 379 CC 63 SCAR 30.1
CC
63
Cov
66%
sendTestMail
42.0
prx
42.0
newConf
33.5
CRAP 20.3
« blib/lib/Lemonldap/NG/Manager/Build/OpenApi.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Notifications.pm »
1 # This module implements all the methods that responds to '/confs/*' requests
2 # It contains 2 sections:
3 # - initialization methods
4 # - upload method
5 #
6 # Read methods are inherited from Lemonldap::NG::Common::Conf::RESTServer
7 package Lemonldap::NG::Manager::Conf;
8  
9 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Lemonldap::NG::Common::Conf::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 28 ▶use Lemonldap::NG::Common::EmailTransport;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 28 ▶use Lemonldap::NG::Common::Util::Crypto;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 28 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 28 extends qw(
19 Lemonldap::NG::Manager::Plugin
20 Lemonldap::NG::Common::Conf::RESTServer
21 );
22  
23 28 our $VERSION = '2.23.0';
24  
25 #############################
26 # I. INITIALIZATION METHODS #
27 #############################
28  
29 28 ▶use constant defaultRoute => 'manager.html';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 28 ▶use constant icon => 'cog';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 28 has defaultNewKeySize => ( is => 'rw', default => 2048, );
33 28 has defaultCurve => ( is => 'rw', default => 'prime256v1', );
34  
35 45 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
36 45 my ( $self, $conf ) = @_;
37  
38 $self->defaultNewKeySize( $conf->{defaultNewKeySize} )
39 45 ▶ if $conf->{defaultNewKeySize};
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"defaultNewKeySize"} 45 0 45
40  
41 45 $self->ua( Lemonldap::NG::Common::UserAgent->new($conf) );
42  
43 # HTML template
44 45 $self->addRoute( 'manager.html', undef, ['GET'] )
45  
46 # READ
47 # Special keys
48 ->addRoute(
49 confs => {
50 ':cfgNum' => [
51 qw(virtualHosts samlIDPMetaDataNodes samlSPMetaDataNodes
52 applicationList oidcOPMetaDataNodes oidcRPMetaDataNodes
53 casSrvMetaDataNodes casAppMetaDataNodes
54 authChoiceModules grantSessionRules combModules sfExtra
55 openIdIDPList keyNodes)
56 ]
57 },
58 ['GET']
59 )
60  
61 # Other keys
62 ->addRoute( confs => { ':cfgNum' => { '*' => 'getKey' } }, ['GET'] )
63  
64 # New key and conf save
65 ->addRoute(
66 confs => {
67 newRSAKey => 'newRSAKey',
68 newCertificate => 'newCertificate',
69 newEcCertificate => 'newEcCertificate',
70 newEcKeys => 'newEcKeys',
71 sendTestMail => 'sendTestMail',
72 raw => 'newRawConf',
73 '*' => 'newConf'
74 },
75 ['POST']
76 )
77  
78 # Difference between confs
79 ->addRoute( diff => { ':conf1' => { ':conf2' => 'diff' } } )
80 ->addRoute( 'diff.html', undef, ['GET'] )
81  
82 # Url loader
83 ->addRoute( 'prx', undef, ['POST'] );
84 45 return 1;
85 }
86  
87 # 35 - New Certificate on demand
88 # --------------------------
89  
90 ##@method public PSGI-JSON-response newRSAKey($req)
91 # Return a hashref containing private and public keys
92 # The posted data must contain a JSON object containing
93 # {"password":"newpassword"}
94 #
95 #@param $req Lemonldap::NG::Common::PSGI::Request object
96 #@return PSGI JSON response
97 2 ▶sub newCertificate {
Subroutine newCertificate: called CC=2 SCAR=7.0
Pod undocumented
98 2 my ( $self, $req, @others ) = @_;
99 2 ▶ return $self->sendError( $req, 'There is no subkey for "newCertificate"',
Branch: 1 branch
BranchTrueFalseTotal
if @others 0 2 2
100 400 )
101 if (@others);
102 2 my $query = $req->jsonBodyToObj;
103  
104 2 my $keys = $self->_generateX509( $query->{password}, "RSA" );
105 2 return $self->sendJSONresponse( $req, $keys );
106 }
107  
108 0 ▶sub newEcCertificate {
Subroutine newEcCertificate: not called CC=2 SCAR=17.9
Pod undocumented
109 0 my ( $self, $req, @others ) = @_;
110 0 ▶ return $self->sendError( $req, 'There is no subkey for "newEcCertificate"',
Branch: 1 branch
BranchTrueFalseTotal
if @others 0 0 0
111 400 )
112 if (@others);
113 0 my $query = $req->jsonBodyToObj;
114  
115 0 my $keys = $self->_generateX509( $query->{password}, "EC" );
116 0 return $self->sendJSONresponse( $req, $keys );
117 }
118  
119 # 35 - New RSA key pair on demand
120 # --------------------------
121  
122 ##@method public PSGI-JSON-response newRSAKey($req)
123 # Return a hashref containing private and public keys
124 # The posted data must contain a JSON object containing
125 # {"password":"newpassword"}
126 #
127 #@param $req Lemonldap::NG::Common::PSGI::Request object
128 #@return PSGI JSON response
129 2 ▶sub newRSAKey {
Subroutine newRSAKey: called CC=2 SCAR=7.0
Pod undocumented
130 2 my ( $self, $req, @others ) = @_;
131 2 ▶ return $self->sendError( $req, 'There is no subkey for "newRSAKey"', 400 )
Branch: 1 branch
BranchTrueFalseTotal
if @others 0 2 2
132 if (@others);
133  
134 2 my $key_size = $self->defaultNewKeySize;
135 2 my $query = $req->jsonBodyToObj;
136 2 my $password = $query->{password};
137  
138 2 my $keys =
139 Lemonldap::NG::Common::Util::Crypto::genRsaKey( $key_size, $password );
140  
141 2 return $self->sendJSONresponse( $req, $keys );
142 }
143  
144 # 35 - New EC key pair on demand
145 # --------------------------
146  
147 ##@method public PSGI-JSON-response newEcKeys($req)
148 # Return a hashref containing private and public keys
149 #
150 #@param $req Lemonldap::NG::Common::PSGI::Request object
151 #@return PSGI JSON response
152 0 ▶sub newEcKeys {
Subroutine newEcKeys: not called CC=1 SCAR=6.9
Pod undocumented
153 0 my ( $self, $req, @others ) = @_;
154  
155 0 my $keys = Lemonldap::NG::Common::Util::Crypto::genEcKey('prime256v1');
156 0 return $self->sendJSONresponse( $req, $keys );
157 }
158  
159 # This function does the dirty X509 work,
160 # mostly copied from IO::Socket::SSL::Utils
161 # and adapter to work on old platforms (CentOS7)
162  
163 2 ▶sub _generateX509 {
Subroutine _generateX509: called CC=3 SCAR=11.8
164 2 my ( $self, $password, $type ) = @_;
165 2 my $conf = $self->confAcc->getConf();
166 2 ▶ my $portal_uri = new URI::URL( $conf->{portal} || "http://localhost" );
Condition50%1/2
$conf->{'portal'} || 'http://localhost'
l!l
20
MC/DC0%0/1
$conf->{'portal'} || 'http://localhost'
$conf->{'portal'}
Truth table50%1/2
A
A= $conf->{'portal'}
Aresult
11
00
167 2 my $portal_host = $portal_uri->host;
168  
169 2 ▶ if ( $type eq "EC" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'EC') { } 0 2 2
170 0 my $curve = $self->defaultCurve;
171 0 return Lemonldap::NG::Common::Util::Crypto::genEcCertKey( $curve,
172 $password, $portal_host );
173 }
174 else {
175 2 my $key_size = $self->defaultNewKeySize;
176 2 return Lemonldap::NG::Common::Util::Crypto::genCertKey( $key_size,
177 $password, $portal_host );
178 }
179 }
180  
181 # Sending a test Email
182 # --------------------
183  
184 ##@method public PSGI-JSON-response sendTestMail($req)
185 # Sends a test email to the provided address
186 # The posted data must contain a JSON object containing
187 # {"dest":"target@example.com"}
188 #
189 #@param $req Lemonldap::NG::Common::PSGI::Request object
190 #@return PSGI JSON response
191 0 ▶sub sendTestMail {
Subroutine sendTestMail: not called CC=6 SCAR=37.4
Pod undocumented
192 0 my ( $self, $req, @others ) = @_;
193 0 ▶ return $self->sendError( $req, 'There is no subkey for "sendTestMail"',
Branch: 1 branch
BranchTrueFalseTotal
if @others 0 0 0
194 400 )
195 if (@others);
196 0 my $dest = $req->jsonBodyToObj->{dest};
197 0 ▶ unless ($dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dest) 0 0 0
198 0 $self->logger->debug("Missing dest parameter for sending test mail");
199 0 return $self->sendJSONresponse(
200 $req,
201 {
202 success => \0,
203 error => "You must provide an email address"
204 }
205 );
206 }
207 0 my $conf = $self->confAcc->getConf();
208  
209 # Try to send test Email
210 0 $self->logger->info("Sending test email to $dest");
211 0 eval {
212 0 Lemonldap::NG::Common::EmailTransport::sendTestMail( $conf, $dest );
213 };
214 0 my $error = $@;
215 0 ▶ my $success = ( $error ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
216  
217 0 ▶ $self->logger->debug("Email was sent") unless $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 0 0 0
218 0 ▶ return $self->sendJSONresponse(
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
219 $req,
220 {
221 success => \$success,
222 ( $error ? ( error => $error ) : () )
223 }
224 );
225 }
226  
227 # 36 - URL File loader
228 # ---------------
229  
230 ##@method public PSGI-JSON-response prx()
231 # Load file using posted URL and return its content
232 #
233 #@return PSGI JSON response
234 0 ▶sub prx {
Subroutine prx: not called CC=6 SCAR=37.4
Pod undocumented
235 0 my ( $self, $req, @others ) = @_;
236 0 ▶ return $self->sendError( $req, 'There is no subkey for "prx"', 400 )
Branch: 1 branch
BranchTrueFalseTotal
if @others 0 0 0
237 if (@others);
238 0 my $query = $req->jsonBodyToObj;
239 return $self->sendError( $req, 'Missing parameter', 400 )
240 0 ▶ unless ( $query->{url} );
Branch: 1 branch
BranchTrueFalseTotal
unless $query->{"url"} 0 0 0
241 return $self->sendError( $req, 'Bad parameter', 400 )
242 0 ▶ unless ( $query->{url} =~ m#^(?:f|ht)tps?://\w# );
Branch: 1 branch
BranchTrueFalseTotal
unless $query->{"url"} =~ m[^(?:f|ht)tps?://\w] 0 0 0
243 0 $self->ua->timeout(10);
244  
245 0 my $response = $self->ua->get( $query->{url} );
246 0 ▶ unless ( $response->code == 200 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->code == 200) 0 0 0
247 return $self->sendError(
248 $req,
249 "Received unexpected HTTP status code "
250 . $response->code . " ("
251 . $response->message
252 . ") when trying to load content from "
253 . $query->{url},
254 0 400
255 );
256 }
257 0 ▶ unless ( $response->header('Content-Type') =~
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->header("Content-Type") =~ m[^(?:application/json|(?:application|text)/.*xml).*$]) 0 0 0
258 m#^(?:application/json|(?:application|text)/.*xml).*$# )
259 {
260 0 return $self->sendError( $req,
261 'Content refused for security reason (neither XML or JSON)', 400 );
262 }
263 0 return $self->sendJSONresponse( $req, { content => $response->content } );
264 }
265  
266 ######################
267 # IV. Upload methods #
268 ######################
269  
270 # In this section, 3 methods:
271 # - getConfByNum: override SUPER method to be able to use Zero
272 # - newConf(), load a new configuration and invokes reloadUrls
273 # - newRawConf(): restore a saved conf
274  
275 246 ▶sub getConfByNum {
Subroutine getConfByNum: called CC=6 SCAR=18.5
Pod undocumented
276 246 my ( $self, $cfgNum, @args ) = @_;
277 246 ▶ unless ( $self->currentConf
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->currentConf and %{$self->currentConf;} and $cfgNum == $self->currentConf->{"cfgNum"}) 39 207 246
Condition83%5/6
$self->currentConf and %{$self->currentConf;} and $cfgNum == $self->currentConf->{"cfgNum"}
!ll&&!rl&&r
318207
$self->currentConf and %{$self->currentConf;}
!ll&&!rl&&r
031215
MC/DC66%2/3
$self->currentConf and %{$self->currentConf;} and $cfgNum == $self->currentConf->{"cfgNum"}
$self->currentConf %{$self->currentConf;} $cfgNum == $self->currentConf->{"cfgNum"}
Truth table75%3/4
A and B and C
A= $self->currentConfB= %{$self->currentConf;}C= $cfgNum == $self->currentConf->{"cfgNum"}
ABCresult
0XX0
10X0
1100
1111
278 246 and %{ $self->currentConf }
279 and $cfgNum == $self->currentConf->{cfgNum} )
280 {
281 39 my $tmp;
282 39 ▶ if ( $cfgNum == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfgNum == 0) { } 0 39 39
283 0 require Lemonldap::NG::Manager::Conf::Zero;
284 0 $tmp = Lemonldap::NG::Manager::Conf::Zero::zeroConf();
285 0 $self->currentConf($tmp);
286 }
287 else {
288 39 $tmp = $self->SUPER::getConfByNum( $cfgNum, @args );
289 39 ▶ return undef unless ( defined $tmp );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmp 1 38 39
290 }
291 }
292 245 return $cfgNum;
293 }
294  
295 ## @method PSGI-JSON-response newConf($req)
296 # Call Lemonldap::NG::Manager::Conf::Parser to parse new configuration and store
297 # it
298 #
299 #@param $req Lemonldap::NG::Common::PSGI::Request
300 #@return PSGI JSON response
301 16 ▶sub newConf {
Subroutine newConf: called CC=26 SCAR=35.1
Pod undocumented
302 16 my ( $self, $req, @other ) = @_;
303 16 ▶ return $self->sendError( $req, 'There is no subkey for "newConf"', 400 )
Branch: 1 branch
BranchTrueFalseTotal
if @other 0 16 16
304 if (@other);
305  
306 # Body must be json
307 16 my $new = $req->jsonBodyToObj;
308 16 ▶ return $self->sendError( $req, undef, 400 ) unless ( defined $new );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 0 16 16
309  
310 # Verify that cfgNum has been sent
311 16 ▶ return $self->sendError( $req, "Missing configuration number", 400 )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req->params("cfgNum") 0 16 16
312 unless ( defined $req->params('cfgNum') );
313  
314 # # Verify that cfgDate has been sent
315 # return $self->sendError( $req, "Missing configuration date", 400 )
316 # unless ( defined $req->params('cfgDate') );
317  
318 # Set current conf to cfgNum
319 16 ▶ return $self->sendError(
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->getConfByNum($req->params("cfgNum")) 0 16 16
320 $req,
321 "Configuration "
322 . $req->params('cfgNum')
323 . " not available "
324 . $Lemonldap::NG::Common::Conf::msg,
325 400
326 ) unless ( defined $self->getConfByNum( $req->params('cfgNum') ) );
327  
328 # Parse new conf
329 16 require Lemonldap::NG::Manager::Conf::Parser;
330 16 my $parser = Lemonldap::NG::Manager::Conf::Parser->new(
331 { tree => $new, refConf => $self->currentConf, req => $req } );
332  
333 # If ref conf isn't last conf, consider conf changed
334 16 my $currentCfgNum = $self->confAcc->lastCfg;
335 16 ▶ $req->error($Lemonldap::NG::Common::Conf::msg)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $currentCfgNum 0 16 16
336 unless ( defined $currentCfgNum );
337 16 ▶ return $self->sendError( $req, undef, 400 ) if ( $req->error );
Branch: 1 branch
BranchTrueFalseTotal
if $req->error 0 16 16
338 16 my $currentConf =
339 $self->confAcc->getConf(
340 { CfgNum => $currentCfgNum, raw => 1, noCache => 1 } );
341 16 my $currentCfgDate = $currentConf->{cfgDate};
342 16 $self->logger->debug(
343 "Current CfgNum/cfgDate: $currentCfgNum/$currentCfgDate");
344 16 ▶ $parser->confChanged(1)
Branch: 1 branch
BranchTrueFalseTotal
if $currentCfgNum ne $req->params("cfgNum") or $req->params("cfgDate") and $req->params("cfgDate") ne $currentCfgDate 0 16 16
Condition33%2/6
$currentCfgNum ne $req->params("cfgNum") or $req->params("cfgDate") and $req->params("cfgDate") ne $currentCfgDate
l!l&&r!l&&!r
0016
$req->params("cfgDate") and $req->params("cfgDate") ne $currentCfgDate
!ll&&!rl&&r
1600
MC/DC0%0/3
$currentCfgNum ne $req->params("cfgNum") or $req->params("cfgDate") and $req->params("cfgDate") ne $currentCfgDate
$currentCfgNum ne $req->params("cfgNum") $req->params("cfgDate") $req->params("cfgDate") ne $currentCfgDate
Truth table25%1/4
A or B and C
A= $currentCfgNum ne $req->params("cfgNum")B= $req->params("cfgDate")C= $req->params("cfgDate") ne $currentCfgDate
ABCresult
1XX1
0111
00X0
0100
345 if ( $currentCfgNum ne $req->params('cfgNum')
346 || $req->params('cfgDate')
347 && $req->params('cfgDate') ne $currentCfgDate );
348  
349 16 my $res = { result => $parser->check( $self->p ) };
350  
351 # "message" fields: note that words enclosed by "__" (__word__) will be
352 # translated
353 $res->{details}->{'__errors__'} = $parser->{errors}
354 16 ▶ if ( @{ $parser->{errors} } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$parser->{"errors"};} 1 15 16
355 16 ▶ unless ( @{ $parser->{errors} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$parser->{"errors"};}) 15 1 16
356 15 ▶ if ( @{ $parser->{needConfirmation} } && !$req->params('force') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$parser->{"needConfirmation"};} and not $req->params("force")) 1 14 15
Condition100%3/3
@{$parser->{"needConfirmation"};} and not $req->params("force")
!ll&&!rl&&r
1311
MC/DC100%2/2
@{$parser->{"needConfirmation"};} and not $req->params("force")
@{$parser->{"needConfirmation"};} not $req->params("force")
Truth table100%3/3
A and not(B)
A= @{$parser->{"needConfirmation"};}B= not $req->params("force")
ABresult
0X0
100
111
357 1 $res->{needConfirm} = 1;
358 $res->{details}->{'__needConfirmation__'} =
359 1 $parser->{needConfirmation};
360 }
361 15 $res->{message} = $parser->{message};
362 15 foreach my $t (qw(warnings changes)) {
363 $res->{details}->{ '__' . $t . '__' } = $parser->$t
364 30 ▶ if ( @{ $parser->$t } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$parser->$t;} 27 3 30
365 }
366 }
367 16 ▶ if ( $res->{result} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"result"}) 12 4 16
368 12 my %args;
369 12 ▶ $args{force} = 1 if ( $req->params('force') );
Branch: 1 branch
BranchTrueFalseTotal
if $req->params("force") 3 9 12
370 12 ▶ if ( $req->params('cfgDate') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->params("cfgDate")) 0 12 12
371 0 $args{cfgDate} = $req->params('cfgDate');
372 0 $args{currentCfgDate} = $currentCfgDate;
373 }
374 12 my $s = UNKNOWN_ERROR;
375 $s = $self->confAcc->saveConf( $parser->newConf, %args )
376 12 ▶ unless ( @{ $parser->{needConfirmation} } && !$args{force} );
Branch: 1 branch
BranchTrueFalseTotal
unless @{$parser->{"needConfirmation"};} and not $args{"force"} 11 1 12
Condition100%3/3
@{$parser->{"needConfirmation"};} and not $args{"force"}
!ll&&!rl&&r
1011
MC/DC100%2/2
@{$parser->{"needConfirmation"};} and not $args{"force"}
@{$parser->{"needConfirmation"};} not $args{"force"}
Truth table100%3/3
A and not(B)
A= @{$parser->{"needConfirmation"};}B= not $args{"force"}
ABresult
0X0
100
111
377 12 ▶ if ( $s > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s > 0) { } 11 1 12
378 11 $self->auditLog(
379 $req,
380 message => (
381 'User ' . $self->p->userId($req) . " has stored conf $s"
382 ),
383 code => "CONF_STORED",
384 user => $self->p->userId($req),
385 cfgNum => $s,
386 );
387  
388 11 $res->{result} = 1;
389 11 $res->{cfgNum} = $s;
390 11 ▶ if ( my $status = $self->applyConf( $parser->newConf ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $status = $self->applyConf($parser->newConf)) 11 0 11
391 0 push @{ $res->{details}->{__applyResult__} },
392 { message => "$_: $status->{$_}" }
393 11 foreach ( keys %$status );
394 }
395 }
396 else {
397 1 $self->auditLog(
398 $req,
399 message => (
400 'Saving attempt rejected, asking for confirmation to '
401 . $self->p->userId($req)
402 ),
403 code => "CONF_REJECTED",
404 user => $self->p->userId($req),
405 );
406  
407 1 $res->{result} = 0;
408 1 ▶ if ( $s == CONFIG_WAS_CHANGED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s == -1) { } 0 1 1
409 0 $res->{needConfirm} = 1;
410 0 ▶ $res->{details}->{'__needConfirmation__'} ||= [];
Condition0%0/2
$res->{"details"}{"__needConfirmation__"} ||= []
l!l
00
MC/DC0%0/1
$res->{"details"}{"__needConfirmation__"} ||= []
$res->{"details"}{"__needConfirmation__"}
Truth table0%0/2
A
A= $res->{"details"}{"__needConfirmation__"}
Aresult
11
00
411 0 push @{ $res->{details}->{'__needConfirmation__'} },
412 { message => '__newCfgAvailableWarning__' };
413 }
414 else {
415 1 $res->{message} = $Lemonldap::NG::Common::Conf::msg;
416 }
417 }
418 }
419 16 return $self->sendJSONresponse( $req, $res );
420 }
421  
422 ## @method PSGI-JSON-response newRawConf($req)
423 # Store directly raw configuration
424 #
425 #@param $req Lemonldap::NG::Common::PSGI::Request
426 #@return PSGI JSON response
427 1 ▶sub newRawConf {
Subroutine newRawConf: called CC=12 SCAR=28.7
Pod undocumented
428 1 my ( $self, $req, @other ) = @_;
429 1 ▶ return $self->sendError( $req, 'There is no subkey for "newConf"', 400 )
Branch: 1 branch
BranchTrueFalseTotal
if @other 0 1 1
430 if (@other);
431  
432 # Body must be json
433 1 my $new = $req->jsonBodyToObj;
434 1 ▶ unless ( defined($new) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new) 0 1 1
435 0 return $self->sendError( $req, undef, 400 );
436 }
437  
438 1 require Lemonldap::NG::Manager::Conf::Parser;
439 1 my $parser = Lemonldap::NG::Manager::Conf::Parser->new( {
440 refConf => $self->currentConf,
441 newConf => $new,
442 req => $req,
443 }
444 );
445  
446 1 $parser->confChanged(1);
447  
448 1 my $res = { result => $parser->check( $self->p ) };
449  
450 # "message" fields: note that words enclosed by "__" (__word__) will be
451 # translated
452 $res->{details}->{'__errors__'} = $parser->{errors}
453 1 ▶ if ( @{ $parser->{errors} } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$parser->{"errors"};} 0 1 1
454 1 ▶ unless ( @{ $parser->{errors} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$parser->{"errors"};}) 1 0 1
455 1 ▶ if ( @{ $parser->{needConfirmation} } && !$req->params('force') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$parser->{"needConfirmation"};} and not $req->params("force")) 0 1 1
Condition33%1/3
@{$parser->{"needConfirmation"};} and not $req->params("force")
!ll&&!rl&&r
100
MC/DC0%0/2
@{$parser->{"needConfirmation"};} and not $req->params("force")
@{$parser->{"needConfirmation"};} not $req->params("force")
Truth table33%1/3
A and not(B)
A= @{$parser->{"needConfirmation"};}B= not $req->params("force")
ABresult
0X0
100
111
456 0 $res->{needConfirm} = 1;
457 $res->{details}->{'__needConfirmation__'} =
458 0 $parser->{needConfirmation};
459 }
460 1 $res->{message} = $parser->{message};
461 1 foreach my $t (qw(warnings changes)) {
462 $res->{details}->{ '__' . $t . '__' } = $parser->$t
463 2 ▶ if ( @{ $parser->$t } );
Branch: 1 branch
BranchTrueFalseTotal
if @{$parser->$t;} 1 1 2
464 }
465 }
466 1 ▶ if ( $res->{result} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"result"}) 1 0 1
467  
468 # When uploading a new conf, always force it since cfgNum has a few
469 # chances to be equal to last config cfgNum
470 1 my $s = $self->confAcc->saveConf( $new, force => 1 );
471 1 ▶ if ( $s > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s > 0) { } 1 0 1
472 1 $self->auditLog(
473 $req,
474 message => (
475 'User '
476 . $self->p->userId($req)
477 . " has stored (raw) conf $s"
478 ),
479 code => "CONF_STORED_RAW",
480 user => $self->p->userId($req),
481 cfgNum => $s,
482 );
483 1 $res->{result} = 1;
484 1 $res->{cfgNum} = $s;
485 }
486 else {
487 0 $self->auditLog(
488 $req,
489 message => (
490 'Raw saving attempt rejected, asking for confirmation to '
491 . $self->p->userId($req)
492 ),
493 code => "CONF_REJECTED_RAW",
494 user => $self->p->userId($req),
495 );
496 0 $res->{result} = 0;
497 0 ▶ $res->{needConfirm} = 1 if ( $s == CONFIG_WAS_CHANGED );
Branch: 1 branch
BranchTrueFalseTotal
if $s == -1 0 0 0
498 0 $res->{message} .= '__needConfirmation__';
499 }
500 }
501 1 return $self->sendJSONresponse( $req, $res );
502 }
503  
504 2 ▶sub diff {
Subroutine diff: called CC=6 SCAR=18.0
Pod undocumented
505 2 my ( $self, $req, @path ) = @_;
506 2 ▶ return $self->sendError( $req, 'to many arguments in path info', 400 )
Branch: 1 branch
BranchTrueFalseTotal
if @path 0 2 2
507 if (@path);
508 2 my @cfgNum =
509 ( scalar( $req->param('conf1') ), scalar( $req->param('conf2') ) );
510 2 my @conf;
511 2 $self->logger->debug(" Loading confs");
512  
513 # Load the 2 configurations
514 2 ▶ for ( my $i = 0 ; $i < 2 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 2) 4 2 6
515 4 ▶ if ( %{ $self->currentConf }
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->currentConf;} and $cfgNum[$i] == $self->currentConf->{'cfgNum'}) { } 2 2 4
Condition66%2/3
%{$self->currentConf;} and $cfgNum[$i] == $self->currentConf->{"cfgNum"}
!ll&&!rl&&r
022
MC/DC50%1/2
%{$self->currentConf;} and $cfgNum[$i] == $self->currentConf->{"cfgNum"}
%{$self->currentConf;} $cfgNum[$i] == $self->currentConf->{"cfgNum"}
Truth table66%2/3
A and B
A= %{$self->currentConf;}B= $cfgNum[$i] == $self->currentConf->{"cfgNum"}
ABresult
0X0
100
111
516 and $cfgNum[$i] == $self->currentConf->{cfgNum} )
517 {
518 2 $conf[$i] = $self->currentConf;
519 }
520 else {
521 2 $conf[$i] = $self->confAcc->getConf(
522 { cfgNum => $cfgNum[$i], raw => 1, noCache => 1 } );
523 2 ▶ return $self->sendError(
Branch: 1 branch
BranchTrueFalseTotal
unless $conf[$i] 0 2 2
524 $req,
525 "Configuration $cfgNum[$i] not available $Lemonldap::NG::Common::Conf::msg",
526 400
527 ) unless ( $conf[$i] );
528 }
529 }
530 2 require Lemonldap::NG::Manager::Conf::Diff;
531 2 return $self->sendJSONresponse(
532 $req,
533 [
534 $self->Lemonldap::NG::Manager::Conf::Diff::diff(
535 $conf[0], $conf[1]
536 )
537 ]
538 );
539 }
540  
541 28 1;
« blib/lib/Lemonldap/NG/Manager/Build/OpenApi.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Notifications.pm »