IBM Connections: Populando o Profiles através de um arquivo LDIF
Em um cliente de IBM Connections precisei utilizar múltiplas fontes de dados de origem para popular os perfis dos usuários.
Essas fontes eram:
- Arquivo CSV
- LDAP
Ao invés de criar um script TDI para fazer isso preferi, gerar um arquivo LDIF e utilizar um script pronto que acompanha o Connections 4.5.
Os detalhes de como utilizar podem ser visto no artigo Creating a connector to synchronize Profiles data using LDIF.
No meu script em perl, eu fazia as seguintes atividades:
- Percorro o arquivo CSV
- Trato os dados que estão no arquivo
- Busco informações no LDAP
- Gero os dados do usuário no arquivo LDIF
Abaixo tem o fonte de geração do Arquivo
Executando
# perl convertCSVtoLDIF.pl ARQUIVO.csv
o arquivo gerado é o ARQUIVO.ldif.
Arquivo ConvertCSVtoLDIF
#!/usr/bin/perl
# Filename: ConvertCSVtoLDIF.pl
#
use FileHandle;
use strict;
use warnings;
use Net::LDAPS;
use Net::LDAP;
my $csvFile = $ARGV[0];
my $ldapServer = "ldap.company.com.br:389";
my $ldapUser = "uid=ldapbinduser";
my $ldapPassword = "<CHANGE_HERE>";
my $searchBase = "ou=users,o=company,c=br";
my $ldaps = 0;
sub ltrim { my $s = shift; $s =~ s/^\s+//; return $s };
sub rtrim { my $s = shift; $s =~ s/\s+$//; return $s };
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };
sub trimZeros { my $s = shift; $s =~ s/^0+//g; return $s };
sub getIBMEntryUUID {
my $uid = $_[0];
my $guid = "";
my $ldap;
my $mesg;
my $attrs = [ 'cn','mail','ibm-entryuuid' ];
if ($ldaps) {
$ldap = Net::LDAPS->new($ldapServer, verify=>'none') or die "$@";
}
else {
$ldap = Net::LDAP->new($ldapServer, verify=>'none') or die "$@";
}
$mesg = $ldap->bind ($ldapUser, password=>"$ldapPassword");
# $mesg->code && return undef;
$mesg->code && return "";
$mesg = $ldap->search(base => $searchBase, scope => "sub", filter => "uid=$uid", attrs => $attrs );
# $mesg->code && return undef;
$mesg->code && return "";
my $entry = $mesg->shift_entry;
if ($entry)
{
$guid = $entry->get_value('ibm-entryuuid');
#$entry->dump;
}
$ldap->unbind;
return $guid;
}
sub getTimeZone {
my $aux = "";
my $estado = $_[0];
if ( $estado eq "SP") {
$aux = "America/Sao_Paulo";
}
if ( $estado eq "MG") {
$aux = "America/Sao_Paulo";
}
return $aux;
}
sub getFone {
my $xDDD = $_[0];
my $xFone1 = $_[1];
my $xFone2 = $_[2];
my $xFone3 = $_[3];
if ( $xDDD ne "") {
if (substr($xDDD,0,1) eq "0") {
$xDDD = substr($xDDD,1);
}
$xDDD = "($xDDD)";
}
if ( $xFone1 ne "") {
if (substr($xFone1,0,1) eq "0") {
$xFone1 = substr($xFone1,1);
}
$xFone1 = " $xFone1";
}
if ( $xFone2 ne "") {
if (substr($xFone2,0,1) eq "0") {
$xFone2 = substr($xFone2,1);
}
$xFone2 = " - $xFone2";
}
if ( $xFone3 ne "") {
if (substr($xFone3,0,1) eq "0") {
$xFone3 = substr($xFone3,1);
}
$xFone3 = " - $xFone3";
}
return "$xDDD$xFone1$xFone2$xFone3";
}
sub getProperCase {
my $aux01 = join(' ',map{ucfirst(lc("$_"))}split(/\s/,$_[0]));
my @auxArray = split(/ /, $aux01);
my $aux02 = "";
foreach my $i (0 .. $#auxArray) {
$aux02 = $auxArray[$i];
if (($aux02 eq "Da")|| ($aux02 eq "De")|| ($aux02 eq "Do")) {
$aux02 = lc($aux02);
} elsif (($aux02 eq "Das")|| ($aux02 eq "Des")|| ($aux02 eq "Dos")) {
$aux02 = lc($aux02);
} elsif ($aux02 eq "E") {
$aux02 = lc($aux02);
}
$auxArray[$i] = $aux02;
}
return join(' ',@auxArray);
}
sub getGivenName {
my @auxArray = split(/ /, lc($_[0]));
return $auxArray[0];
}
sub getSurName {
my @auxArray = split(/ /, lc($_[0]));
my $aux01 = "";
my $aux02 = "";
my $j = 0;
foreach my $i (1..$#auxArray) {
$j = $i-1;
$aux01 = $auxArray[$j];
$aux02 = $auxArray[$i];
if ($aux02 eq "junior") {
$aux02 = $aux01;
} elsif ($aux02 eq "filho") {
$aux02 = $aux01;
} elsif ($aux02 eq "neto") {
$aux02 = $aux01;
}
}
return $aux02;
}
sub getSurNames {
my @auxArray = split(/ /, lc($_[0]));
$auxArray[0] = "";
return trim(join(' ',@auxArray));
}
##########################
sub ConvertCSVtoLDIF {
my $inputfile = new FileHandle($csvFile) ||
die "Can't open the import file!\n";
my $outputfile = new FileHandle(">>$csvFile.ldif") ||
die "Can't create or open the output file!\n";
my $outputfileNotOnLDAP = new FileHandle(">notOnLDAP.ldif") ||
die "Can't create or open the output file!\n";
my $line;
my $linecount = 0;
while ($line = <$inputfile>)
{
$linecount++;
chomp $line;
my $fUid;
my $fNome;
my $fEmail;
my $fUF;
my $fCargo;
my $fDDDTrabalho;
my $fFone1Trabalho;
my $fFone2Trabalho;
my $fFone3Trabalho;
my $fDDDResidencia;
my $fFone1Residencia;
my $fFone2Residencia;
my $fFone3Residencia;
my $fDDDCelular;
my $fFoneCelular;
my $fLinha1;
my $fLinha2;
($fUid,$fNome,$fEmail,$fUF,$fCargo,$fDDDTrabalho,$fFone1Trabalho,$fFone2Trabalho,$fFone3Trabalho,$fDDDResidencia,
$fFone1Residencia,$fFone2Residencia,$fFone3Residencia,$fDDDCelular,$fFoneCelular,$fLinha1,$fLinha2) = split (";", $line);
my $uid = lc($fUid);
my $guid = getIBMEntryUUID ($uid);
if ($guid eq "" ) {
printf $outputfileNotOnLDAP ("%s","uid=$uid\n");
} else {
printf $outputfile ("%s","dn: uid=$uid,$searchBase\n");
printf $outputfile ("%s","uid: $uid\n");
printf $outputfile ("%s","ibm-entryUuid: $guid\n");
my $sn = getProperCase($fNome);
printf $outputfile ("%s","mail: $fEmail\n");
printf $outputfile ("%s","sn: $sn\n");
printf $outputfile ("%s","givenName: $uid\n");
my $givenName = getGivenName($sn);
printf $outputfile ("%s","givenName: $givenName\n");
my $surName = getSurName($sn);
printf $outputfile ("%s","surName: $surName\n");
my $surNames = getSurNames($sn);
printf $outputfile ("%s","surNames: $surNames\n");
my $timezone = getTimeZone($fUF);
printf $outputfile ("%s","timezone: $timezone\n");
my $foneTrabalho = getFone($fDDDTrabalho,$fFone1Trabalho,$fFone2Trabalho,$fFone3Trabalho);
if ($foneTrabalho ne "") {
printf $outputfile ("%s","foneTrabalho: $foneTrabalho\n");
}
my $foneResidencia = getFone($fDDDResidencia,$fFone1Residencia,$fFone2Residencia,$fFone3Residencia);
if ($foneResidencia ne "") {
printf $outputfile ("%s","foneResidencia: $foneResidencia\n");
}
my $foneCelular = getFone($fDDDCelular,$fFoneCelular,"","");
if ($foneCelular ne "") {
printf $outputfile ("%s","mobile: $foneCelular\n");
}
# Atributos Extendidos
#printf $outputfile ("%s","info0:\n");
if ($fLinha1 ne "") {
printf $outputfile ("%s","info1: $fLinha1\n");
}
if ($fLinha2 ne "") {
printf $outputfile ("%s","info2: $fLinha2\n");
}
printf $outputfile ("%s","\n");
}
}
print "Registros: $linecount\n";
close $inputfile;
close $outputfile;
close $outputfileNotOnLDAP
}
#################################################### Main ####################################################
print "\nConvertCSVtoLDIF: start\n";
ConvertCSVtoLDIF();
print "\nConvertCSVtoLDIF: end\n";
exit (0);