use strict;
use warnings;
+use LiCoM::Config (qw(get_config));
+
use Net::LDAP;
use Net::LDAP::Filter;
=cut
-our %Config =
-(
- base_dn => undef
-);
-
our %ValidFields =
(
telephoneNumber => 1,
labeledURI => 1,
mail => 1,
mobile => 1,
- o => 1,
userPassword => 0
);
uri => 'labeledURI',
mail => 'mail',
cellphone => 'mobile',
- group => 'o',
password => 'userPassword'
);
return (1);
-=head1 METHODS
+=head1 STATIC FUNCTIONS
=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;
- my $entry = shift;
- my $obj = {};
-
- $obj->{'dn'} = $entry->dn ();
- $obj->{'ldap'} = $entry;
-
- for (keys %ValidFields)
- {
- my $key = $_;
- $obj->{$key} = $entry->get_value ($key, asref => $ValidFields{$key});
- }
-
- return (bless ($obj, $pkg));
-}
-
-=item Person-E<gt>B<load> (I<$cn>)
+=item LiCoM::Person-E<gt>B<load> (I<$cn>)
Loads the given CN and returns the B<Person>-object.
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.
my %hash = @_;
my $entry = Net::LDAP::Entry->new ();
my $dn;
- my $ou;
$entry->add (objectClass => [qw(top organizationalUnit person organizationalPerson inetOrgPerson)]);
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');
- $entry->add (cn => "$sn $gn", ou => $ou);
+ $entry->add (cn => "$sn $gn");
$entry->dn ($dn);
- print "<!--\n";
- $entry->dump (*STDOUT);
- print "-->\n";
-
$entry->changetype ('add');
my $mesg = $entry->update ($Ldap);
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.
$filter = '(objectclass=inetOrgPerson)';
}
- #print STDERR "Debug: using filter: $filter";
-
$mesg = $Ldap->search
(
- base => $Config{'base_dn'},
+ base => get_config ('base_dn'),
filter => $filter
);
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.
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;
}
}
-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);
-}
-
-=item LiCoM::Person-E<gt>B<all_groups> ()
-
-Returns all defined groups. In scalar context returns a hash-ref with the
-group-names as keys and the number of group members as values. In list context
-returns a sorted list of group names.
-
-=cut
-
-sub all_groups
-{
- my $pkg = shift;
- my %retval = ();
-
- my $mesg = $Ldap->search
- (
- base => $Config{'base_dn'},
- filter => "(objectClass=groupOfNames)"
- );
-
- if ($mesg->is_error ())
- {
- warn ("Error while querying LDAP server: " . $mesg->error_text ());
- return (qw());
- }
-
- for ($mesg->entries ())
- {
- my $entry = $_;
- my ($name) = $entry->get_value ('cn', asref => 0);
- my @members = $entry->get_value ('member', asref => 0);
-
- $retval{$name} = scalar (@members);
- }
-
- if (wantarray ())
- {
- return (sort (keys %retval));
- }
- else
- {
- return (\%retval);
- }
-}
-
=back
=head1 AUTHOR