AddressBook

 view release on metacpan or  search on metacpan

lib/AddressBook/DB/LDAP.pm  view on Meta::CPAN

The LDAP host to which to connect.

=item base

The base for LDAP queries.

=item objectclass

The objectclass for AddressBook entries.

=item username

An LDAP dn to use for accessing the server.

=item password

=item dn_calculate

A perl expression which, when eval'd returns a valid LDAP "dn" 
(omitting the "base" part of the dn).  Other attributes may be referenced as "$<attr>".  

For example, if LDAP entries have a dn like: "cn=John Doe,mail=jdoe@mail.com", then use
the following:

  dn_calculate="'cn=' . $cn . ',mail=' . $mail"

=back

Any of these options may be specified in the constructor, or in the configuration file.

=cut

use strict;
use Net::LDAP;
use Net::LDAP::Util qw(ldap_error_text);
use AddressBook;
use Date::Manip;
use Carp;
use vars qw(@ISA $VERSION);

$VERSION = '0.13';

@ISA = qw(AddressBook);

=head2 new

  $a = AddressBook->new(source => "LDAP");
  $a = AddressBook->new(source => "LDAP:localhost/ou=People,dc=example,dc=com");
  $a = AddressBook->new(source => "LDAP",
			hostname=>"localhost",
			base=>"o=test"
			);

Any or all options may be specified in the constructor, or in the configuration file.

=cut

sub new {
  my $class = shift;
  my $self = {};
  bless ($self,$class);
  my %args = @_;
  foreach (keys %args) {
    $self->{$_} = $args{$_};
  }
  my ($hostname,$base,$mesg);
  if ($self->{dsn}) {
    ($hostname,$base) = split "/", $self->{dsn};
  }
  $self->{hostname} = $hostname || $self->{hostname};
  $self->{base} = $base || $self->{base};
  $self->{ldap} = Net::LDAP->new($self->{hostname}, async => 1 || croak $@);
  unless ($self->{anonymous}) {
    $mesg = $self->{ldap}->bind($self->{username}, password => $self->{password});
  } else {
    $mesg = $self->{ldap}->bind;
  }
  if ($mesg->is_error) {
    croak "could not bind to LDAP server: " . $mesg->error;
  }
  return $self;
}

sub search {
  my $self = shift;
  my $class = ref $self || croak "Not a method call.";
  my @ret;
  my %arg = @_;
  my $max_size = $arg{entries} || 0;
  my $max_time = $arg{time} || 0;
  my $fuzzy = $arg{fuzzy} || 0;
  if (exists $arg{strict}) {
    warn "The 'strict' parameter to LDAP backend searches has been removed";
  } 
  delete $arg{entries};
  delete $arg{time};

  if(defined $arg{filter}) {
    # We have stuff to look for;
    if (ref($arg{filter}) ne "ARRAY") {$arg{filter} = [$arg{filter}]}
    my $evalstring = "=";
    my ($entry,$filter,$filter_element,$subfilter,$value);
    foreach $filter_element (@{$arg{filter}}) {
      $entry = AddressBook::Entry->new(attr=>$filter_element,
				       config => $self->{config},
				       );
      #$entry->calculate;
      $entry = $entry->get(db=>$self->{db_name},values_only=>'1');
      $subfilter="";
      foreach (keys %{$entry}) { 
	$value = $entry->{$_}->[0];
	$value =~ s/\(/\\(/g;
	$value =~ s/\)/\\)/g;
        if ($fuzzy) {
	  $value = "*" . $value . "*";
	}
	$subfilter .= "(" . $_ . $evalstring . $value . ")";
      }
      $filter .= "(& $subfilter)";
    }
    $filter = "(| $filter)";



( run in 0.885 second using v1.01-cache-2.11-cpan-39bf76dae61 )