Skip to content

Core\Ldap\Ad::memberOf

Returns a filter that matches the members of a group. Use it with search on a connection, to find the users in a group.

By default the filter matches only the direct members. With { nested: true }, it also matches the members of a group that is itself in the group, at any depth. Only Active Directory understands the nested filter.

Good to know: the group is a Core\Ldap\Dn or a string. A string cannot be tainted. When a part of the DN comes from a user, build it with Core\Ldap\Dn::of.

Parameters

  • $groupCore\Ldap\Dn|stringThe group's DN. A string cannot be tainted. Build the DN with Core\Ldap\Dn::of when a part of it comes from a user.

Options

  • nestedbooloptionalWith true, the filter also matches the members of a group that is itself a member of the group, at any depth. The default is false, which matches only the direct members.

Return value

Returns Core\Ldap\Filter. A Core\Ldap\Filter.

Examples

Direct members

<?nvs
// Shows the filter for the direct members of a group.
var $staff = Core\Ldap\Dn::parse('CN=Staff,OU=Groups,DC=example,DC=test');
// This prints (memberOf=CN=Staff,OU=Groups,DC=example,DC=test).
echo Core\Ldap\Ad::memberOf($staff)->toString(), "\n";
Output
(memberOf=CN=Staff,OU=Groups,DC=example,DC=test)

Nested members

<?nvs
// Shows the filter for every member of a group, through nested groups too.
var $staff = Core\Ldap\Dn::parse('CN=Staff,OU=Groups,DC=example,DC=test');
// The filter uses a rule that only Active Directory understands.
echo Core\Ldap\Ad::memberOf($staff, { nested: true })->toString(), "\n";
Output
(memberOf:1.2.840.113556.1.4.1941:=CN=Staff,OU=Groups,DC=example,DC=test)

Group from a form

<?nvs
// Shows how to build the group's DN when its name comes from a user.
// A name from a form is `tainted`. `child` escapes the comma in it.
tainted string $name = 'Sales, North';
var $group = Core\Ldap\Dn::parse('OU=Groups,DC=example,DC=test')->child('CN', $name);
var $members = Core\Ldap\Filter::all(
Core\Ldap\Filter::equals('objectClass', 'user'),
Core\Ldap\Ad::memberOf($group),
);
echo $members->toString(), "\n";
Output
(&(objectClass=user)(memberOf=CN=Sales\5c, North,OU=Groups,DC=example,DC=test))