Commit 46afa3aa authored by Francesc Guasch's avatar Francesc Guasch
Browse files

[#40] get the first user found in search

parent e254de89
......@@ -55,4 +55,11 @@ sub login {
return Ravada::Auth::SQL->new(name => $name, password => $pass);
}
sub LDAP {
my $value = shift;
return $LDAP if !defined $value;
$LDAP = $value;
return $value;
}
1;
......@@ -92,7 +92,7 @@ Removes the user
sub remove_user {
my $name = shift;
_init_ldap_admin();
my $entry = search_user($name, $LDAP_ADMIN);
my ($entry) = search_user($name, $LDAP_ADMIN);
die "ERROR: Entry for user $name not found\n" if !$entry;
# $LDAP->delete($entry);
......@@ -238,7 +238,12 @@ Adds user to group
sub add_to_group {
my ($uid, $group_name) = @_;
my $user = search_user($uid) or die "No such user $uid";
my @user = search_user($uid) or die "No such user $uid";
warn "Found ".scalar(@user)." users $uid , getting the first one ".Dumper(\@user)
if scalar(@user)>1;
my $user = $user[0];
my $group = search_group(name => $group_name, ldap => $LDAP_ADMIN)
or die "No such group $group_name";
......@@ -361,6 +366,8 @@ sub _init_ldap_admin {
} else {
confess "ERROR: Missing ldap section in config file ".Dumper($$CONFIG)."\n"
}
confess "ERROR: Missing ldap -> admin_user -> dn "
if !$dn;
$LDAP_ADMIN = _connect_ldap($dn, $pass) ;
return $LDAP_ADMIN;
}
......@@ -390,7 +397,9 @@ sub is_admin {
return 0;
};
my $dn = search_user($self->name)->dn;
my ($user) = search_user($self->name);
my $dn = $user->dn;
return grep /^$dn$/,$group->get_value('uniqueMember');
}
......
......@@ -14,6 +14,8 @@ my $ravada = Ravada->new(connector => $test->connector);
Ravada::Auth::SQL::add_user('root','root', 1);
Ravada::Auth::LDAP(0);
{
my $user_fail;
eval { $user_fail = Ravada::Auth::SQL->new(name => 'root',password => 'fail')};
......
......@@ -64,6 +64,8 @@ sub test_user{
eval { $mcnulty = Ravada::Auth::LDAP->new(name => $name,password => 'jameson') };
ok($mcnulty,($@ or "ldap login failed for $name")) or return;
ok(ref($mcnulty) =~ /Ravada/i,"User must be Ravada::Auth::LDAP , it is '".ref($mcnulty));
ok(!$mcnulty->is_admin,"User ".$mcnulty->name." should not be admin "
.Dumper($mcnulty->{_data}));
......@@ -145,7 +147,6 @@ sub test_manage_group {
my $group0 = Ravada::Auth::LDAP::search_group(name => $name);
ok(!$group0,"Group $name shouldn't exist") or return;
diag("Adding group $name");
Ravada::Auth::LDAP::add_group($name);
my $group = Ravada::Auth::LDAP::search_group(name => $name);
......@@ -154,7 +155,10 @@ sub test_manage_group {
my $uid = 'ragnar.lothbrok';
my $user = test_user($uid);
ok(!$user->is_admin,"User $uid should not be admin");
my $is_admin;
eval { $is_admin = $user->is_admin };
ok(!$@,$@);
ok(!$is_admin,"User $uid should not be admin");
Ravada::Auth::LDAP::add_to_group($uid, $name);
ok($user->is_admin,"User $uid should be admin") or exit;
......@@ -181,7 +185,7 @@ SKIP: {
skip( ($@ or "No LDAP server found"),6) if !$ldap && $@ !~ /Bad credentials/;
ok(!$@ ) and do {
ok($ldap) and do {
test_user_fail();
test_user();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment