From 4bf1cfa16397a79531a3ca0002ebdce401234347 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Wed, 17 Jul 2019 12:30:13 +0200 Subject: [PATCH] Some fixes with multi or single valued attr --- zmldapsync/zmldapsync.pl | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/zmldapsync/zmldapsync.pl b/zmldapsync/zmldapsync.pl index c560f9c..c27c072 100644 --- a/zmldapsync/zmldapsync.pl +++ b/zmldapsync/zmldapsync.pl @@ -301,13 +301,13 @@ DOMAIN: foreach my $domain ( keys $conf->{domains} ) { my $ext_users = ldap2hashref( $ext_user_search, $conf->{domains}->{$domain}->{users}->{key}, - ( $conf->{domains}->{$domain}->{users}->{alias_attr} ), - @single + [ $conf->{domains}->{$domain}->{users}->{alias_attr} ], + \@single ); my $zim_users = ldap2hashref( $zim_user_search, 'uid', - ('mail') + [ 'mail' ] ); # First loop : Check users which exist in external LDAP but not in Zimbra @@ -509,16 +509,16 @@ DOMAIN: foreach my $domain ( keys $conf->{domains} ) { my $ext_groups = ldap2hashref( $ext_group_search, $conf->{domains}->{$domain}->{groups}->{key}, - ( + [ $conf->{domains}->{$domain}->{groups}->{members_attr}, $conf->{domains}->{$domain}->{groups}->{alias_attr} - ), - @single + ], + \@single ); my $zim_dl = ldap2hashref( $zim_dl_search, 'uid', - ('zimbraMailForwardingAddress', 'mail') + [ 'zimbraMailForwardingAddress', 'mail' ] ); # Build a dn2id hashref to lookup users or groups by their DN @@ -752,20 +752,26 @@ sub handle_error { # It'll return a hashref. The key will be unaccentuated and lower cased. sub ldap2hashref { - my ( $search, $key, @want_array, @want_single ) = @_; - my $return = {}; + my $search = shift; + my $key = shift; + my $want_array = shift; + my $want_single = shift; + my $return = {}; + + $want_array ||= []; + $want_single ||= []; foreach my $entry ( $search->entries ) { $return->{unidecode( lc $entry->get_value($key) )}->{dn} = $entry->dn; foreach my $attr ( $entry->attributes ) { my @values = $entry->get_value($attr); - if ( grep { $attr eq $_ } @want_array ) { + if ( grep { $attr eq $_ } @{ $want_array } ) { $return->{unidecode( lc $entry->get_value($key) )}->{$attr} = \@values; - } elsif ( grep { $attr eq $_ } @want_single ) { + } elsif ( grep { $attr eq $_ } @{ $want_single } ) { $return->{unidecode( lc $entry->get_value($key) )}->{$attr} = $values[0]; } else { $return->{unidecode( lc $entry->get_value($key) )}->{$attr} = ( scalar @values == 1 ) ? - \@values : $values[0]; + $values[0] : \@values; } } }