Skip to content

Commit

Permalink
chore(tests): UT for LDAP STD
Browse files Browse the repository at this point in the history
Signed-off-by: Rubén D <[email protected]>
  • Loading branch information
nuxsmin committed Nov 1, 2023
1 parent 8ab5f32 commit d1b0403
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/SP/Providers/Auth/Ldap/LdapBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ protected function getGroupFromParams(): string
return LdapUtil::getGroupName($this->ldapParams->getGroup()) ?: '';
}

return $this->ldapParams->getGroup();
return $this->ldapParams->getGroup() ?? '';
}

/**
Expand Down
16 changes: 7 additions & 9 deletions lib/SP/Providers/Auth/Ldap/LdapMsAds.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getGroupMembershipIndirectFilter(): string
$attributes = $this->ldapParams->getFilterGroupAttributes();
}

return '(&(|' . LdapUtil::getAttributesForFilter($attributes, $this->getGroupDn()) . ')' . $filter . ')';
return sprintf("(&(|%s)%s)", LdapUtil::getAttributesForFilter($attributes, $this->getGroupDn()), $filter);
}

/**
Expand All @@ -90,11 +90,11 @@ public function getUserDnFilter(string $userLogin): string
$attributes = $this->ldapParams->getFilterUserAttributes();
}

return '(&(|'
. LdapUtil::getAttributesForFilter($attributes, $userLogin)
. ')'
. $this->getUserObjectFilter()
. ')';
return sprintf(
"(&(|%s)%s)",
LdapUtil::getAttributesForFilter($attributes, $userLogin),
$this->getUserObjectFilter()
);
}

/**
Expand Down Expand Up @@ -196,9 +196,7 @@ public function getGroupMembershipDirectFilter(?string $userDn = null): string
$attributes = $this->ldapParams->getFilterGroupAttributes();
}

return '(|'
. LdapUtil::getAttributesForFilter($attributes, $this->getGroupDn())
. ')';
return sprintf("(|%s)", LdapUtil::getAttributesForFilter($attributes, $this->getGroupDn()));
}

protected function pickServer(): string
Expand Down
43 changes: 26 additions & 17 deletions lib/SP/Providers/Auth/Ldap/LdapStd.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getGroupMembershipIndirectFilter(): string
$attributes = $this->ldapParams->getFilterGroupAttributes();
}

return '(&(|' . LdapUtil::getAttributesForFilter($attributes, $this->getGroupDn()) . ')' . $filter . ')';
return sprintf("(&(|%s)%s)", LdapUtil::getAttributesForFilter($attributes, $this->getGroupDn()), $filter);
}

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ public function getUserDnFilter(string $userLogin): string

$filter = $this->getUserObjectFilter();

return '(&(|' . LdapUtil::getAttributesForFilter($attributes, $userLogin) . ')' . $filter . ')';
return sprintf("(&(|%s)%s)", LdapUtil::getAttributesForFilter($attributes, $userLogin), $filter);
}

/**
Expand All @@ -104,17 +104,15 @@ public function isUserInGroup(string $userDn, string $userLogin, array $groupsDn
// los grupos del usuario
if (empty($this->ldapParams->getGroup())
|| $this->ldapParams->getGroup() === '*'
|| in_array($this->getGroupDn(), $groupsDn, true)) {
|| in_array($this->getGroupDn(), $groupsDn, true)
) {
$this->eventDispatcher->notifyEvent(
'ldap.check.group',
new Event(
$this,
EventMessage::factory()
->addDescription(__u('User in group verified'))
->addDetail(
__u('User'),
$userDn
)
->addDetail(__u('User'), $userDn)
->addDetail(__u('Group'), $this->ldapParams->getGroup())
)
);
Expand Down Expand Up @@ -144,21 +142,26 @@ private function checkUserInGroupByFilter(string $userDn): bool
$this,
EventMessage::factory()
->addDescription(__u('User does not belong to the group'))
->addDetail(
__u('User'),
$userDn
)
->addDetail(__u('User'), $userDn)
->addDetail(__u('Group'), $this->getGroupFromParams())
->addDetail(
'LDAP FILTER',
$filter
)
->addDetail('LDAP FILTER', $filter)
)
);

return false;
}

$this->eventDispatcher->notifyEvent(
'ldap.check.group',
new Event(
$this,
EventMessage::factory()
->addDescription(__u('User in group verified'))
->addDetail(__u('User'), $userDn)
->addDetail(__u('Group'), $this->getGroupFromParams())
)
);

return true;
}

Expand All @@ -174,8 +177,14 @@ public function getGroupMembershipDirectFilter(?string $userDn = null): string
return $this->getUserObjectFilter();
}

return '(&(cn=' . $groupName . ')' . '(|(memberUid=' . $member . ')(member=' . $member . ')(uniqueMember=' . $member . '))' .
$this->getGroupObjectFilter() . ')';
return sprintf(
'(&(cn=%s)(|(memberUid=%s)(member=%s)(uniqueMember=%s))%s)',
$groupName,
$member,
$member,
$member,
$this->getGroupObjectFilter()
);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/SP/Providers/Auth/Ldap/LdapMsAdsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@
class LdapMsAdsTest extends UnitaryTestCase
{

private LdapConnectionInterface|MockObject $ldapConnection;
private LdapActionsInterface|MockObject|LdapMsAds $ldapActions;
private EventDispatcherInterface|MockObject $eventDispatcher;
private LdapMsAds $ldap;
private LdapParams $ldapParams;
private LdapConnectionInterface|MockObject $ldapConnection;
private LdapActionsInterface|MockObject $ldapActions;
private EventDispatcherInterface|MockObject $eventDispatcher;
private LdapMsAds $ldap;
private LdapParams $ldapParams;

public static function groupDataProvider(): array
{
Expand Down
Loading

0 comments on commit d1b0403

Please sign in to comment.