I have an LDAP directory that I request using Net::LDAP . This gives me a set of parent-child relationships.
This is a directory of people - and includes a "manager" DN (this is another field in the directory).
I am having problems turning this manager → set of people records into a hierarchical structure.
What I have so far:
#!/usr/bin/env perl use strict; use warnings; use Net::LDAP; use Data::Dumper; my %people; my $ldap = Net::LDAP->new('my_ldap_server'); my $result = $ldap->bind('bind_dn'); die if $result->code; my $search = $ldap->search( base => 'ou=yaddayadda', scope => 'subtree', filter => 'objectClass=person', attrs => ['manager'], ); foreach my $found ( $search->entries ) { my $mgr = $found->get_value('manager'); my $dn = $result->dn; push( @{ $people{$mgr} }, $dn ); }
This gives me a hash of managers and the people who work for them (using a unique unique DN).
The entry from %people looks like this:
$VAR1 = { 'cn=Firstname Lastname,ou=OrgUnit' => [ 'cn=Personame Lastname,ou=OrgUnit', 'cn=AnotherPerson NameHere,ou=OrgUnit', ], 'cn=AnotherPerson NameHere,ou=OrgUnit' => [ 'cn=Someone Else,ou=OrgUnit', ] };
But I am having trouble converting this parent-child mapping to a hierarchical structure.
eg:.
'ceo' => [ 'pa' => [], 'head_of_dept' => [ 'person' => [], 'person_with_staff' => [ 'person3', 'person4' ] ] ]
I am at a loss how to do this. It does not seem to be too difficult to do, given that each person is unique in the structure of the organization.
NB - in the above example, I have cn=AnotherPerson NameHere,ou=OrgUnit , who has a subordinate, and I will make a nested mapping from this:
eg:.
$VAR1 = { 'cn=Firstname Lastname,ou=OrgUnit' => [ 'cn=Personame Lastname,ou=OrgUnit', 'cn=AnotherPerson NameHere,ou=OrgUnit', [ 'cn=Someone Else,ou=OrgUnit' ] ] };