This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
/
Builder.php
1145 lines (987 loc) · 26.2 KB
/
Builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Adldap\Query;
use Adldap\Classes\Utilities;
use Adldap\Connections\ConnectionInterface;
use Adldap\Exceptions\InvalidQueryOperatorException;
use Adldap\Exceptions\ModelNotFoundException;
use Adldap\Models\Entry;
use Adldap\Objects\Paginator;
use Adldap\Schemas\ActiveDirectory;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use InvalidArgumentException;
class Builder
{
/**
* The field key for a where statement.
*
* @var string
*/
public static $whereFieldKey = 'field';
/**
* The operator key for a where statement.
*
* @var string
*/
public static $whereOperatorKey = 'operator';
/**
* The value key for a where statement.
*
* @var string
*/
public static $whereValueKey = 'value';
/**
* The available binding types.
*
* @var array
*/
public $bindings = [
'where' => 'wheres',
'orWhere' => 'orWheres',
];
/**
* Stores the column selects to use in the query when assembled.
*
* @var array
*/
public $selects = [];
/**
* Stores the current where filters
* on the current query.
*
* @var array
*/
public $wheres = [];
/**
* Stores the current or where filters
* on the current query.
*
* @var array
*/
public $orWheres = [];
/**
* Stores the raw filters on the current query.
*
* @var array
*/
public $filters = [];
/**
* Stores the bool to determine whether or
* not the current query is paginated.
*
* @var bool
*/
public $paginated = false;
/**
* Stores the field to sort search results by.
*
* @var string
*/
protected $sortByField = '';
/**
* Stores the direction to sort the results by.
*
* @var string
*/
protected $sortByDirection = '';
/**
* The distinguished name to perform searches upon.
*
* @var string|null
*/
protected $dn;
/**
* The object category model class mappings.
*
* @var array
*/
protected $mappings = [
ActiveDirectory::OBJECT_CATEGORY_COMPUTER => 'Adldap\Models\Computer',
ActiveDirectory::OBJECT_CATEGORY_PERSON => 'Adldap\Models\User',
ActiveDirectory::OBJECT_CATEGORY_GROUP => 'Adldap\Models\Group',
ActiveDirectory::MS_EXCHANGE_SERVER => 'Adldap\Models\ExchangeServer',
ActiveDirectory::OBJECT_CATEGORY_CONTAINER => 'Adldap\Models\Container',
ActiveDirectory::OBJECT_CATEGORY_PRINTER => 'Adldap\Models\Printer',
ActiveDirectory::OBJECT_CATEGORY_ORGANIZATIONAL_UNIT => 'Adldap\Models\OrganizationalUnit',
];
/**
* Stores the bool to determine whether or not
* to search LDAP recursively.
*
* @var bool
*/
protected $recursive = true;
/**
* Stores the bool to determine whether or not
* to search LDAP on the base scope.
*
* @var bool
*/
protected $read = false;
/**
* Stores the bool to determine whether or not
* to return LDAP results in their raw format.
*
* @var bool
*/
protected $raw = false;
/**
* Stores the current connection instance.
*
* @var ConnectionInterface
*/
protected $connection;
/**
* Stores the current grammar instance.
*
* @var Grammar
*/
protected $grammar;
/**
* Constructor.
*
* @param ConnectionInterface $connection
* @param Grammar $grammar
*/
public function __construct(ConnectionInterface $connection, Grammar $grammar)
{
$this->connection = $connection;
$this->grammar = $grammar;
}
/**
* Returns a new Query Builder instance.
*
* @return Builder
*/
public function newInstance()
{
$new = new self($this->connection, $this->grammar);
$new->setDn($this->getDn());
return $new;
}
/**
* Returns the current query.
*
* @param string|array $columns
*
* @return array|ArrayCollection|bool
*/
public function get($columns = [])
{
return $this->select($columns)->query($this->getQuery());
}
/**
* Compiles and returns the current query string.
*
* @return string
*/
public function getQuery()
{
return $this->grammar->compileQuery($this);
}
/**
* Returns the current Grammar instance.
*
* @return Grammar
*/
public function getGrammar()
{
return $this->grammar;
}
/**
* Returns the current Connection instance.
*
* @return ConnectionInterface
*/
public function getConnection()
{
return $this->connection;
}
/**
* Returns the builders DN to perform
* searches upon.
*
* @return string
*/
public function getDn()
{
return $this->dn;
}
/**
* Sets the DN to perform searches upon.
*
* @param string|null $dn
*
* @return Builder
*/
public function setDn($dn = null)
{
$this->dn = $dn;
return $this;
}
/**
* Performs the specified query on the current LDAP connection.
*
* @param string $query
*
* @return array|ArrayCollection
*/
public function query($query)
{
$dn = $this->getDn();
$selects = $this->getSelects();
if ($this->read) {
// If read is true, we'll perform a read search, retrieving one record.
$results = $this->connection->read($dn, $query, $selects);
} elseif ($this->recursive) {
// If recursive is true, we'll perform a recursive search.
$results = $this->connection->search($dn, $query, $selects);
} else {
// Read and recursive is false, we'll return a listing.
$results = $this->connection->listing($dn, $query, $selects);
}
return $this->newCollection($this->processResults($results));
}
/**
* Paginates the current LDAP query.
*
* @param int $perPage
* @param int $currentPage
* @param bool $isCritical
*
* @return Paginator|bool
*/
public function paginate($perPage = 50, $currentPage = 0, $isCritical = true)
{
// Set the current query to paginated.
$this->paginated = true;
// Stores all LDAP entries in a page array.
$pages = [];
$cookie = '';
do {
$this->connection->controlPagedResult($perPage, $isCritical, $cookie);
$results = $this->connection->search($this->getDn(), $this->getQuery(), $this->getSelects());
if ($results) {
$this->connection->controlPagedResultResponse($results, $cookie);
// We'll collect the results into the pages array.
$pages[] = $results;
}
} while ($cookie !== null && !empty($cookie));
if (count($pages) > 0) {
return $this->processPaginatedResults($pages, $perPage, $currentPage);
}
return false;
}
/**
* Returns the first entry in a search result.
*
* @param string|array $columns
*
* @return Entry|bool
*/
public function first($columns = [])
{
$results = $this->get($columns);
if ($results instanceof ArrayCollection) {
return $results->first();
} elseif (is_array($results) && array_key_exists(0, $results)) {
return $results[0];
}
// No entries were returned, return false
return false;
}
/**
* Returns the first entry in a search result.
*
* If no entry is found, an exception is thrown.
*
* @throws ModelNotFoundException
*
* @return array|bool
*/
public function firstOrFail()
{
$record = $this->first();
if (!$record) {
$message = 'Unable to find record in Active Directory.';
throw new ModelNotFoundException($message);
}
return $record;
}
/**
* Finds a record using ambiguous name resolution.
*
* @param string $anr
*
* @return bool|Entry
*/
public function find($anr)
{
return $this->whereEquals(ActiveDirectory::ANR, $anr)->first();
}
/**
* Finds a record by the specified attribute and value.
*
* @param string $attribute
* @param string $value
* @param array|string $columns
*
* @return Entry|bool
*/
public function findBy($attribute, $value, $columns = [])
{
return $this->whereEquals($attribute, $value)->first($columns);
}
/**
* Finds a record using ambiguous name resolution. If a record
* is not found, an exception is thrown.
*
* @param string $anr
*
* @throws ModelNotFoundException
*
* @return array|bool
*/
public function findOrFail($anr)
{
$entry = $this->find($anr);
// Make sure we check if the result is an entry or an array before
// we throw an exception in case the user wants raw results.
if (!$entry instanceof Entry && !is_array($entry)) {
$message = 'Unable to find record in Active Directory.';
throw new ModelNotFoundException($message);
}
return $entry;
}
/**
* Finds a record by its distinguished name.
*
* @param string $dn
* @param array $fields
*
* @return bool|Entry
*/
public function findByDn($dn, $fields = [])
{
return $this
->setDn($dn)
->read(true)
->select($fields)
->whereHas(ActiveDirectory::OBJECT_CLASS)
->first();
}
/**
* Finds a record by its distinguished name.
*
* Fails upon no records returned.
*
* @param string $dn
* @param array $fields
*
* @throws ModelNotFoundException
*
* @return bool|Entry
*/
public function findByDnOrFail($dn, $fields = [])
{
return $this
->setDn($dn)
->read(true)
->select($fields)
->whereHas(ActiveDirectory::OBJECT_CLASS)
->firstOrFail();
}
/**
* Finds the Base DN of your domain controller.
*
* @return string|bool
*/
public function findBaseDn()
{
$result = $this
->setDn(null)
->read()
->raw()
->whereHas(ActiveDirectory::OBJECT_CLASS)
->first();
$key = ActiveDirectory::DEFAULT_NAMING_CONTEXT;
if (is_array($result) && array_key_exists($key, $result)) {
if (array_key_exists(0, $result[$key])) {
return $result[$key][0];
}
}
return false;
}
/**
* Adds the inserted fields to query on the current LDAP connection.
*
* @param array|string $fields
*
* @return Builder
*/
public function select($fields = [])
{
if (is_array($fields)) {
foreach ($fields as $field) {
$this->selects[] = $field;
}
} elseif (is_string($fields)) {
$this->selects[] = $fields;
}
return $this;
}
/**
* Adds a raw filter to the current query.
*
* @param string $filter
*
* @return Builder
*/
public function rawFilter($filter)
{
$this->filters[] = $filter;
return $this;
}
/**
* Adds a where clause to the current query.
*
* @param string $field
* @param string|null $operator
* @param string|null $value
*
* @return Builder
*/
public function where($field, $operator = null, $value = null)
{
// If the column is an array, we will assume it is an array of
// key-value pairs and can add them each as a where clause.
if (is_array($field)) {
foreach ($field as $key => $value) {
$this->whereEquals($key, $value);
}
} else {
$this->addBinding($field, $operator, $value, __FUNCTION__);
}
return $this;
}
/**
* Adds a where equals clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function whereEquals($field, $value)
{
$this->where($field, Operator::$equals, $value);
return $this;
}
/**
* Adds a where approximately equals clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function whereApproximatelyEquals($field, $value)
{
$this->where($field, Operator::$approximatelyEquals, $value);
return $this;
}
/**
* Adds a where has clause to the current query.
*
* @param string $field
*
* @return Builder
*/
public function whereHas($field)
{
$this->where($field, Operator::$has);
return $this;
}
/**
* Adds a where not has clause to the current query.
*
* @param string $field
*
* @return Builder
*/
public function whereNotHas($field)
{
$this->where($field, Operator::$notHas);
return $this;
}
/**
* Adds a where contains clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function whereContains($field, $value)
{
$this->where($field, Operator::$contains, $value);
return $this;
}
/**
* Adds a where contains clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function whereNotContains($field, $value)
{
$this->where($field, Operator::$notContains, $value);
return $this;
}
/**
* Adds a where starts with clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function whereStartsWith($field, $value)
{
$this->where($field, Operator::$startsWith, $value);
return $this;
}
/**
* Adds a where *not* starts with clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function whereNotStartsWith($field, $value)
{
$this->where($field, Operator::$notStartsWith, $value);
return $this;
}
/**
* Adds a where ends with clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function whereEndsWith($field, $value)
{
$this->where($field, Operator::$endsWith, $value);
return $this;
}
/**
* Adds an or where clause to the current query.
*
* @param string $field
* @param string|null $operator
* @param string|null $value
*
* @return Builder
*/
public function orWhere($field, $operator = null, $value = null)
{
// If the column is an array, we will assume it is an array of
// key-value pairs and can add them each as a where clause.
if (is_array($field)) {
foreach ($field as $key => $value) {
$this->orWhereEquals($key, $value);
}
} else {
$this->addBinding($field, $operator, $value, __FUNCTION__);
}
return $this;
}
/**
* Adds an or where has clause to the current query.
*
* @param string $field
*
* @return Builder
*/
public function orWhereHas($field)
{
$this->orWhere($field, Operator::$has);
return $this;
}
/**
* Adds a where not has clause to the current query.
*
* @param string $field
*
* @return Builder
*/
public function orWhereNotHas($field)
{
$this->orWhere($field, Operator::$notHas);
return $this;
}
/**
* Adds an or where equals clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function orWhereEquals($field, $value)
{
$this->orWhere($field, Operator::$equals, $value);
return $this;
}
/**
* Adds a or where approximately equals clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function orWhereApproximatelyEquals($field, $value)
{
$this->orWhere($field, Operator::$approximatelyEquals, $value);
return $this;
}
/**
* Adds an or where contains clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function orWhereContains($field, $value)
{
$this->orWhere($field, Operator::$contains, $value);
return $this;
}
/**
* Adds an or where *not* contains clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function orWhereNotContains($field, $value)
{
$this->orWhere($field, Operator::$notContains, $value);
return $this;
}
/**
* Adds an or where starts with clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function orWhereStartsWith($field, $value)
{
$this->orWhere($field, Operator::$startsWith, $value);
return $this;
}
/**
* Adds an or where *not* starts with clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function orWhereNotStartsWith($field, $value)
{
$this->orWhere($field, Operator::$notStartsWith, $value);
return $this;
}
/**
* Adds an or where ends with clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function orWhereEndsWith($field, $value)
{
$this->orWhere($field, Operator::$endsWith, $value);
return $this;
}
/**
* Adds an or where *not* ends with clause to the current query.
*
* @param string $field
* @param string $value
*
* @return Builder
*/
public function orWhereNotEndsWith($field, $value)
{
$this->orWhere($field, Operator::$notEndsWith, $value);
return $this;
}
/**
* Returns true / false depending if the current object
* contains selects.
*
* @return bool
*/
public function hasSelects()
{
if (count($this->selects) > 0) {
return true;
}
return false;
}
/**
* Returns the current selected fields to retrieve.
*
* @return array
*/
public function getSelects()
{
$selects = $this->selects;
if (count($selects) > 0) {
// Always make sure object category, class, and distinguished
// name are included in the selected fields
$selects[] = ActiveDirectory::OBJECT_CATEGORY;
$selects[] = ActiveDirectory::OBJECT_CLASS;
$selects[] = ActiveDirectory::DISTINGUISHED_NAME;
}
return $selects;
}
/**
* Returns the wheres on the current search object.
*
* @return array
*/
public function getWheres()
{
return $this->wheres;
}
/**
* Returns the or wheres on the current search object.
*
* @return array
*/
public function getOrWheres()
{
return $this->orWheres;
}
/**
* Sorts the LDAP search results by the
* specified field and direction.
*
* @param string $field
* @param string $direction
*
* @return Builder
*/
public function sortBy($field, $direction = 'asc')
{
$this->sortByField = $field;
if ($direction === 'asc' || $direction === 'desc') {
$this->sortByDirection = $direction;
}
return $this;
}
/**
* Sets the recursive property to tell the search
* whether or not to search recursively.
*
* @param bool $recursive
*
* @return Builder
*/
public function recursive($recursive = true)
{
$this->recursive = (bool) $recursive;
return $this;
}
/**
* Sets the recursive property to tell the search
* whether or not to search on the base scope and
* return a single entry.
*
* @param bool $read
*
* @return Builder
*/
public function read($read = true)
{
$this->read = (bool) $read;
return $this;
}
/**
* Sets the recursive property to tell the search
* whether or not to return the LDAP results in
* their raw format.
*
* @param bool $raw
*
* @return Builder
*/
public function raw($raw = true)
{
$this->raw = (bool) $raw;
return $this;
}
/**
* Returns a new LDAP Entry instance.
*
* @param array $attributes
*
* @return Entry
*/
public function newLdapEntry(array $attributes = [])
{
$attribute = ActiveDirectory::OBJECT_CATEGORY;
if (array_key_exists($attribute, $attributes) && array_key_exists(0, $attributes[$attribute])) {
// We'll explode the DN so we can grab it's object category.
$category = Utilities::explodeDn($attributes[$attribute][0]);
// Make sure the category string exists in the attribute array
if (array_key_exists(0, $category)) {
$category = strtolower($category[0]);
if (array_key_exists($category, $this->mappings)) {
$model = $this->mappings[$category];
return (new $model([], $this))->setRawAttributes($attributes);
}
}
}
// A default entry object if the object category isn't found
return (new Entry([], $this))->setRawAttributes($attributes);
}
/**
* Returns a new doctrine array collection instance.
*
* @param array $elements
*
* @return ArrayCollection