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

filter: Statementstmt 93.9% 31 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.4% 42 / 44 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Lemonldap/NG/Manager/Cli/Lib.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Build/CTrees.pm »
1 package Lemonldap::NG::Manager::Build;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Lemonldap::NG::Manager::Build::Attributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Lemonldap::NG::Manager::Build::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Lemonldap::NG::Manager::Build::CTrees;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Lemonldap::NG::Manager::Build::PortalConstants;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Lemonldap::NG::Manager::Conf::Zero;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Regexp::Common 'URI';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 0 ▶use Regexp::Assemble;
Subroutine BEGIN: called CC=1 SCAR=2.6
14 use JSON;
15 use Getopt::Std;
16 use IO::String;
17  
18 my %peOkValues = (
19 PE_2FWAIT => 'Wait for second factor',
20 PE_IDPCHOICE => 'Prompt to choose identity provider',
21 PE_SENDRESPONSE => 'Send response given in $req->response',
22 PE_INFO => 'Prompt to display $req->info',
23 PE_REDIRECT => 'Redirect user to $req->urldc',
24 PE_DONE => "Don't launch next steps",
25 );
26  
27 has structFile => ( isa => 'Str', is => 'ro', required => 1 );
28 has confTreeFile => ( isa => 'Str', is => 'ro', required => 1 );
29 has managerConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
30 has managerAttributesFile => ( isa => 'Str', is => 'ro', required => 1 );
31 has defaultValuesFile => ( isa => 'Str', is => 'ro', required => 1 );
32 has confConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
33 has firstLmConfFile => ( isa => 'Str', is => 'ro', required => 1 );
34 has reverseTreeFile => ( isa => 'Str', is => 'ro', required => 1 );
35 has portalConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
36 has handlerStatusConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
37 has docConstantsFile => ( isa => 'Str', is => 'ro', required => 1 );
38  
39 my @managerAttrKeys = qw(keyTest keyMsgFail select type test msgFail default);
40 my $format = 'Creating %-69s: ';
41 my $reIgnoreKeys = qr/^$/;
42 my $module = __PACKAGE__;
43  
44 my @angularScopeVars;
45 my @bool;
46 my @arrayParam;
47 my @cnodesKeys;
48 my %cnodesRe;
49 my @ignoreKeys;
50 my $ignoreKeys;
51 my $mainTree;
52 my @sessionTypes;
53 my @simpleHashKeys;
54 my @doubleHashKeys;
55 my $authParameters;
56 my $issuerParameters;
57 my $samlServiceParameters;
58 my $oidcServiceParameters;
59 my $casServiceParameters = [];
60 my $defaultValues;
61  
62 my $attributes = Lemonldap::NG::Manager::Build::Attributes::attributes();
63 my $jsonEnc = JSON->new()->allow_nonref;
64 $jsonEnc->canonical(1);
65  
66 $Data::Dumper::Sortkeys = sub {
67 my ($hash) = @_;
68 return [
69 ( defined $hash->{id} ? ('id') : () ),
70 ( defined $hash->{title} ? ( 'title', ) : () ),
71 (
72 grep { /^(?:id|title)$/ ? 0 : 1 }
73 sort {
74 return 1
75 if ( $a =~ /node/ and $b !~ /node/ );
76 return -1 if ( $b =~ /node/ );
77 lc($a) cmp lc($b);
78 } keys %$hash
79 )
80 ];
81 };
82  
83 $Data::Dumper::Deparse = 1;
84 $Data::Dumper::Deepcopy = 1;
85  
86 sub run {
87 my $self = shift;
88 $self = $module->new(@_) unless ref $self;
89  
90 # 1. confTree.js
91 printf STDERR $format, $self->confTreeFile;
92 $mainTree = Lemonldap::NG::Manager::Build::CTrees::cTrees();
93  
94 my $script = 'function templates(tpl,key) {
95 var ind;
96 var scalarTemplate = function(r) {
97 return {
98 "id": tpl+"s/"+(ind++),
99 "title": r,
100 "get": tpl+"s/"+key+"/"+r
101 };
102 };
103 switch(tpl){
104 ';
105  
106 my $reverseScanResult =
107 $self->reverseScan( Lemonldap::NG::Manager::Build::Tree::tree(),
108 '', {}, $mainTree );
109  
110 # To build confTree.js, each special node is scanned from
111 # Lemonldap::NG::Manager::Build::CTrees
112 foreach my $node ( sort keys %$mainTree ) {
113 @cnodesKeys = ();
114 my $jsonTree = [];
115 $self->scanTree( $mainTree->{$node}, $jsonTree, '__KEY__', '' );
116 $jsonEnc->pretty(1);
117 my $tmp = $jsonEnc->encode($jsonTree);
118 $tmp =~ s!"__KEY__!tpl+"s/"+key+"/"+"!mg;
119 $tmp =~ s/"(true|false)"/$1/sg;
120 $tmp =~ s/:\s*"(\d+)"\s*(["\}])/:$1$2/sg;
121 $script .= " case '$node':
122 return $tmp;
123 ";
124  
125 # Second step, Manager/Constants.pm file will contain data issued from
126 # this scan
127 my $ra = Regexp::Assemble->new;
128  
129 # Build $oidcOPMetaDataNodeKeys, $samlSPMetaDataNodeKeys,...
130 foreach my $r (@cnodesKeys) {
131 $ra->add($r);
132 }
133 $cnodesRe{$node} = $ra->as_string;
134  
135 push @ignoreKeys, $node;
136 }
137 $script .= " default:\n return [];\n }\n}";
138 open F, ">", $self->confTreeFile or die $!;
139 print F $script;
140 close F;
141 print STDERR "done\n";
142 my $ra = Regexp::Assemble->new;
143 foreach my $re (@ignoreKeys) {
144 $ra->add($re);
145 }
146 $ignoreKeys = $ra->as_string;
147 $reIgnoreKeys = $ra->re;
148  
149 # Reinitialize $defaultValues
150 $defaultValues = {};
151  
152 # 2. struct.json
153 printf STDERR $format, $self->structFile;
154 $mainTree = Lemonldap::NG::Manager::Build::Tree::tree();
155 my $jsonTree = [];
156 $self->scanTree( $mainTree, $jsonTree, '', '' );
157 $script = "\n\nfunction setScopeVars(scope) {\n";
158 foreach my $v (@angularScopeVars) {
159 $script .=
160 " scope.$v->[0] = scope$v->[1];\n scope.getKey(scope.$v->[0]);\n";
161 }
162 $script .= "}";
163 open F, ">>", $self->confTreeFile || die $!;
164 print F $script;
165 close F;
166 open F, ">", $self->structFile || die $!;
167 $jsonEnc->pretty(0);
168 my $tmp = $jsonEnc->encode($jsonTree);
169 $tmp =~ s/"(true|false)"/$1/sg;
170 $tmp =~ s/:\s*"(\d+)"\s*(["\}])/:$1$2/sg;
171 print F $tmp;
172 close F;
173 print STDERR "done\n";
174 $tmp = undef;
175  
176 printf STDERR $format, $self->managerConstantsFile;
177  
178 open F, ">", $self->managerConstantsFile or die($!);
179 my $exportedVars = '$'
180 . join( 'Keys $',
181 'simpleHash', 'doubleHash', 'specialNode', sort keys %cnodesRe )
182 . 'Keys $specialNodeHash $authParameters $issuerParameters $samlServiceParameters $oidcServiceParameters $casServiceParameters';
183 print F <<EOF;
184 # This file is generated by $module. Don't modify it by hand
185 package Lemonldap::NG::Common::Conf::ReConstants;
186  
187 use strict;
188 use Exporter 'import';
189 use base qw(Exporter);
190  
191 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
192  
193 our %EXPORT_TAGS = ( 'all' => [qw($exportedVars)] );
194 our \@EXPORT_OK = ( \@{ \$EXPORT_TAGS{'all'} } );
195 our \@EXPORT = ( \@{ \$EXPORT_TAGS{'all'} } );
196  
197 our \$specialNodeHash = {
198 virtualHosts => [qw(exportedHeaders locationRules post vhostOptions)],
199 samlIDPMetaDataNodes => [qw(samlIDPMetaDataXML samlIDPMetaDataExportedAttributes samlIDPMetaDataOptions)],
200 samlSPMetaDataNodes => [qw(samlSPMetaDataXML samlSPMetaDataExportedAttributes samlSPMetaDataOptions samlSPMetaDataMacros)],
201 oidcOPMetaDataNodes => [qw(oidcOPMetaDataJSON oidcOPMetaDataJWKS oidcOPMetaDataOptions oidcOPMetaDataExportedVars)],
202 oidcRPMetaDataNodes => [qw(oidcRPMetaDataOptions oidcRPMetaDataExportedVars oidcRPMetaDataOptionsExtraClaims oidcRPMetaDataMacros oidcRPMetaDataScopeRules)],
203 casSrvMetaDataNodes => [qw(casSrvMetaDataOptions casSrvMetaDataExportedVars)],
204 casAppMetaDataNodes => [qw(casAppMetaDataOptions casAppMetaDataExportedVars casAppMetaDataMacros)],
205 keyNodes => [qw(keys)],
206 };
207  
208 EOF
209  
210 # Reinitialize $attributes
211 $attributes = Lemonldap::NG::Manager::Build::Attributes::attributes();
212  
213 $ra = Regexp::Assemble->new;
214 foreach (@doubleHashKeys) {
215 $ra->add($_);
216 }
217 print F "our \$doubleHashKeys = '" . $ra->as_string . "';\n";
218 $ra = Regexp::Assemble->new;
219 foreach (@simpleHashKeys) {
220 $ra->add($_);
221 }
222 print F "our \$simpleHashKeys = '"
223 . $ra->as_string . "';\n"
224 . "our \$specialNodeKeys = '${ignoreKeys}s';\n";
225 foreach ( sort keys %cnodesRe ) {
226 print F "our \$${_}Keys = '$cnodesRe{$_}';\n";
227 }
228 print F "\n";
229  
230 foreach (qw(authParameters issuerParameters)) {
231 $tmp = "our \$$_ = {\n";
232 no strict 'refs';
233 foreach my $k ( sort keys %$$_ ) {
234 my $v = $$_->{$k};
235 $tmp .= " $k => [qw(" . join( ' ', @$v ) . ")],\n";
236 }
237 print F "$tmp};\n";
238 }
239 foreach (qw(samlServiceParameters oidcServiceParameters)) {
240 no strict 'refs';
241 $tmp = "our \$$_ = [qw(" . join( ' ', @$$_ ) . ")];\n";
242 print F "$tmp";
243 }
244  
245 print F "\n1;\n";
246 close F;
247 print STDERR "done\n";
248  
249 printf STDERR $format, $self->defaultValuesFile;
250 $defaultValues->{locationRules} = $attributes->{locationRules}->{default};
251 foreach ( keys %$attributes ) {
252 if ( not /(?:MetaData|vhost)/
253 and $attributes->{$_}->{default} )
254 {
255 $defaultValues->{$_} = $attributes->{$_}->{default};
256 }
257 }
258 my $defaultAttr = mydump( $defaultValues, 'defaultValues' );
259 $defaultAttr = "# This file is generated by $module. Don't modify it by hand
260 package Lemonldap::NG::Common::Conf::DefaultValues;
261  
262 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
263  
264 $defaultAttr}
265  
266 1;
267 ";
268  
269 my $dst;
270  
271 eval {
272 require Perl::Tidy;
273 Perl::Tidy::perltidy(
274 source => IO::String->new($defaultAttr),
275 destination => \$dst
276 );
277 };
278 $dst = $defaultAttr if ($@);
279  
280 open( F, ">", $self->defaultValuesFile ) or die($!);
281 print F $dst;
282 close F;
283 print STDERR "done\n";
284  
285 printf STDERR $format, $self->confConstantsFile;
286 $ra = Regexp::Assemble->new;
287 foreach ( @simpleHashKeys, @doubleHashKeys, sort keys %cnodesRe ) {
288 $ra->add($_);
289 }
290 foreach ( qw(
291 exportedHeaders locationRules post vhostOptions keys
292 samlIDPMetaDataXML samlIDPMetaDataExportedAttributes
293 samlIDPMetaDataURL samlSPMetaDataURL
294 samlIDPMetaDataOptions samlSPMetaDataXML
295 samlSPMetaDataExportedAttributes samlSPMetaDataMacros
296 samlSPMetaDataOptions oidcOPMetaDataJSON
297 oidcOPMetaDataJWKS oidcOPMetaDataOptions
298 oidcOPMetaDataExportedVars oidcRPMetaDataOptions
299 oidcRPMetaDataExportedVars oidcRPMetaDataOptionsExtraClaims
300 oidcRPMetaDataMacros oidcRPMetaDataScopeRules
301 casAppMetaDataExportedVars casAppMetaDataOptions casAppMetaDataMacros
302 casSrvMetaDataExportedVars casSrvMetaDataOptions
303 )
304 )
305 {
306 $ra->add($_);
307 }
308  
309 my $sessionTypes = join( "', '", @sessionTypes );
310 my $confConstants =
311 "our \$hashParameters = qr/^" . $ra->as_string . "\$/;\n";
312 $ra = Regexp::Assemble->new;
313 foreach (@arrayParam) {
314 $ra->add($_);
315 }
316  
317 # Not in Tree.pm
318 foreach (qw(mySessionAuthorizedRWKeys)) {
319 $ra->add($_);
320 }
321 $confConstants .=
322 "our \$arrayParameters = qr/^" . $ra->as_string . "\$/;\n";
323 $ra = Regexp::Assemble->new;
324 foreach (@bool) {
325 $ra->add($_);
326 }
327 $confConstants .= "our \$boolKeys = qr/^" . $ra->as_string . "\$/;\n";
328 open( F, ">", $self->confConstantsFile ) or die($!);
329 print F <<EOF;
330 # This file is generated by $module. Don't modify it by hand
331 package Lemonldap::NG::Common::Conf::Constants;
332  
333 use strict;
334 use Exporter 'import';
335 use base qw(Exporter);
336  
337 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
338  
339 # CONSTANTS
340  
341 use constant CONFIG_WAS_CHANGED => -1;
342 use constant UNKNOWN_ERROR => -2;
343 use constant DATABASE_LOCKED => -3;
344 use constant UPLOAD_DENIED => -4;
345 use constant SYNTAX_ERROR => -5;
346 use constant DEPRECATED => -6;
347 use constant DEFAULTCONFFILE => "/usr/local/lemonldap-ng/etc/lemonldap-ng.ini";
348 use constant DEFAULTSECTION => "all";
349 use constant CONFSECTION => "configuration";
350 use constant PORTALSECTION => "portal";
351 use constant HANDLERSECTION => "handler";
352 use constant MANAGERSECTION => "manager";
353 use constant SESSIONSEXPLORERSECTION => "sessionsExplorer";
354 use constant APPLYSECTION => "apply";
355  
356 # Default configuration backend
357 use constant DEFAULTCONFBACKEND => "File";
358 use constant DEFAULTCONFBACKENDOPTIONS => (
359 dirName => '/usr/local/lemonldap-ng/data/conf',
360 );
361 $confConstants
362 our \@sessionTypes = ( '$sessionTypes' );
363  
364 sub NO {qr/^(?:off|no|0)?\$/i}
365  
366 our %EXPORT_TAGS = (
367 'all' => [
368 qw(
369 CONFIG_WAS_CHANGED
370 UNKNOWN_ERROR
371 DATABASE_LOCKED
372 UPLOAD_DENIED
373 SYNTAX_ERROR
374 DEPRECATED
375 DEFAULTCONFFILE
376 DEFAULTSECTION
377 CONFSECTION
378 PORTALSECTION
379 HANDLERSECTION
380 MANAGERSECTION
381 SESSIONSEXPLORERSECTION
382 APPLYSECTION
383 DEFAULTCONFBACKEND
384 DEFAULTCONFBACKENDOPTIONS
385 NO
386 \$hashParameters
387 \$arrayParameters
388 \@sessionTypes
389 \$boolKeys
390 )
391 ]
392 );
393 our \@EXPORT_OK = ( \@{ \$EXPORT_TAGS{'all'} } );
394 our \@EXPORT = ( \@{ \$EXPORT_TAGS{'all'} } );
395  
396 1;
397 EOF
398 close F;
399 print STDERR "done\n";
400  
401 printf STDERR $format, $self->managerAttributesFile;
402 my $managerAttr = {
403 map {
404 my @r;
405 foreach my $f (@managerAttrKeys) {
406 push @r, $f, $attributes->{$_}->{$f}
407 if ( defined $attributes->{$_}->{$f} );
408 }
409 ( $_ => {@r} );
410 } keys(%$attributes)
411 };
412 $managerAttr = mydump( $managerAttr, 'attributes' );
413 my $managerSub =
414 Dumper( \&Lemonldap::NG::Manager::Build::Attributes::perlExpr );
415 $managerSub =~ s/\$VAR1 = sub/sub perlExpr/s;
416 $managerSub =~ s/^\s*(?:use strict;|package .*?;|)\n//gm;
417 my $managerTypes =
418 mydump( Lemonldap::NG::Manager::Build::Attributes::types(), 'types' );
419 $managerAttr = "# This file is generated by $module. Don't modify it by hand
420 package Lemonldap::NG::Manager::Attributes;
421  
422 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
423  
424 $managerSub
425  
426 $managerTypes}
427  
428 $managerAttr}
429  
430 ";
431 eval {
432 Perl::Tidy::perltidy(
433 source => IO::String->new($managerAttr),
434 destination => \$dst
435 );
436 };
437 $dst = $managerAttr if ($@);
438  
439 open( F, ">", $self->managerAttributesFile ) or die($!);
440 print F $dst;
441 close F;
442 print STDERR "done\n";
443  
444 $self->buildZeroConf();
445  
446 printf STDERR $format, $self->reverseTreeFile;
447 open( F, ">", $self->reverseTreeFile ) or die($!);
448 $jsonEnc->pretty(0);
449 print F $jsonEnc->encode($reverseScanResult);
450 close F;
451  
452 print STDERR "done\n";
453 $self->buildPortalConstants();
454 }
455  
456 sub buildZeroConf {
457 my $self = shift;
458 $jsonEnc->pretty(1);
459 printf STDERR $format, $self->firstLmConfFile;
460 open( F, '>', $self->firstLmConfFile ) or die($!);
461 my $tmp = Lemonldap::NG::Manager::Conf::Zero::zeroConf(
462 '__DNSDOMAIN__', '__SESSIONDIR__',
463 '__PSESSIONDIR__', '__NOTIFICATIONDIR__',
464 '__CACHEDIR__'
465 );
466 $tmp->{cfgNum} = 1;
467 print F $jsonEnc->encode($tmp);
468 close F;
469 print STDERR "done\n";
470 }
471  
472 sub buildPortalConstants() {
473 my $self = shift;
474  
475 my %portalConstants =
476 %{ Lemonldap::NG::Manager::Build::PortalConstants::portalConstants() };
477 my %reverseConstants = reverse %portalConstants;
478 die "Duplicate value in portal constants"
479 unless %reverseConstants == %portalConstants;
480  
481 printf STDERR $format, $self->portalConstantsFile;
482 open( F, '>', $self->portalConstantsFile ) or die($!);
483 my $urire = $RE{URI}{HTTP}{ -scheme => qr/https?/ }{-keep};
484 $urire =~ s/([\$\@])/\\$1/g;
485 my $content = <<EOF;
486 # This file is generated by $module. Don't modify it by hand
487 package Lemonldap::NG::Portal::Main::Constants;
488  
489 use strict;
490 use Exporter 'import';
491  
492 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
493  
494 use constant HANDLER => 'Lemonldap::NG::Handler::PSGI::Main';
495 use constant URIRE => qr{$urire};
496 use constant {
497 EOF
498 for my $pe (
499 sort { $portalConstants{$a} <=> $portalConstants{$b} }
500 keys %portalConstants
501 )
502 {
503 my $str = $portalConstants{$pe};
504 $content .= " $pe => $str,\n";
505 }
506  
507 my $exports = join ", ", map { "'$_'" }
508 sort { $portalConstants{$a} <=> $portalConstants{$b} }
509 keys %portalConstants;
510  
511 my $portalConstsStr .= mydump( \%reverseConstants, 'portalConsts' );
512 $content .= <<EOF;
513 };
514  
515 $portalConstsStr
516 }
517  
518 # EXPORTER PARAMETERS
519 our \@EXPORT_OK = ( 'portalConsts', 'HANDLER', 'URIRE', $exports );
520 our %EXPORT_TAGS = ( 'all' => [ \@EXPORT_OK, 'import' ], );
521  
522 our \@EXPORT = qw(import PE_OK);
523  
524 1;
525 EOF
526  
527 my $dst;
528 eval {
529 Perl::Tidy::perltidy(
530 source => IO::String->new($content),
531 destination => \$dst
532 );
533 };
534 $dst = $content if ($@);
535 open( F, '>', $self->portalConstantsFile ) or die($!);
536 print F $dst;
537 close F;
538 print STDERR "done\n";
539  
540 printf STDERR $format, $self->handlerStatusConstantsFile;
541  
542 # Handler Status file
543 $content = <<EOF;
544 # This file is generated by $module. Don't modify it by hand
545 package Lemonldap::NG::Handler::Lib::StatusConstants;
546  
547 use strict;
548 use Exporter 'import';
549  
550 our \$VERSION = '$Lemonldap::NG::Manager::Build::Attributes::VERSION';
551  
552 $portalConstsStr
553 }
554  
555 # EXPORTER PARAMETERS
556 our \@EXPORT_OK = ( 'portalConsts' );
557 our %EXPORT_TAGS = ( 'all' => [ \@EXPORT_OK, 'import' ], );
558  
559 1;
560 EOF
561  
562 eval {
563 Perl::Tidy::perltidy(
564 source => IO::String->new($content),
565 destination => \$dst
566 );
567 };
568 $dst = $content if ($@);
569 open( F, '>', $self->handlerStatusConstantsFile ) or die($!);
570 print F $dst;
571 close F;
572 print STDERR "done\n";
573  
574 printf STDERR $format, $self->docConstantsFile;
575  
576 # Doc error code list
577 my %details;
578 if ( open F, 'lemonldap-ng-portal/site/htdocs/static/languages/en.json' ) {
579 local $/ = undef, my $tmp = JSON::from_json(<F>);
580 close F;
581 %details = map { s/^\s+//; $_ } %$tmp;
582 }
583 $content = <<EOF;
584 ..
585 This file is generated by $module. Don't modify it by hand
586  
587 Error codes list
588 ================
589  
590 .. note::
591  
592 This page references all Portal error codes.
593  
594 .. csv-table::
595 :header: "Error label", "Error number", "Detail"
596 :delim: ,
597 :widths: auto
598  
599  
600 EOF
601  
602 for my $key (
603 sort { $portalConstants{$a} <=> $portalConstants{$b} }
604 keys %portalConstants
605 )
606 {
607  
608 $content .=
609 " ``"
610 . $key . "``,"
611 . $portalConstants{$key} . ','
612 . (
613 %details
614 ? '"'
615 . (
616 $details{"PE$portalConstants{$key}"}
617 || $peOkValues{$key}
618 || [ $key =~ /PE_(.*)$/ ]->[0]
619 )
620 . '"'
621 : ''
622 ) . "\n";
623 }
624  
625 open( F, '>', $self->docConstantsFile ) or die($!);
626 print F $content;
627 close F;
628  
629 print STDERR "done\n";
630 }
631  
632 sub mydump {
633 my ( $obj, $subname ) = @_;
634 my $t = Dumper($obj);
635 $t =~ s/^\s*(?:use strict;|package .*?;|)\n//gm;
636 $t =~ s/\n\s*BEGIN.*?\}\n/\n/sg;
637 $t =~ s/^\$VAR1\s*=/sub $subname {\n return/;
638 return $t;
639 }
640  
641 sub scanTree {
642 my ( $self, $tree, $json, $prefix, $path ) = @_;
643 unless ( ref($tree) eq 'ARRAY' ) {
644 die 'Not an array';
645 }
646 $prefix //= '';
647 my $ord = -1;
648 my $nodeName = $path ? '_nodes' : 'data';
649 foreach my $leaf (@$tree) {
650 $ord++;
651 my $jleaf = {};
652  
653 # Grouped leaf
654 if ( ref($leaf) and $leaf->{group} ) {
655 die "'form' is required when using 'group'"
656 unless ( $leaf->{form} );
657 push @$json,
658 {
659 id => "$prefix$leaf->{title}",
660 title => $leaf->{title},
661 type => $leaf->{form},
662 get => [ map { "${prefix}$_" } @{ $leaf->{group} } ]
663 };
664 }
665  
666 # Subnode
667 elsif ( ref($leaf) ) {
668 $jleaf->{title} = $jleaf->{id} = $leaf->{title};
669 $jleaf->{type} = $leaf->{form} if ( $leaf->{form} );
670 if ( $leaf->{title} =~ /^((?:oidc|saml|cas)Service)MetaData$/ ) {
671 no strict 'refs';
672 my @tmp = $self->scanLeaf( $leaf->{nodes} );
673 ${ $1 . 'Parameters' } = \@tmp;
674 }
675 foreach my $n (qw(nodes nodes_cond)) {
676 if ( $leaf->{$n} ) {
677 $jleaf->{"_$n"} = [];
678 $self->scanTree( $leaf->{$n}, $jleaf->{"_$n"}, $prefix,
679 "$path.$nodeName\[$ord\]" );
680 if ( (
681 $leaf->{title} eq 'authParams'
682 and $n eq 'nodes_cond'
683 )
684 or $leaf->{title} eq 'issuerParams'
685 )
686 {
687 my $vn = $leaf->{title};
688 $vn =~ s/Params$/Parameters/;
689 foreach my $sn ( @{ $leaf->{$n} } ) {
690 no strict 'refs';
691 my @cn = $self->scanLeaf( $sn->{nodes} );
692 ${$vn}->{ $sn->{title} } = \@cn;
693 }
694 }
695 elsif ( $leaf->{title} eq 'issuerParams' ) {
696 }
697 if ( $n eq 'nodes_cond' ) {
698 foreach my $sn ( @{ $jleaf->{"_$n"} } ) {
699 $sn->{show} = 'false';
700 }
701 }
702 }
703 }
704 $jleaf->{help} = $leaf->{help} if ( $leaf->{help} );
705 $jleaf->{_nodes_filter} = $leaf->{nodes_filter}
706 if ( $leaf->{nodes_filter} );
707 push @$json, $jleaf;
708 }
709  
710 # Leaf
711 else {
712 # Get data type and build tree
713 #
714 # Types : PerlModule array bool boolOrExpr catAndAppList file
715 # hostname int keyTextContainer lmAttrOrMacro longtext
716 # openidServerList oidcAttributeContainer pcre rulesContainer
717 # samlAssertion samlAttributeContainer samlService select text
718 # trool url virtualHostContainer word password
719  
720 if ( $leaf =~ s/^\*// ) {
721 push @angularScopeVars, [ $leaf, "$path._nodes[$ord]" ];
722 }
723 if ( $leaf =~ /^(.*)(?<!notification)StorageOptions$/ ) {
724 push @sessionTypes, $1;
725 }
726 my $attr = $attributes->{$leaf} or die("Missing attribute $leaf");
727  
728 #print STDERR "| $attr->{documentation} | $leaf |\n";
729 $jleaf = { id => "$prefix$leaf", title => $leaf };
730 unless ( $attr->{type} ) {
731 print STDERR "Fatal: no type: $leaf\n";
732 exit;
733 }
734  
735 # TODO: change this
736 $attr->{type} =~
737 s/^(?:url|word|pcre|lmAttrOrMacro|hostname|PerlModule)$/text/;
738 $jleaf->{type} = $attr->{type} if ( $attr->{type} ne 'text' );
739 foreach my $w (qw(default help select get template)) {
740 $jleaf->{$w} = $attr->{$w} if ( defined $attr->{$w} );
741 }
742 if ( defined $jleaf->{default} ) {
743 unless ( $attr->{type} eq 'bool' and $jleaf->{default} == 0 ) {
744 $defaultValues->{$leaf} = $jleaf->{default};
745 }
746 if ( ref( $jleaf->{default} ) ) {
747 $jleaf->{default} = [];
748 my $type = $attr->{type};
749 $type =~ s/Container//;
750 foreach my $k ( sort keys( %{ $attr->{default} } ) ) {
751  
752 # Special handling for oidcAttribute
753 my $default = $attr->{default}->{$k};
754 if ( $attr->{type} eq 'oidcAttributeContainer' ) {
755 $default = [ $default, "string", "auto" ];
756 }
757  
758 push @{ $jleaf->{default} },
759 {
760 id => "$prefix$leaf/$k",
761 title => $k,
762 type => $type,
763 data => $default,
764 (
765 $type eq 'rule'
766 ? ( re => $k )
767 : ()
768 ),
769 };
770 }
771 }
772 }
773  
774 if ($prefix) {
775 push @cnodesKeys, $leaf;
776 }
777  
778 # issue 2439
779 # FIXME: in future versions, oidcOPMetaDataJSON and samlIDPMetaDataXML should
780 # behave the same
781 if ( $leaf =~ /^oidcOPMetaData(?:JSON|JWKS)$/ ) {
782 push @simpleHashKeys, $leaf;
783 }
784  
785 if ( $attr->{type} =~ /^(?:catAndAppList|\w+Container)$/ ) {
786 $jleaf->{cnodes} = $prefix . $leaf;
787 unless ( $prefix or $leaf =~ $reIgnoreKeys ) {
788 push @simpleHashKeys, $leaf;
789 }
790 }
791 elsif ( $attr->{type} eq 'doubleHash' and $leaf !~ $reIgnoreKeys ) {
792 push @doubleHashKeys, $leaf;
793 }
794 else {
795 if ( $prefix and !$jleaf->{get} ) {
796 $jleaf->{get} = $prefix . $jleaf->{title};
797 }
798 if ( $attr->{type} eq 'bool' ) {
799 push @bool, $leaf;
800 }
801 if ( $attr->{type} eq 'array' ) {
802 push @arrayParam, $leaf;
803 }
804 }
805 push @$json, $jleaf;
806 }
807 }
808 }
809  
810 sub scanLeaf {
811 my ( $self, $tree ) = @_;
812 my @res;
813 foreach my $k (@$tree) {
814 if ( ref $k ) {
815 push @res, $self->scanLeaf1( $k->{nodes} || $k->{group} );
816 }
817 else {
818 push @res, $k;
819 }
820 }
821 return @res;
822 }
823  
824 sub scanLeaf1 {
825 my ( $self, $tree ) = @_;
826 my @res;
827 foreach my $k (@$tree) {
828 if ( ref $k ) {
829 push @res, $self->scanLeaf( $k->{nodes} || $k->{group} );
830 }
831 else {
832 push @res, $k;
833 }
834 }
835 return @res;
836 }
837  
838 sub reverseScan {
839 my ( $self, $tree, $path, $res, $cTrees ) = @_;
840 foreach my $elem (@$tree) {
841 $elem =~ s/^\*//;
842 if ( ref($elem) eq 'HASH' ) {
843 foreach (qw(nodes nodes_cond group)) {
844 $self->reverseScan( $elem->{$_}, "$path$elem->{title}/", $res,
845 $cTrees )
846 if ( $elem->{$_} );
847 }
848 }
849 else {
850 my ($singular) = $elem =~ /^(.*)s$/;
851 if ( $singular and $cTrees->{$singular} ) {
852 $self->reverseScan( $cTrees->{$singular},
853 "${path}${elem}/__confKey__/", $res, $cTrees );
854 }
855 else {
856 my $tmp = $path;
857 $tmp =~ s#/$##;
858 $res->{$elem} = $tmp;
859 }
860 }
861 }
862 return $res;
863 }
864  
865 1;
866 __END__
« blib/lib/Lemonldap/NG/Manager/Cli/Lib.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/Build/CTrees.pm »