2019-12-17 09:56:59 +01:00
|
|
|
package Lemonldap::NG::Manager::Api::Common;
|
2020-01-10 21:29:11 +01:00
|
|
|
|
|
|
|
our $VERSION = '2.0.8';
|
2019-12-17 09:56:59 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-12-17 09:56:59 +01:00
|
|
|
# use Scalar::Util 'weaken'; ?
|
|
|
|
|
|
|
|
sub _isSimpleKeyValueHash {
|
2019-12-17 20:58:55 +01:00
|
|
|
my ( $self, $hash ) = @_;
|
2020-01-10 21:29:11 +01:00
|
|
|
return 0 if ( ref($hash) ne "HASH" );
|
|
|
|
|
|
|
|
foreach ( keys %$hash ) {
|
|
|
|
return 0 if ( ref( $hash->{$_} ) ne '' || ref($_) ne '' );
|
2019-12-17 09:56:59 +01:00
|
|
|
}
|
2020-01-10 21:29:11 +01:00
|
|
|
|
2019-12-17 09:56:59 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:52:44 +02:00
|
|
|
sub _getDefaultValues {
|
|
|
|
my ( $self, $rootNode ) = @_;
|
2019-12-17 20:58:55 +01:00
|
|
|
my @allAttrs = $self->_listAttributes($rootNode);
|
2019-12-17 09:56:59 +01:00
|
|
|
my $defaultAttrs = Lemonldap::NG::Manager::Build::Attributes::attributes();
|
2020-04-06 22:52:44 +02:00
|
|
|
my $attrs = {};
|
2019-12-17 09:56:59 +01:00
|
|
|
|
|
|
|
foreach $attr (@allAttrs) {
|
2020-04-06 22:52:44 +02:00
|
|
|
$attrs->{$attr} = $defaultAttrs->{$attr}->{default}
|
|
|
|
if ( defined $defaultAttrs->{$attr}
|
|
|
|
&& defined $defaultAttrs->{$attr}->{default} );
|
2019-12-17 09:56:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _hasAllowedAttributes {
|
2019-12-17 20:58:55 +01:00
|
|
|
my ( $self, $attributes, $rootNode ) = @_;
|
2019-12-17 09:56:59 +01:00
|
|
|
my @allowedAttributes = $self->_listAttributes($rootNode);
|
|
|
|
|
2019-12-17 20:58:55 +01:00
|
|
|
foreach $attribute ( keys %{$attributes} ) {
|
|
|
|
if ( length( ref($attribute) ) ) {
|
2019-12-17 09:56:59 +01:00
|
|
|
return {
|
|
|
|
res => "ko",
|
|
|
|
msg => "Invalid input: Attribute $attribute is not a string."
|
|
|
|
};
|
|
|
|
}
|
2020-04-06 22:52:44 +02:00
|
|
|
unless ( grep { $_ eq $attribute } @allowedAttributes ) {
|
2019-12-17 09:56:59 +01:00
|
|
|
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 ) = @_;
|
2020-02-20 23:34:02 +01:00
|
|
|
my $mainTree = Lemonldap::NG::Manager::Build::CTrees::cTrees();
|
|
|
|
my $rootNodes = [ grep { ref($_) eq "HASH" } @{ $mainTree->{$rootNode} } ];
|
2020-01-10 21:29:11 +01:00
|
|
|
my @attributes = map { $self->_listNodeAttributes($_) } @$rootNodes;
|
2019-12-17 09:56:59 +01:00
|
|
|
|
|
|
|
return @attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _listNodeAttributes {
|
2019-12-17 20:58:55 +01:00
|
|
|
my ( $self, $node ) = @_;
|
2020-01-10 21:29:11 +01:00
|
|
|
my @attributes =
|
|
|
|
map { ref($_) eq "HASH" ? $self->_listNodeAttributes($_) : $_ }
|
|
|
|
@{ $node->{nodes} };
|
2019-12-17 09:56:59 +01:00
|
|
|
|
|
|
|
return @attributes;
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:52:44 +02:00
|
|
|
sub _translateOptionApiToConf {
|
|
|
|
my ( $self, $optionName, $prefix ) = @_;
|
|
|
|
return $prefix . "MetaDataOptions" . ( ucfirst $optionName );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _translateOptionConfToApi {
|
|
|
|
my ( $self, $optionName ) = @_;
|
|
|
|
$optionName =~ s/^(\w+)MetaDataOptions//;
|
|
|
|
|
|
|
|
$optionName = lcfirst $optionName;
|
|
|
|
|
|
|
|
# iDToken looks ugly
|
|
|
|
$optionName =~ s/^iDToken/IDToken/;
|
|
|
|
return $optionName;
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:56:59 +01:00
|
|
|
1;
|