Skip to content

Commit

Permalink
Add extra nested match query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Mar 19, 2021
1 parent e9442ee commit c1f611e
Showing 1 changed file with 107 additions and 23 deletions.
130 changes: 107 additions & 23 deletions src/Query/Model/ActiveDirectoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace LdapRecord\Query\Model;

use Closure;
use LdapRecord\LdapInterface;
use LdapRecord\Models\ModelNotFoundException;
use LdapRecord\Models\Attributes\AccountControl;

Expand Down Expand Up @@ -75,10 +77,9 @@ public function whereDisabled()
*/
public function whereMember($dn, $nested = false)
{
return $this->whereEquals(
$nested ? 'member:1.2.840.113556.1.4.1941:' : 'member',
$dn
);
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->whereEquals($attribute, $dn);
}, 'member', $nested);
}

/**
Expand All @@ -91,10 +92,9 @@ public function whereMember($dn, $nested = false)
*/
public function orWhereMember($dn, $nested = false)
{
return $this->orWhereEquals(
$nested ? 'member:1.2.840.113556.1.4.1941:' : 'member',
$dn
);
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->orWhereEquals($attribute, $dn);
}, 'member', $nested);
}

/**
Expand All @@ -107,10 +107,24 @@ public function orWhereMember($dn, $nested = false)
*/
public function whereMemberOf($dn, $nested = false)
{
return $this->whereEquals(
$nested ? 'memberof:1.2.840.113556.1.4.1941:' : 'memberof',
$dn
);
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->whereEquals($attribute, $dn);
}, 'memberof', $nested);
}

/**
* Adds a 'where not member of' filter to the current query.
*
* @param string $dn
* @param boolean $nested
*
* @return $this
*/
public function whereNotMemberof($dn, $nested = false)
{
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->whereNotEquals($attribute, $dn);
}, 'memberof', $nested);
}

/**
Expand All @@ -123,10 +137,24 @@ public function whereMemberOf($dn, $nested = false)
*/
public function orWhereMemberOf($dn, $nested = false)
{
return $this->orWhereEquals(
$nested ? 'memberof:1.2.840.113556.1.4.1941:' : 'memberof',
$dn
);
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->orWhereEquals($attribute, $dn);
}, 'memberof', $nested);
}

/**
* Adds a 'or where not member of' filter to the current query.
*
* @param string $dn
* @param boolean $nested
*
* @return $this
*/
public function orWhereNotMemberof($dn, $nested = false)
{
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->orWhereNotEquals($attribute, $dn);
}, 'memberof', $nested);
}

/**
Expand All @@ -139,10 +167,24 @@ public function orWhereMemberOf($dn, $nested = false)
*/
public function whereManager($dn, $nested = false)
{
return $this->whereEquals(
$nested ? 'manager:1.2.840.113556.1.4.1941:' : 'manager',
$dn
);
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->whereEquals($attribute, $dn);
}, 'manager', $nested);
}

/**
* Adds a 'where not manager' filter to the current query.
*
* @param string $dn
* @param bool $nested
*
* @return $this
*/
public function whereNotManager($dn, $nested = false)
{
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->whereNotEquals($attribute, $dn);
}, 'manager', $nested);
}

/**
Expand All @@ -155,9 +197,51 @@ public function whereManager($dn, $nested = false)
*/
public function orWhereManager($dn, $nested = false)
{
return $this->orWhereEquals(
$nested ? 'manager:1.2.840.113556.1.4.1941:' : 'manager',
$dn
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->orWhereEquals($attribute, $dn);
}, 'manager', $nested);
}

/**
* Adds an 'or where not manager' filter to the current query.
*
* @param string $dn
* @param bool $nested
*
* @return $this
*/
public function orWhereNotManager($dn, $nested = false)
{
return $this->nestedMatchQuery(function ($attribute) use ($dn) {
return $this->orWhereNotEquals($attribute, $dn);
}, 'manager', $nested);
}

/**
* Execute the callback with a nested match attribute.
*
* @param Clousre $callback
* @param string $attribute
* @param boolean $nested
*
* @return $this
*/
protected function nestedMatchQuery(Closure $callback, $attribute, $nested = false)
{
return $callback(
$nested ? $this->makeNestedMatchAttribute($attribute) : $attribute
);
}

/**
* Make a "nested match" filter attribute for querying descendants.
*
* @param string $attribute
*
* @return string
*/
protected function makeNestedMatchAttribute($attribute)
{
return sprintf('%s:%s:', $attribute, LdapInterface::OID_MATCHING_RULE_IN_CHAIN);
}
}

0 comments on commit c1f611e

Please sign in to comment.