#/usr/bin/perl use strict; use Net::LDAP; use Text::CSV; my $ldapserver = "localhost"; my $binddn = "cn=manager,o=od"; my $bindpw = "123Soleil" ; my $isPeople=0 ; my $isEnt=0; my $ldap = Net::LDAP->new( $ldapserver ) or die "$@" ; my $mesg = $ldap->bind( $binddn, password => $bindpw ); $mesg->code and die $mesg->error; # check for errors my $csv = Text::CSV->new({ sep_char => ';' }) ; my $file = $ARGV[0] or die "Need to get CSV file on the command line\n" ; if( $file =~ /entreprise/i ) { $isEnt=1 ; }elsif( $file =~ /particulier/i ) { $isPeople=1 ; } else{ die "Unknown branch\n" ; } open(my $data, '<:encoding(utf8)', $file) or die "Could not open '$file' $!\n"; my @cols = @{$csv->getline ($data)}; my $row = {} ; $csv->bind_columns (\@{$row}{@cols}); foreach my $c( @cols) { print ">".$c."<\n" ; } while ($csv->getline ($data)) { my $dn = $row->{dn} ; if ( $dn =~ /^dn/ ) { warn "next" ; next ; } my $idTechnique = $row->{idTechnique} ? $row->{idTechnique} : "" ; my $question1 = $row->{question1} ? $row->{question1} : "UNDEFINED" ; my $question2 = $row->{question2} ? $row->{question2} : "UNDEFINED" ; my $question3 = $row->{question3} ? $row->{question3} : "UNDEFINED" ; my $reponse1 = $row->{reponse1} ? $row->{reponse1} : "UNDEFINED" ; my $reponse2 = $row->{reponse2} ? $row->{reponse2} : "UNDEFINED" ; my $reponse3 = $row->{reponse3} ? $row->{reponse3} : "UNDEFINED" ; my $siren = $row->{siren} ? $row->{siren} : "UNDEFINED" ; my $civilite = $row->{civilite} ? $row->{civilite} : "UNDEFINED" ; my $phoneNumber = $row->{telephoneNumber} ? $row->{telephoneNumber} : "UNDEFINED" ; my $birthDate = $row->{birthDate} ? $row->{birthDate} : "UNDEFINED" ; } warn "$dn\n" ; my $mesg = $ldap -> search( scope => 'base', attrs => ['1.1'], filter => '(objectClass=klesiaentreprise)', base => $dn, ); $mesg->code and warn $dn."\t".$mesg->error; if( $mesg-> count() == 0 ) { warn "Add klesia OC\n" ; my $mesg = $ldap-> modify( $dn, add => { objectClass => 'klesiaentreprise', }); } # add common attributes warn "Add common attributes\n" ; my $mesg = $ldap-> modify( $dn, changes=> [ replace => [ 'idTechnique' => $idTechnique ], replace => [ 'question1' => $question1 ], replace => [ 'question2' => $question2 ], replace => [ 'question3' => $question3 ], replace => [ 'reponse1' => $reponse1 ], replace => [ 'reponse2' => $reponse2 ], replace => [ 'reponse3' => $reponse3 ], replace => [ 'civilite' => $civilite], ] ) ; $mesg->code and warn $dn."\t".$mesg->error; # add entreprise specific attributes if( $isEnt ){ warn "Add enterprise specific attributes\n" ; my $mesg = $ldap-> modify( $dn, changes=> [ replace => [ 'siren' => $siren], ] ) ; $mesg->code and warn $dn."\t".$mesg->error; } # add people specific attributes if( $isPeople ){ warn "Add people specific attributes\n" ; my $mesg = $ldap-> modify( $dn, changes=> [ replace => [ 'birthDate' => $birthDate], replace => [ 'telephoneNumber' => $phoneNumber], ] ) ; $mesg->code and warn $dn."\t".$mesg->error; } }