Perl and Net::LDAP, objects and arrays query

Hi

I'm not a programmer but am muddling through as best I can. I am trying to set up a PostSearchHook for Radiator (RADIUS server), that carries out an LDAP lookup, and, based on the
string returned ("staff" or "student") in the "businessCategory" attribute, will set the $role to be either 40 or 20 (a VLAN ID).

There can however be multiple attributes for "businessCategory" (I for example, have "staff" and "imageportaladmin"), but the only string values needed are "staff" or "student", a
nd if both are returned, then "staff" must take precedent.

The code I have so far:

#!/usr/bin/perl -w
use diagnostics;
use Net::LDAP;
my $role = "";
$ldap = Net::LDAP->new("ldap://ldaphost");
$ldap->bind();
$mesg = $ldap->search(base => "ou=people,dc=base",
                        scope => "1",
                        filter => "(uid=MLott)",
                        attrs => ['businessCategory'],
                        );

                                        
# unbind from LDAP cleanly
$mesg = $ldap->unbind();

What I'm not clear on is how I retrieve the data in the $mesg object. If I add the following code above the unbind() method:

foreach $entry (@entries) {
        $entry->dump;
}

I get:

------------------------------------------------------------------------
dn:uid=MLott,ou=People,dc=base

businessCategory: staff
                  imageportaladmin

However, I need to be able to query this data for the correct strings (that I mentioned earlier). I know that dump() is method of Net::LDAP, but it shows that the query result is
correct.

If I add the following lines so that I can get a count, I only get the result of "1":

foreach $max ($mesg->count) {
        print "Count = $max\n";
}

Is that because it is "1" object returned (as opposed to items in a scalar list)?

Any help would be great.

Thanks,

Mike

Phew, it's been quite some time since I last played with Net::LDAP.
And as far as I can remember it took quite a bit of reading the POD beforehand.
So I would suggest you revisit the POD because it is all explained there how you access the returned objref of Net::LDAP::Search.
Because I have forgotten almost all I just have had a quick glance at it on search.cpan.org,
Net::LDAP::Search - Object returned by Net::LDAP search method - search.cpan.org
This class offers several accessor methods to fetch the result set.
As you went for the entries() method, this it says returns an array of Net::LDAP::Entry objrefs.
So you would have to look at this class' POD as well to know how to access the data
Net::LDAP::Entry - An LDAP entry object - search.cpan.org
Just from my very superficial exposure to the POD right now I would probably try something like this
(please, note this is a mere wild guess since I haven't even Net::LDAP installed on my surf laptop)

foreach my $entry ($mesg->entries()) {
    my @vals = grep { /staff|student/ } @{ $entry->get_value('businessCategory', asref => 1) };
    # further process @vals here
}

But please, this is a wild untested guess which probably is horribly wrong.
You just will have to consult the POD.
What I usually do to get a quick view of the deeply nested data structures I am not yet clear about
is to run my code in the Perl debugger (just invoke your script with preceeding perl -d
Then I continue (c) to the line where the result is fetched and view it in my pager by

DB<3> | x $mesg

This is most of the times far quicker to grasp then perusing the whole POD.

Hey buffoonix

Thanks for the effort. I've been scouring the POD all day :slight_smile: Like, I say, I'm not a programmer so this is all tough to take in but I guess that is half the fun.

Thanks for the tip on the Perl debugger; that's very helpful and I'll give that a try together with the POD and maybe I'll come up trumps with it.

Thanks,
Mike

Well, I figured it out (I think), so this is the code I'm using and it seems to work. Am fully open to someone checking it out and pointing out better ways of achieving the same thing though:

while( my $entry = $mesg->shift_entry) {
	my @values = $entry->get_value( 'businessCategory' );
       #print "@values\n";    
	if ( grep ( /staff$/i, @values ) ) { $role = '40'; }
	elsif ( grep ( /student$/i, @values ) ) { $role = '20'; }
	elsif ( grep ( /left$/i, @values ) ) { exit 0; }
  	}

Mike

1 Like