Skip to content

Commit cff35dc

Browse files
authored
Merge pull request #767 from DirectoryTree/bug-766
Bug 766 - {base} inside whereMemberOf doesn't work
2 parents 9eb34c1 + 7167283 commit cff35dc

File tree

2 files changed

+66
-11
lines changed

2 files changed

+66
-11
lines changed

src/Query/Model/ActiveDirectoryBuilder.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function findBySidOrFail(string $sid, array $columns = ['*']): Model
4040
public function whereEnabled(): static
4141
{
4242
return $this->notFilter(
43-
fn ($query) => $query->whereDisabled()
43+
fn (self $query) => $query->whereDisabled()
4444
);
4545
}
4646

@@ -60,7 +60,7 @@ public function whereDisabled(): static
6060
public function whereMember(string $dn, bool $nested = false): static
6161
{
6262
return $this->nestedMatchQuery(
63-
fn ($attribute) => $this->whereEquals($attribute, $dn),
63+
fn (string $attribute) => $this->whereEquals($attribute, $this->substituteBaseDn($dn)),
6464
'member',
6565
$nested
6666
);
@@ -72,7 +72,7 @@ public function whereMember(string $dn, bool $nested = false): static
7272
public function orWhereMember(string $dn, bool $nested = false): static
7373
{
7474
return $this->nestedMatchQuery(
75-
fn ($attribute) => $this->orWhereEquals($attribute, $dn),
75+
fn (string $attribute) => $this->orWhereEquals($attribute, $this->substituteBaseDn($dn)),
7676
'member',
7777
$nested
7878
);
@@ -84,7 +84,7 @@ public function orWhereMember(string $dn, bool $nested = false): static
8484
public function whereMemberOf(string $dn, bool $nested = false): static
8585
{
8686
return $this->nestedMatchQuery(
87-
fn ($attribute) => $this->whereEquals($attribute, $dn),
87+
fn (string $attribute) => $this->whereEquals($attribute, $this->substituteBaseDn($dn)),
8888
'memberof',
8989
$nested
9090
);
@@ -96,7 +96,7 @@ public function whereMemberOf(string $dn, bool $nested = false): static
9696
public function whereNotMemberof(string $dn, bool $nested = false): static
9797
{
9898
return $this->nestedMatchQuery(
99-
fn ($attribute) => $this->whereNotEquals($attribute, $dn),
99+
fn (string $attribute) => $this->whereNotEquals($attribute, $this->substituteBaseDn($dn)),
100100
'memberof',
101101
$nested
102102
);
@@ -108,7 +108,7 @@ public function whereNotMemberof(string $dn, bool $nested = false): static
108108
public function orWhereMemberOf(string $dn, bool $nested = false): static
109109
{
110110
return $this->nestedMatchQuery(
111-
fn ($attribute) => $this->orWhereEquals($attribute, $dn),
111+
fn (string $attribute) => $this->orWhereEquals($attribute, $this->substituteBaseDn($dn)),
112112
'memberof',
113113
$nested
114114
);
@@ -120,7 +120,7 @@ public function orWhereMemberOf(string $dn, bool $nested = false): static
120120
public function orWhereNotMemberof(string $dn, bool $nested = false): static
121121
{
122122
return $this->nestedMatchQuery(
123-
fn ($attribute) => $this->orWhereNotEquals($attribute, $dn),
123+
fn (string $attribute) => $this->orWhereNotEquals($attribute, $this->substituteBaseDn($dn)),
124124
'memberof',
125125
$nested
126126
);
@@ -132,7 +132,7 @@ public function orWhereNotMemberof(string $dn, bool $nested = false): static
132132
public function whereManager(string $dn, bool $nested = false): static
133133
{
134134
return $this->nestedMatchQuery(
135-
fn ($attribute) => $this->whereEquals($attribute, $dn),
135+
fn (string $attribute) => $this->whereEquals($attribute, $this->substituteBaseDn($dn)),
136136
'manager',
137137
$nested
138138
);
@@ -144,7 +144,7 @@ public function whereManager(string $dn, bool $nested = false): static
144144
public function whereNotManager(string $dn, bool $nested = false): static
145145
{
146146
return $this->nestedMatchQuery(
147-
fn ($attribute) => $this->whereNotEquals($attribute, $dn),
147+
fn (string $attribute) => $this->whereNotEquals($attribute, $this->substituteBaseDn($dn)),
148148
'manager',
149149
$nested
150150
);
@@ -156,7 +156,7 @@ public function whereNotManager(string $dn, bool $nested = false): static
156156
public function orWhereManager(string $dn, bool $nested = false): static
157157
{
158158
return $this->nestedMatchQuery(
159-
fn ($attribute) => $this->orWhereEquals($attribute, $dn),
159+
fn (string $attribute) => $this->orWhereEquals($attribute, $this->substituteBaseDn($dn)),
160160
'manager',
161161
$nested
162162
);
@@ -168,7 +168,7 @@ public function orWhereManager(string $dn, bool $nested = false): static
168168
public function orWhereNotManager(string $dn, bool $nested = false): static
169169
{
170170
return $this->nestedMatchQuery(
171-
fn ($attribute) => $this->orWhereNotEquals($attribute, $dn),
171+
fn (string $attribute) => $this->orWhereNotEquals($attribute, $this->substituteBaseDn($dn)),
172172
'manager',
173173
$nested
174174
);

tests/Unit/Query/Model/ActiveDirectoryTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ public function test_where_member_of()
2929
$this->assertEquals('(memberof=cn=Accounting,dc=org,dc=acme)', $b->getUnescapedQuery());
3030
}
3131

32+
public function test_where_member_of_substitutes_base_dn()
33+
{
34+
$b = $this->newBuilder();
35+
$b->setBaseDn('dc=org,dc=acme');
36+
$b->whereMemberOf('cn=Accounting,{base}');
37+
$where = $b->filters['and'][0];
38+
$this->assertEquals('memberof', $where['field']);
39+
$this->assertEquals('=', $where['operator']);
40+
$this->assertEquals(
41+
'\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',
42+
$where['value']
43+
);
44+
$this->assertEquals(
45+
'(memberof=cn=Accounting,dc=org,dc=acme)',
46+
$b->getUnescapedQuery()
47+
);
48+
}
49+
3250
public function test_where_member_of_nested()
3351
{
3452
$b = $this->newBuilder();
@@ -43,6 +61,24 @@ public function test_where_member_of_nested()
4361
$this->assertEquals('(memberof:1.2.840.113556.1.4.1941:=cn=Accounting,dc=org,dc=acme)', $b->getUnescapedQuery());
4462
}
4563

64+
public function test_where_member_of_nested_substitutes_base_dn()
65+
{
66+
$b = $this->newBuilder();
67+
$b->setBaseDn('dc=org,dc=acme');
68+
$b->whereMemberOf('cn=Accounting,{base}', $nested = true);
69+
$where = $b->filters['and'][0];
70+
$this->assertEquals('memberof:1.2.840.113556.1.4.1941:', $where['field']);
71+
$this->assertEquals('=', $where['operator']);
72+
$this->assertEquals(
73+
'\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',
74+
$where['value']
75+
);
76+
$this->assertEquals(
77+
'(memberof:1.2.840.113556.1.4.1941:=cn=Accounting,dc=org,dc=acme)',
78+
$b->getUnescapedQuery()
79+
);
80+
}
81+
4682
public function test_or_where_member_of()
4783
{
4884
$b = $this->newBuilder();
@@ -61,6 +97,25 @@ public function test_or_where_member_of()
6197
);
6298
}
6399

100+
public function test_or_where_member_of_substitutes_base_dn()
101+
{
102+
$b = $this->newBuilder();
103+
$b->setBaseDn('dc=org,dc=acme');
104+
$b->orWhereEquals('cn', 'John Doe');
105+
$b->orWhereMemberOf('cn=Accounting,{base}');
106+
$where = $b->filters['or'][1];
107+
$this->assertEquals('memberof', $where['field']);
108+
$this->assertEquals('=', $where['operator']);
109+
$this->assertEquals(
110+
'\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',
111+
$where['value']
112+
);
113+
$this->assertEquals(
114+
'(|(cn=John Doe)(memberof=cn=Accounting,dc=org,dc=acme))',
115+
$b->getUnescapedQuery()
116+
);
117+
}
118+
64119
public function test_or_where_member_of_nested()
65120
{
66121
$b = $this->newBuilder();

0 commit comments

Comments
 (0)