blib/lib/Lemonldap/NG/Manager/Api/Common.pm

filter: Statementstmt 97.9% 96 / 98 Branchbran 78.1% 25 / 32 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 19 / 19 Podpod n/a totaltotal 90.5% 144 / 159 CC 22 SCAR 11.9
CC
22
Cov
91%
_isSimpleKeyValueHash
5.9
_translateValueApiToConf
5.9
_hasAllowedAttributes
4.0
CRAP 3.3
« blib/lib/Lemonldap/NG/Manager/Attributes.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/2ndFA.pm »
1 package Lemonldap::NG::Manager::Api::Common;
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 Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Lemonldap::NG::Manager::Build::Attributes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Lemonldap::NG::Manager::Build::CTrees;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 28 our @EXPORTS = qw(&_listAttributes);
13  
14 # use Scalar::Util 'weaken'; ?
15  
16 28 my @arrayize = qw(
17 oidcRPMetaDataOptionsRedirectUris
18 oidcRPMetaDataOptionsPostLogoutRedirectUris
19 oidcRPMetaDataOptionsAdditionalAudiences
20 casAppMetaDataOptionsService
21 oidcRPMetaDataOptionsRequestUris
22 );
23  
24 2552 ▶sub _mustArrayizeOption {
Subroutine _mustArrayizeOption: called CC=2 SCAR=6.9
25 2552 my ( $self, $option ) = @_;
26 2552 ▶ return ( grep { $_ eq $option } @arrayize ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $option;} @arrayize) ? : 187 2365 2552
27 }
28  
29 32 ▶sub _isSimpleKeyValueHash {
Subroutine _isSimpleKeyValueHash: called CC=5 SCAR=17.8
30 32 my ( $self, $hash ) = @_;
31 32 ▶ return 0 if ( ref($hash) ne "HASH" );
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash ne "HASH" 0 32 32
32  
33 32 foreach ( keys %$hash ) {
34 49 ▶ return 0 if ( ref( $hash->{$_} ) ne '' || ref($_) ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash->{$_} ne "" or ref $_ ne "" 0 49 49
Condition33%1/3
ref $hash->{$_} ne "" or ref $_ ne ""
l!l&&r!l&&!r
0049
MC/DC0%0/2
ref $hash->{$_} ne "" or ref $_ ne ""
ref $hash->{$_} ne "" ref $_ ne ""
Truth table33%1/3
A or B
A= ref $hash->{$_} ne ""B= ref $_ ne ""
ABresult
1X1
011
000
35 }
36  
37 32 return 1;
38 }
39  
40 19 ▶sub _getDefaultValues {
Subroutine _getDefaultValues: called CC=4 SCAR=13.9
41 19 my ( $self, $rootNode ) = @_;
42 19 my @allAttrs = $self->_listAttributes($rootNode);
43 19 my $defaultAttrs = Lemonldap::NG::Manager::Build::Attributes::attributes();
44 19 my $attrs = {};
45  
46 19 foreach my $attr (@allAttrs) {
47 $attrs->{$attr} = $defaultAttrs->{$attr}->{default}
48 if ( defined $defaultAttrs->{$attr}
49 521 ▶ && defined $defaultAttrs->{$attr}->{default} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $defaultAttrs->{$attr} and defined $defaultAttrs->{$attr}{"default"} 316 205 521
Condition66%2/3
defined $defaultAttrs->{$attr} and defined $defaultAttrs->{$attr}{"default"}
!ll&&!rl&&r
0205316
MC/DC50%1/2
defined $defaultAttrs->{$attr} and defined $defaultAttrs->{$attr}{"default"}
defined $defaultAttrs->{$attr} defined $defaultAttrs->{$attr}{"default"}
Truth table66%2/3
A and B
A= defined $defaultAttrs->{$attr}B= defined $defaultAttrs->{$attr}{"default"}
ABresult
0X0
100
111
50 }
51  
52 19 return $attrs;
53 }
54  
55 25 ▶sub _hasAllowedAttributes {
Subroutine _hasAllowedAttributes: called CC=4 SCAR=14.0
56 25 my ( $self, $attributes, $rootNode ) = @_;
57 25 my @allowedAttributes = $self->_listAttributes($rootNode);
58  
59 25 foreach my $attribute ( keys %{$attributes} ) {
60 315 ▶ if ( length( ref($attribute) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $attribute) 0 315 315
61 return {
62 0 res => "ko",
63 msg => "Invalid input: Attribute $attribute is not a string."
64 };
65 }
66 315 ▶ unless ( grep { $_ eq $attribute } @allowedAttributes ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $attribute;} @allowedAttributes) 9 306 315
67 return {
68 9 res => "ko",
69 msg => "Invalid input: Attribute $attribute does not exist."
70 };
71 }
72 }
73  
74 16 return { res => "ok" };
75 }
76  
77 44 ▶sub _listAttributes {
Subroutine _listAttributes: called CC=1 SCAR=0.0
78 44 my ( $self, $rootNode ) = @_;
79 44 my $mainTree = Lemonldap::NG::Manager::Build::CTrees::cTrees();
80 44 my $rootNodes = [ grep { ref($_) eq "HASH" } @{ $mainTree->{$rootNode} } ];
81 44 my @attributes = map { $self->_listNodeAttributes($_) } @$rootNodes;
82  
83 44 return @attributes;
84 }
85  
86 236 ▶sub _listNodeAttributes {
Subroutine _listNodeAttributes: called CC=2 SCAR=6.9
87 236 my ( $self, $node ) = @_;
88 my @attributes =
89 1442 ▶ map { ref($_) eq "HASH" ? $self->_listNodeAttributes($_) : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 162 1280 1442
90 236 @{ $node->{nodes} };
91  
92 236 return @attributes;
93 }
94  
95 82 ▶sub _translateOptionApiToConf {
Subroutine _translateOptionApiToConf: called CC=1 SCAR=0.0
96 82 my ( $self, $optionName, $prefix ) = @_;
97  
98 # For consistency
99 82 $optionName =~ s/^clientId$/clientID/;
100  
101 82 return $prefix . "MetaDataOptions" . ( ucfirst $optionName );
102 }
103  
104 2589 ▶sub _translateOptionConfToApi {
Subroutine _translateOptionConfToApi: called CC=1 SCAR=0.0
105 2589 my ( $self, $optionName ) = @_;
106 2589 $optionName =~ s/^(\w+)MetaDataOptions//;
107  
108 2589 $optionName = lcfirst $optionName;
109  
110 # iDToken looks ugly
111 2589 $optionName =~ s/^iDToken/IDToken/;
112  
113 # For consistency
114 2589 $optionName =~ s/^clientID/clientId/;
115 2589 return $optionName;
116 }
117  
118 2470 ▶sub _translateValueConfToApi {
Subroutine _translateValueConfToApi: called CC=2 SCAR=6.9
119 2470 my ( $self, $optionName, $optionValue ) = @_;
120  
121 2470 ▶ if ( $self->_mustArrayizeOption($optionName) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_mustArrayizeOption($optionName)) { } 163 2307 2470
122 163 return [ split( /\s+/, $optionValue, ) ];
123 }
124 else {
125 2307 return $optionValue;
126 }
127 }
128  
129 82 ▶sub _translateValueApiToConf {
Subroutine _translateValueApiToConf: called CC=5 SCAR=17.8
130 82 my ( $self, $optionName, $optionValue ) = @_;
131  
132 82 ▶ if ( $self->_mustArrayizeOption($optionName) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_mustArrayizeOption($optionName)) { } 24 58 82
elsif (&JSON::is_bool($optionValue)) { } 0 58 58
133 24 ▶ die "$optionName is not an array\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $optionValue eq "ARRAY" 0 24 24
134 unless ( ref($optionValue) eq "ARRAY" );
135 24 return join( ' ', @{$optionValue} );
136 }
137  
138 # Translate JSON booleans to integers
139 elsif ( JSON::is_bool($optionValue) ) {
140 0 ▶ return $optionValue ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$optionValue ? : 0 0 0
141 }
142 else {
143 58 return $optionValue;
144 }
145 }
146  
147 29 ▶sub _getRegexpFromPattern {
Subroutine _getRegexpFromPattern: called CC=3 SCAR=11.0
148 29 my ( $self, $pattern ) = @_;
149 29 ▶ return unless ( $pattern =~ /[\w\.\-\*]+/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern =~ /[\w\.\-\*]+/ 10 19 29
150  
151 # . is allowed, and must be escaped
152 19 $pattern =~ s/\./\\\./g;
153 19 $pattern =~ s/\*/\.\*/g;
154  
155 # anchor string, unless * was provided
156 19 ▶ $pattern = "^$pattern\$" if ( $pattern =~ /\*/ );
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ /\*/ 8 11 19
157  
158 19 return qr/$pattern/;
159 }
160  
161 108 ▶sub _getPersistentMod {
Subroutine _getPersistentMod: called CC=1 SCAR=0.0
162 108 my ($self) = @_;
163 108 my $mod = $self->sessionTypes->{persistent};
164 108 $mod->{options}->{backend} = $mod->{module};
165 108 return $mod;
166 }
167  
168 24 ▶sub _getSSOMod {
Subroutine _getSSOMod: called CC=1 SCAR=0.0
169 24 my ($self) = @_;
170 24 my $mod = $self->sessionTypes->{global};
171 24 $mod->{options}->{backend} = $mod->{module};
172 24 return $mod;
173 }
174  
175 31 ▶sub _saveApplyConf {
Subroutine _saveApplyConf: called CC=1 SCAR=0.0
176 31 my ( $self, $conf ) = @_;
177 31 $self->_confAcc->saveConf($conf);
178 31 $self->applyConf($conf);
179 }
180  
181 48 ▶sub _merge_hash {
Subroutine _merge_hash: called CC=3 SCAR=11.0
182 48 my ( $self, $conf, $option, $provider, $hash_to_merge ) = @_;
183  
184 48 ▶ while ( my ( $key, $value ) = each %{$hash_to_merge} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$hash_to_merge;}) 302 48 350
185 302 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 294 8 302
186 294 $conf->{$option}->{$provider}->{$key} = $value;
187 }
188 else {
189 8 delete $conf->{$option}->{$provider}->{$key};
190 }
191 }
192 48 return;
193 }
194  
195 28 1;
« blib/lib/Lemonldap/NG/Manager/Attributes.pm ↑ Summary blib/lib/Lemonldap/NG/Manager/2ndFA.pm »