blob: c3911d80e916acdfa38b5fbfb65083962b9d4809 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord\Query\Model;
4
5use Closure;
6use LdapRecord\LdapInterface;
7use LdapRecord\Models\Attributes\AccountControl;
8use LdapRecord\Models\ModelNotFoundException;
9
10class ActiveDirectoryBuilder extends Builder
11{
12 /**
13 * Finds a record by its Object SID.
14 *
15 * @param string $sid
16 * @param array|string $columns
17 *
18 * @return \LdapRecord\Models\ActiveDirectory\Entry|static|null
19 */
20 public function findBySid($sid, $columns = [])
21 {
22 try {
23 return $this->findBySidOrFail($sid, $columns);
24 } catch (ModelNotFoundException $e) {
25 return;
26 }
27 }
28
29 /**
30 * Finds a record by its Object SID.
31 *
32 * Fails upon no records returned.
33 *
34 * @param string $sid
35 * @param array|string $columns
36 *
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020037 * @return \LdapRecord\Models\ActiveDirectory\Entry|static
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010038 *
39 * @throws ModelNotFoundException
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020040 */
41 public function findBySidOrFail($sid, $columns = [])
42 {
43 return $this->findByOrFail('objectsid', $sid, $columns);
44 }
45
46 /**
47 * Adds a enabled filter to the current query.
48 *
49 * @return $this
50 */
51 public function whereEnabled()
52 {
53 return $this->notFilter(function ($query) {
54 return $query->whereDisabled();
55 });
56 }
57
58 /**
59 * Adds a disabled filter to the current query.
60 *
61 * @return $this
62 */
63 public function whereDisabled()
64 {
65 return $this->rawFilter(
66 (new AccountControl())->accountIsDisabled()->filter()
67 );
68 }
69
70 /**
71 * Adds a 'where member' filter to the current query.
72 *
73 * @param string $dn
74 * @param bool $nested
75 *
76 * @return $this
77 */
78 public function whereMember($dn, $nested = false)
79 {
80 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
81 return $this->whereEquals($attribute, $dn);
82 }, 'member', $nested);
83 }
84
85 /**
86 * Adds an 'or where member' filter to the current query.
87 *
88 * @param string $dn
89 * @param bool $nested
90 *
91 * @return $this
92 */
93 public function orWhereMember($dn, $nested = false)
94 {
95 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
96 return $this->orWhereEquals($attribute, $dn);
97 }, 'member', $nested);
98 }
99
100 /**
101 * Adds a 'where member of' filter to the current query.
102 *
103 * @param string $dn
104 * @param bool $nested
105 *
106 * @return $this
107 */
108 public function whereMemberOf($dn, $nested = false)
109 {
110 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
111 return $this->whereEquals($attribute, $dn);
112 }, 'memberof', $nested);
113 }
114
115 /**
116 * Adds a 'where not member of' filter to the current query.
117 *
118 * @param string $dn
119 * @param bool $nested
120 *
121 * @return $this
122 */
123 public function whereNotMemberof($dn, $nested = false)
124 {
125 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
126 return $this->whereNotEquals($attribute, $dn);
127 }, 'memberof', $nested);
128 }
129
130 /**
131 * Adds an 'or where member of' filter to the current query.
132 *
133 * @param string $dn
134 * @param bool $nested
135 *
136 * @return $this
137 */
138 public function orWhereMemberOf($dn, $nested = false)
139 {
140 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
141 return $this->orWhereEquals($attribute, $dn);
142 }, 'memberof', $nested);
143 }
144
145 /**
146 * Adds a 'or where not member of' filter to the current query.
147 *
148 * @param string $dn
149 * @param bool $nested
150 *
151 * @return $this
152 */
153 public function orWhereNotMemberof($dn, $nested = false)
154 {
155 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
156 return $this->orWhereNotEquals($attribute, $dn);
157 }, 'memberof', $nested);
158 }
159
160 /**
161 * Adds a 'where manager' filter to the current query.
162 *
163 * @param string $dn
164 * @param bool $nested
165 *
166 * @return $this
167 */
168 public function whereManager($dn, $nested = false)
169 {
170 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
171 return $this->whereEquals($attribute, $dn);
172 }, 'manager', $nested);
173 }
174
175 /**
176 * Adds a 'where not manager' filter to the current query.
177 *
178 * @param string $dn
179 * @param bool $nested
180 *
181 * @return $this
182 */
183 public function whereNotManager($dn, $nested = false)
184 {
185 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
186 return $this->whereNotEquals($attribute, $dn);
187 }, 'manager', $nested);
188 }
189
190 /**
191 * Adds an 'or where manager' filter to the current query.
192 *
193 * @param string $dn
194 * @param bool $nested
195 *
196 * @return $this
197 */
198 public function orWhereManager($dn, $nested = false)
199 {
200 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
201 return $this->orWhereEquals($attribute, $dn);
202 }, 'manager', $nested);
203 }
204
205 /**
206 * Adds an 'or where not manager' filter to the current query.
207 *
208 * @param string $dn
209 * @param bool $nested
210 *
211 * @return $this
212 */
213 public function orWhereNotManager($dn, $nested = false)
214 {
215 return $this->nestedMatchQuery(function ($attribute) use ($dn) {
216 return $this->orWhereNotEquals($attribute, $dn);
217 }, 'manager', $nested);
218 }
219
220 /**
221 * Execute the callback with a nested match attribute.
222 *
223 * @param Closure $callback
224 * @param string $attribute
225 * @param bool $nested
226 *
227 * @return $this
228 */
229 protected function nestedMatchQuery(Closure $callback, $attribute, $nested = false)
230 {
231 return $callback(
232 $nested ? $this->makeNestedMatchAttribute($attribute) : $attribute
233 );
234 }
235
236 /**
237 * Make a "nested match" filter attribute for querying descendants.
238 *
239 * @param string $attribute
240 *
241 * @return string
242 */
243 protected function makeNestedMatchAttribute($attribute)
244 {
245 return sprintf('%s:%s:', $attribute, LdapInterface::OID_MATCHING_RULE_IN_CHAIN);
246 }
247}