blob: 199107efd9d6eb49d22c74b3ed4ed6db585cfcb9 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace Adldap\Query;
4
5use Adldap\Models\RootDse;
6use Adldap\Schemas\ActiveDirectory;
7use Adldap\Schemas\SchemaInterface;
8use Adldap\Connections\ConnectionInterface;
9
10/**
11 * Adldap2 Search Factory.
12 *
13 * Constructs new LDAP queries.
14 *
15 *
16 * @mixin Builder
17 */
18class Factory
19{
20 /**
21 * @var ConnectionInterface
22 */
23 protected $connection;
24
25 /**
26 * Stores the current schema instance.
27 *
28 * @var SchemaInterface
29 */
30 protected $schema;
31
32 /**
33 * The base DN to use for the search.
34 *
35 * @var string|null
36 */
37 protected $base;
38
39 /**
40 * The query cache.
41 *
42 * @var Cache
43 */
44 protected $cache;
45
46 /**
47 * Constructor.
48 *
49 * @param ConnectionInterface $connection The connection to use when constructing a new query.
50 * @param SchemaInterface|null $schema The schema to use for the query and models located.
51 * @param string $baseDn The base DN to use for all searches.
52 */
53 public function __construct(ConnectionInterface $connection, SchemaInterface $schema = null, $baseDn = '')
54 {
55 $this->setConnection($connection)
56 ->setSchema($schema)
57 ->setBaseDn($baseDn);
58 }
59
60 /**
61 * Sets the connection property.
62 *
63 * @param ConnectionInterface $connection
64 *
65 * @return $this
66 */
67 public function setConnection(ConnectionInterface $connection)
68 {
69 $this->connection = $connection;
70
71 return $this;
72 }
73
74 /**
75 * Sets the schema property.
76 *
77 * @param SchemaInterface|null $schema
78 *
79 * @return $this
80 */
81 public function setSchema(SchemaInterface $schema = null)
82 {
83 $this->schema = $schema ?: new ActiveDirectory();
84
85 return $this;
86 }
87
88 /**
89 * Sets the base distinguished name to perform searches upon.
90 *
91 * @param string $base
92 *
93 * @return $this
94 */
95 public function setBaseDn($base = '')
96 {
97 $this->base = $base;
98
99 return $this;
100 }
101
102 /**
103 * Sets the cache for storing query results.
104 *
105 * @param Cache $cache
106 *
107 * @return $this
108 */
109 public function setCache(Cache $cache)
110 {
111 $this->cache = $cache;
112
113 return $this;
114 }
115
116 /**
117 * Returns a new query builder instance.
118 *
119 * @return Builder
120 */
121 public function newQuery()
122 {
123 return $this->newBuilder()->in($this->base);
124 }
125
126 /**
127 * Performs a global 'all' search query on the current
128 * connection by performing a search for all entries
129 * that contain a common name attribute.
130 *
131 * @return \Adldap\Query\Collection|array
132 */
133 public function get()
134 {
135 return $this->newQuery()->whereHas($this->schema->commonName())->get();
136 }
137
138 /**
139 * Returns a query builder scoped to users.
140 *
141 * @return Builder
142 */
143 public function users()
144 {
145 $wheres = [
146 [$this->schema->objectClass(), Operator::$equals, $this->schema->objectClassUser()],
147 [$this->schema->objectCategory(), Operator::$equals, $this->schema->objectCategoryPerson()],
148 ];
149
150 // OpenLDAP doesn't like specifying the omission of user objectclasses
151 // equal to `contact`. We'll make sure we're working with
152 // ActiveDirectory before adding this filter.
153 if (is_a($this->schema, ActiveDirectory::class)) {
154 $wheres[] = [$this->schema->objectClass(), Operator::$doesNotEqual, $this->schema->objectClassContact()];
155 }
156
157 return $this->where($wheres);
158 }
159
160 /**
161 * Returns a query builder scoped to printers.
162 *
163 * @return Builder
164 */
165 public function printers()
166 {
167 return $this->where([
168 $this->schema->objectClass() => $this->schema->objectClassPrinter(),
169 ]);
170 }
171
172 /**
173 * Returns a query builder scoped to organizational units.
174 *
175 * @return Builder
176 */
177 public function ous()
178 {
179 return $this->where([
180 $this->schema->objectClass() => $this->schema->objectClassOu(),
181 ]);
182 }
183
184 /**
185 * Returns a query builder scoped to organizations.
186 *
187 * @return Builder
188 */
189 public function organizations()
190 {
191 return $this->where([
192 $this->schema->objectClass() => $this->schema->objectClassOrganization(),
193 ]);
194 }
195
196 /**
197 * Returns a query builder scoped to groups.
198 *
199 * @return Builder
200 */
201 public function groups()
202 {
203 return $this->where([
204 $this->schema->objectClass() => $this->schema->objectClassGroup(),
205 ]);
206 }
207
208 /**
209 * Returns a query builder scoped to containers.
210 *
211 * @return Builder
212 */
213 public function containers()
214 {
215 return $this->where([
216 $this->schema->objectClass() => $this->schema->objectClassContainer(),
217 ]);
218 }
219
220 /**
221 * Returns a query builder scoped to contacts.
222 *
223 * @return Builder
224 */
225 public function contacts()
226 {
227 return $this->where([
228 $this->schema->objectClass() => $this->schema->objectClassContact(),
229 ]);
230 }
231
232 /**
233 * Returns a query builder scoped to computers.
234 *
235 * @return Builder
236 */
237 public function computers()
238 {
239 return $this->where([
240 $this->schema->objectClass() => $this->schema->objectClassComputer(),
241 ]);
242 }
243
244 /**
245 * Returns the root DSE record.
246 *
247 * @return RootDse|null
248 */
249 public function getRootDse()
250 {
251 $query = $this->newQuery();
252
253 $root = $query->in('')->read()->whereHas($this->schema->objectClass())->first();
254
255 if ($root) {
256 return (new RootDse([], $query))
257 ->setRawAttributes($root->getAttributes());
258 }
259 }
260
261 /**
262 * Handle dynamic method calls on the query builder object.
263 *
264 * @param string $method
265 * @param array $parameters
266 *
267 * @return mixed
268 */
269 public function __call($method, $parameters)
270 {
271 return call_user_func_array([$this->newQuery(), $method], $parameters);
272 }
273
274 /**
275 * Returns a new query grammar instance.
276 *
277 * @return Grammar
278 */
279 protected function newGrammar()
280 {
281 return new Grammar();
282 }
283
284 /**
285 * Returns a new query builder instance.
286 *
287 * @return Builder
288 */
289 protected function newBuilder()
290 {
291 $builder = new Builder($this->connection, $this->newGrammar(), $this->schema);
292
293 $builder->setCache($this->cache);
294
295 return $builder;
296 }
297}