|
Net::LDAP::Express - Simplified interface for Net::LDAP |
Net::LDAP::Express - Simplified interface for Net::LDAP
use Net::LDAP::Express;
eval {
my $ldap =
Net::LDAP::Express->new(host => 'localhost',
bindDN => 'cn=admin,ou=People,dc=me',
bindpw => 'secret',
base => 'ou=People,dc=me',
searchattrs => [qw(cn uid loginname)],
%parms) ; # params for Net::LDAP::new
} ;
if ($@) {
die "Can't connect to ldap server: $@" ;
}
my $filter = '(|(loginname=~bronto)(|(cn=~bronto)(uid=~bronto)))' ;
my $entries ;
# These all return the same array of Net::LDAP::Entry objects
$entries = $ldap->search(filter => $filter) ; # uses new()'s base
$entries = $ldap->search(base => 'ou=People,dc=me',
filter => $filter) ;
$entries = $ldap->simplesearch('bronto') ; # uses new()'s searchattrs
# Now elaborate results:
foreach my $entry (@$entries) {
modify_something_in_this($entry) ;
}
# You often want to update a set of entries
foreach my $entry (@$entries) {
die "Error updating entry" unless defined $ldap->update($entry) ;
}
# but I think you'll prefer this way:
my $result = $ldap->update(@$entries) ;
unless (@$result == @$entries) {
print "Error updating entries: ",$ldap->error,
"; code ",$ldap->errcode,".\n\n" ;
}
# Add an entry, or an array of them, works as above: die $ldap->error unless $ldap->add_many(@some_other_entries) ;
# rename an entry: sometimes you simply want to change a name # and nothing else... $ldap->rename($entry,$newrdn) ;
# Ask for just a few attributes, sort results
$ldap = Net::LDAP::Express->new(host => $server,
port => $port,
base => $base,
bindDN => $binddn,
bindpw => $bindpw,
onlyattrs => \@only,
sort_by => \@sortby,
searchattrs => \@search) ;
my $entries = $ldap->simplesearch('person') ;
Net::LDAP::Express is an alternative interface to the fantastic Graham Barr's Net::LDAP, that simplifies the tasks of adding and deleting multiple entries, renaming them, or searching entries residing in a common subtree.
Net::LDAP is a great module for working with directory servers, but it's a bit overkill when you want to do simple short scripts or have big programs that always do the same job again and again, say: open an authenticated connection to a directory server, search entries against the same attributes each time and in the same way (e.g.: approx search against the three attributes cn, uid and loginname). With Net::LDAP this would mean:
bind() ;
code() or is_error()
method on the search object;
With Net::LDAP::Express this is done with:
new() method;
new() and check the return
value: if it was successful you have a reference to an array of
Net::LDAP::Entry objects, if it was unsuccessful you get undef, and
you can check what the error was with the error() method (or the error
code with errcode) ;
new(%parms)host parameter. Specific Net::LDAP::Express
parameters are therefore:
All Net::LDAP methods are supported via inheritance. Method specific in Net::LDAP::Express or that override inherited methods are documented below.
attrs parameter.
search() returns a Net::LDAP::Search object, thus mantaining an almost
complete compatibility with the parent class interface.
add_many, but it deletes entries instead :-)
rename($entry,$newrdn)update(@entries)simplesearch($searchstring)
Marco Marongiu, <bronto@cpan.org>
|
Net::LDAP::Express - Simplified interface for Net::LDAP |