Bugfix: The created entries no longer are in the `top' or `organizationalUnit' object...
[licom.git] / lib / LiCoM / Person.pm
index d9453ff..887e39e 100644 (file)
@@ -3,6 +3,9 @@ package LiCoM::Person;
 use strict;
 use warnings;
 
+use LiCoM::Config (qw(get_config));
+use LiCoM::Connection (qw($Ldap));
+
 use Net::LDAP;
 use Net::LDAP::Filter;
 
@@ -12,11 +15,6 @@ Person - High level interface for address books using an LDAP-backend.
 
 =cut
 
-our %Config =
-(
-       base_dn         => undef
-);
-
 our %ValidFields =
 (
        telephoneNumber                 => 1,
@@ -29,7 +27,6 @@ our %ValidFields =
        labeledURI                      => 1,
        mail                            => 1,
        mobile                          => 1,
-       o                               => 1,
        userPassword                    => 0
 );
 
@@ -45,70 +42,11 @@ our %ExternalNames =
        uri             => 'labeledURI',
        mail            => 'mail',
        cellphone       => 'mobile',
-       group           => 'o',
        password        => 'userPassword'
 );
 
-our $Ldap;
-
 return (1);
 
-=head1 METHODS
-
-=over 4
-
-=item Person-E<gt>B<connect> (I<$server>, I<$bind_dn>, I<$password>, I<$base_dn>, [I<$port>])
-
-Connects to the LDAP-Server given.
-
-=cut
-
-sub connect
-{
-       my $pkg = shift;
-       my %opts = @_;
-
-       my $bind_dn = $opts{'bind_dn'};
-       my $base_dn = $opts{'base_dn'};
-       my $uri     = $opts{'uri'};
-       my $passwd  = $opts{'password'};
-
-       my $msg;
-
-       die unless ($bind_dn and $base_dn and $uri and defined ($passwd));
-
-       $Ldap = Net::LDAP->new ($uri);
-
-       $msg = $Ldap->bind ($bind_dn, password => $passwd);
-       if ($msg->is_error ())
-       {
-               warn ('LDAP bind failed: ' . $msg->error_text ());
-               return (0);
-       }
-
-       $Config{'base_dn'} = $base_dn;
-
-       return (1);
-}
-
-=item Person-E<gt>B<disconnect> ()
-
-Disconnect from the LDAP-Server.
-
-=cut
-
-sub disconnect
-{
-       $Ldap->unbind ();
-       $Ldap = undef;
-}
-
-=item Person-E<gt>B<new> (I<$ldap_entry>)
-
-Created a new I<Person>-object from the passed I<Net::LDAP::Entry>-object.
-
-=cut
-
 sub new
 {
        my $pkg = shift;
@@ -127,7 +65,11 @@ sub new
        return (bless ($obj, $pkg));
 }
 
-=item Person-E<gt>B<load> (I<$cn>)
+=head1 STATIC FUNCTIONS
+
+=over 4
+
+=item LiCoM::Person-E<gt>B<load> (I<$cn>)
 
 Loads the given CN and returns the B<Person>-object.
 
@@ -149,7 +91,7 @@ sub load
        return ($retval);
 }
 
-=item Person-E<gt>B<create> (B<lastname> =E<gt> I<$lastname>, B<firstname> =E<gt> I<$firstname>, ...)
+=item LiCoM::Person-E<gt>B<create> (B<lastname> =E<gt> I<$lastname>, B<firstname> =E<gt> I<$firstname>, ...)
 
 Create a new I<Net::LDAP::Entry>-object and return it's corresponding
 I<Person>-object.
@@ -165,7 +107,7 @@ sub create
        my $dn;
        my $ou;
 
-       $entry->add (objectClass => [qw(top organizationalUnit person organizationalPerson inetOrgPerson)]);
+       $entry->add (objectClass => [qw(person organizationalPerson inetOrgPerson)]);
 
        for (keys %hash)
        {
@@ -223,16 +165,12 @@ sub create
                return (undef);
        }
 
-       $dn = "cn=$sn $gn," . $Config{'base_dn'};
-       ($ou) = $Config{'base_dn'} =~ m/\bou\s*=\s*([^,]+)/i;
+       $dn = "cn=$sn $gn," . get_config ('base_dn');
+       ($ou) = get_config ('base_dn') =~ m/\bou\s*=\s*([^,]+)/i;
        
        $entry->add (cn => "$sn $gn", ou => $ou);
        $entry->dn ($dn);
 
-       print "<!--\n";
-       $entry->dump (*STDOUT);
-       print "-->\n";
-
        $entry->changetype ('add');
        my $mesg = $entry->update ($Ldap);
 
@@ -245,7 +183,7 @@ sub create
        return (new ($pkg, $entry));
 }
 
-=item Person-E<gt>B<search> (B<firstname> =E<gt> I<"Flor*">)
+=item LiCoM::Person-E<gt>B<search> (B<firstname> =E<gt> I<"Flor*">)
 
 Search for the given patterns. Returns a list of I<Person>-objects.
 
@@ -317,11 +255,9 @@ sub search
                $filter = '(objectclass=inetOrgPerson)';
        }
 
-       #print STDERR "Debug: using filter: $filter";
-       
        $mesg = $Ldap->search
        (
-               base   => $Config{'base_dn'},
+               base   => get_config ('base_dn'),
                filter => $filter
        );
 
@@ -342,6 +278,57 @@ sub search
        return (@retval);
 }
 
+=item LiCoM::Person-E<gt>B<get_user> (I<$dn>)
+
+Returns the cn and, if defined, the user-id of this dn.
+
+=cut
+
+sub get_user
+{
+       my $pkg = shift;
+       my $dn = shift;
+       my ($search) = $dn =~ m/cn\s*=\s*([^,]+)/i;
+
+       die unless ($search);
+       
+       my $cn = '';
+       my $id = '';
+
+       my $mesg = $Ldap->search
+       (
+               base   => get_config ('base_dn'),
+               filter => "(cn=$search)"
+       );
+
+       if ($mesg->is_error ())
+       {
+               warn ("Error while querying LDAP server: " . $mesg->error_text ());
+               return ('');
+       }
+
+       for ($mesg->entries ())
+       {
+               my $e = $_;
+               my ($t_cn) = $e->get_value ('cn', asref => 0);
+               my ($t_id) = $e->get_value ('uid', asref => 0);
+
+               if (!$id or $t_id)
+               {
+                       $cn = $t_cn;
+                       $id = $t_id;
+               }
+       }
+
+       return ($cn, $id);
+}
+
+=back
+
+=head1 METHODS
+
+=over 4
+
 =item I<$obj>-E<gt>B<delete> ()
 
 Deletes the record.
@@ -373,7 +360,7 @@ sub _update_dn
        my $sn = $obj->{'sn'};
        my $gn = $obj->{'givenName'};
        my $cn = "$sn $gn";
-       my $dn = "cn=$cn," . $Config{'base_dn'};
+       my $dn = "cn=$cn," . get_config ('base_dn');
 
        $obj->{'cn'} = $cn;
 
@@ -539,45 +526,6 @@ sub set
        }
 }
 
-sub get_user
-{
-       my $pkg = shift;
-       my $dn = shift;
-       my ($search) = $dn =~ m/cn\s*=\s*([^,]+)/i;
-
-       die unless ($search);
-       
-       my $cn = '';
-       my $id = '';
-
-       my $mesg = $Ldap->search
-       (
-               base   => $Config{'base_dn'},
-               filter => "(cn=$search)"
-       );
-
-       if ($mesg->is_error ())
-       {
-               warn ("Error while querying LDAP server: " . $mesg->error_text ());
-               return ('');
-       }
-
-       for ($mesg->entries ())
-       {
-               my $e = $_;
-               my ($t_cn) = $e->get_value ('cn', asref => 0);
-               my ($t_id) = $e->get_value ('uid', asref => 0);
-
-               if (!$id or $t_id)
-               {
-                       $cn = $t_cn;
-                       $id = $t_id;
-               }
-       }
-
-       return ($cn, $id);
-}
-
 =back
 
 =head1 AUTHOR