Browse Source

* LDV::LDAP : tested

master
Alex 'AdUser' Z 10 years ago
parent
commit
0ef9b5103c
  1. 14
      lib/LDV/LDAP.pm
  2. 36
      t/LDAP.t

14
lib/LDV/LDAP.pm

@ -59,8 +59,8 @@ sub create {
my $result = $conn->add($dn, attr => [
objectClass => ['inetOrgPerson'],
uid => $uid,
sn => 'just',
cn => 'created',
cn => 'just',
sn => 'created',
]);
$conn->unbind;
return $result->error if ($result->code);
@ -90,9 +90,11 @@ sub get {
my $data = {};
foreach my $attr ($entry->attributes) {
$data->{$attr} = $entry->get_value($attr);
utf8::decode($data->{$attr});
}
delete $data->{userPassword};
delete $data->{objectClass};
return $data;
}
@ -109,11 +111,13 @@ sub update {
unless ($data);
my $dn = sprintf "uid=%s,%s", $uid, $self->{userbase};
my %upd = ();
while (my ($key, $value) = each(%$attrs)) {
next if ($value and exists $data->{$key});
delete $attrs->{$key};
next unless ($value and exists $data->{$key});
next if ($key eq 'uid'); # rename protection
$upd{$key} = $value;
}
my $result = $conn->modify($dn, replace => $attrs);
my $result = $conn->modify($dn, replace => \%upd);
$conn->unbind;
return $result->error if ($result->code);

36
t/LDAP.t

@ -4,21 +4,31 @@ use strict;
use warnings;
use utf8;
use Test;
use LDV::LDAP;
use File::Slurp;
use Data::Dumper;
use Test::More tests => 8;
BEGIN { plan test => 2 };
my $hash = read_file('conf/ldv.conf');
my $config = eval "$hash";
SKIP: {
skip "Can't load config", 8 unless (ref($config) eq 'HASH');
my $ldap = LDV::LDAP->new({
server => '127.0.0.1',
binddn => undef,
bindpass => undef,
userbase => undef,
});
my $ldap = LDV::LDAP->new($config->{ldap});
isa_ok($ldap, "LDV::LDAP", "LDV::LDAP->new");
can_ok($ldap, qw(create delete get update));
print Dumper $ldap->get("ad_user");
#print Dumper $ldap->create("test20");
#print Dumper $ldap->get("test20");
#print Dumper $ldap->delete("test20");
print Dumper $ldap->get("test20");
is($ldap->get("test20"), undef, "get non-existing user");
is($ldap->create("test20"), undef, "creating user");
my $attrs = {uid => 'test20', cn => 'just', sn => 'created'};
is_deeply($ldap->get("test20"), $attrs, "getting data of newly created user");
$attrs = {uid => 'test20', cn => 'Полиграф', sn => 'Шариков'};
is($ldap->update("test20", $attrs), undef, "updating user data");
is_deeply($ldap->get("test20"), $attrs, "getting data of updated user");
is($ldap->delete("test20"), undef, "deleting user");
};
exit 0;

Loading…
Cancel
Save