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

96 lines
2.3 KiB
Perl
Raw Normal View History

package Lemonldap::NG::Manager::Api::Common;
our $VERSION = '2.0.7';
package Lemonldap::NG::Manager::Api;
use Lemonldap::NG::Manager::Build::Attributes;
use Lemonldap::NG::Manager::Build::CTrees;
2019-12-17 20:58:55 +01:00
# use Scalar::Util 'weaken'; ?
sub _isSimpleKeyValueHash {
2019-12-17 20:58:55 +01:00
my ( $self, $hash ) = @_;
if ( ref($hash) ne "HASH" ) {
return 0;
}
2019-12-17 20:58:55 +01:00
foreach ( keys %{$hash} ) {
if ( ref( $hash->{$_} ) ne '' || ref($_) ne '' ) {
return 0;
}
}
return 1;
}
sub _setDefaultValues {
2019-12-17 20:58:55 +01:00
my ( $self, $attrs, $rootNode ) = @_;
my @allAttrs = $self->_listAttributes($rootNode);
my $defaultAttrs = Lemonldap::NG::Manager::Build::Attributes::attributes();
foreach $attr (@allAttrs) {
2019-12-17 20:58:55 +01:00
unless ( defined $attrs->{$attr} ) {
if ( defined $defaultAttrs->{$attr}
&& defined $defaultAttrs->{$attr}->{default} )
{
$attrs->{$attr} = $defaultAttrs->{$attr}->{default};
}
}
}
return $attrs;
}
sub _hasAllowedAttributes {
2019-12-17 20:58:55 +01:00
my ( $self, $attributes, $rootNode ) = @_;
my @allowedAttributes = $self->_listAttributes($rootNode);
2019-12-17 20:58:55 +01:00
foreach $attribute ( keys %{$attributes} ) {
if ( length( ref($attribute) ) ) {
return {
res => "ko",
msg => "Invalid input: Attribute $attribute is not a string."
};
}
2019-12-17 20:58:55 +01:00
unless ( grep { /^$attribute$/ } @allowedAttributes ) {
return {
res => "ko",
msg => "Invalid input: Attribute $attribute does not exist."
};
}
}
return { res => "ok" };
}
sub _listAttributes {
2019-12-17 20:58:55 +01:00
my ( $self, $rootNode ) = @_;
my $mainTree = Lemonldap::NG::Manager::Build::CTrees::cTrees();
2019-12-17 20:58:55 +01:00
my $rootNodes = [ grep { ref($_) eq "HASH" } @{ $mainTree->{$rootNode} } ];
my @attributes;
2019-12-17 20:58:55 +01:00
foreach $node ( @{$rootNodes} ) {
push @attributes, $self->_listNodeAttributes($node);
}
return @attributes;
}
sub _listNodeAttributes {
2019-12-17 20:58:55 +01:00
my ( $self, $node ) = @_;
my @attributes;
2019-12-17 20:58:55 +01:00
foreach $child ( @{ $node->{nodes} } ) {
if ( ref($child) eq "HASH" ) {
push( @attributes, $self->_listNodeAttributes($child) );
}
else {
push( @attributes, $child );
}
}
return @attributes;
}
1;