From be9d4600c68ff0356307b2c488424d855659e85a Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 20 May 2025 16:47:32 -0400 Subject: [PATCH 1/2] Substitute base DN on whereMember methods --- src/Query/Model/ActiveDirectoryBuilder.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Query/Model/ActiveDirectoryBuilder.php b/src/Query/Model/ActiveDirectoryBuilder.php index b360b02d..7fb8ab68 100644 --- a/src/Query/Model/ActiveDirectoryBuilder.php +++ b/src/Query/Model/ActiveDirectoryBuilder.php @@ -40,7 +40,7 @@ public function findBySidOrFail(string $sid, array $columns = ['*']): Model public function whereEnabled(): static { return $this->notFilter( - fn ($query) => $query->whereDisabled() + fn (self $query) => $query->whereDisabled() ); } @@ -60,7 +60,7 @@ public function whereDisabled(): static public function whereMember(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->whereEquals($attribute, $dn), + fn (string $attribute) => $this->whereEquals($attribute, $this->substituteBaseDn($dn)), 'member', $nested ); @@ -72,7 +72,7 @@ public function whereMember(string $dn, bool $nested = false): static public function orWhereMember(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->orWhereEquals($attribute, $dn), + fn (string $attribute) => $this->orWhereEquals($attribute, $this->substituteBaseDn($dn)), 'member', $nested ); @@ -84,7 +84,7 @@ public function orWhereMember(string $dn, bool $nested = false): static public function whereMemberOf(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->whereEquals($attribute, $dn), + fn (string $attribute) => $this->whereEquals($attribute, $this->substituteBaseDn($dn)), 'memberof', $nested ); @@ -96,7 +96,7 @@ public function whereMemberOf(string $dn, bool $nested = false): static public function whereNotMemberof(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->whereNotEquals($attribute, $dn), + fn (string $attribute) => $this->whereNotEquals($attribute, $this->substituteBaseDn($dn)), 'memberof', $nested ); @@ -108,7 +108,7 @@ public function whereNotMemberof(string $dn, bool $nested = false): static public function orWhereMemberOf(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->orWhereEquals($attribute, $dn), + fn (string $attribute) => $this->orWhereEquals($attribute, $this->substituteBaseDn($dn)), 'memberof', $nested ); @@ -120,7 +120,7 @@ public function orWhereMemberOf(string $dn, bool $nested = false): static public function orWhereNotMemberof(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->orWhereNotEquals($attribute, $dn), + fn (string $attribute) => $this->orWhereNotEquals($attribute, $this->substituteBaseDn($dn)), 'memberof', $nested ); @@ -132,7 +132,7 @@ public function orWhereNotMemberof(string $dn, bool $nested = false): static public function whereManager(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->whereEquals($attribute, $dn), + fn (string $attribute) => $this->whereEquals($attribute, $this->substituteBaseDn($dn)), 'manager', $nested ); @@ -144,7 +144,7 @@ public function whereManager(string $dn, bool $nested = false): static public function whereNotManager(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->whereNotEquals($attribute, $dn), + fn (string $attribute) => $this->whereNotEquals($attribute, $this->substituteBaseDn($dn)), 'manager', $nested ); @@ -156,7 +156,7 @@ public function whereNotManager(string $dn, bool $nested = false): static public function orWhereManager(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->orWhereEquals($attribute, $dn), + fn (string $attribute) => $this->orWhereEquals($attribute, $this->substituteBaseDn($dn)), 'manager', $nested ); @@ -168,7 +168,7 @@ public function orWhereManager(string $dn, bool $nested = false): static public function orWhereNotManager(string $dn, bool $nested = false): static { return $this->nestedMatchQuery( - fn ($attribute) => $this->orWhereNotEquals($attribute, $dn), + fn (string $attribute) => $this->orWhereNotEquals($attribute, $this->substituteBaseDn($dn)), 'manager', $nested ); From 7167283baf208334546a8bed053e8e9c80709fce Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 20 May 2025 16:47:35 -0400 Subject: [PATCH 2/2] Add tests --- .../Unit/Query/Model/ActiveDirectoryTest.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/Unit/Query/Model/ActiveDirectoryTest.php b/tests/Unit/Query/Model/ActiveDirectoryTest.php index 9e7adaa2..379b7373 100644 --- a/tests/Unit/Query/Model/ActiveDirectoryTest.php +++ b/tests/Unit/Query/Model/ActiveDirectoryTest.php @@ -29,6 +29,24 @@ public function test_where_member_of() $this->assertEquals('(memberof=cn=Accounting,dc=org,dc=acme)', $b->getUnescapedQuery()); } + public function test_where_member_of_substitutes_base_dn() + { + $b = $this->newBuilder(); + $b->setBaseDn('dc=org,dc=acme'); + $b->whereMemberOf('cn=Accounting,{base}'); + $where = $b->filters['and'][0]; + $this->assertEquals('memberof', $where['field']); + $this->assertEquals('=', $where['operator']); + $this->assertEquals( + '\63\6e\3d\41\63\63\6f\75\6e\74\69\6e\67\2c\64\63\3d\6f\72\67\2c\64\63\3d\61\63\6d\65', + $where['value'] + ); + $this->assertEquals( + '(memberof=cn=Accounting,dc=org,dc=acme)', + $b->getUnescapedQuery() + ); + } + public function test_where_member_of_nested() { $b = $this->newBuilder(); @@ -43,6 +61,24 @@ public function test_where_member_of_nested() $this->assertEquals('(memberof:1.2.840.113556.1.4.1941:=cn=Accounting,dc=org,dc=acme)', $b->getUnescapedQuery()); } + public function test_where_member_of_nested_substitutes_base_dn() + { + $b = $this->newBuilder(); + $b->setBaseDn('dc=org,dc=acme'); + $b->whereMemberOf('cn=Accounting,{base}', $nested = true); + $where = $b->filters['and'][0]; + $this->assertEquals('memberof:1.2.840.113556.1.4.1941:', $where['field']); + $this->assertEquals('=', $where['operator']); + $this->assertEquals( + '\63\6e\3d\41\63\63\6f\75\6e\74\69\6e\67\2c\64\63\3d\6f\72\67\2c\64\63\3d\61\63\6d\65', + $where['value'] + ); + $this->assertEquals( + '(memberof:1.2.840.113556.1.4.1941:=cn=Accounting,dc=org,dc=acme)', + $b->getUnescapedQuery() + ); + } + public function test_or_where_member_of() { $b = $this->newBuilder(); @@ -61,6 +97,25 @@ public function test_or_where_member_of() ); } + public function test_or_where_member_of_substitutes_base_dn() + { + $b = $this->newBuilder(); + $b->setBaseDn('dc=org,dc=acme'); + $b->orWhereEquals('cn', 'John Doe'); + $b->orWhereMemberOf('cn=Accounting,{base}'); + $where = $b->filters['or'][1]; + $this->assertEquals('memberof', $where['field']); + $this->assertEquals('=', $where['operator']); + $this->assertEquals( + '\63\6e\3d\41\63\63\6f\75\6e\74\69\6e\67\2c\64\63\3d\6f\72\67\2c\64\63\3d\61\63\6d\65', + $where['value'] + ); + $this->assertEquals( + '(|(cn=John Doe)(memberof=cn=Accounting,dc=org,dc=acme))', + $b->getUnescapedQuery() + ); + } + public function test_or_where_member_of_nested() { $b = $this->newBuilder();