|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_mapper\Search\Connector; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
|
7
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
|
8
|
|
|
use kalanis\kw_mapper\Records\TFill; |
|
9
|
|
|
use kalanis\kw_mapper\Storage; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class Ldap |
|
14
|
|
|
* @package kalanis\kw_mapper\Search |
|
15
|
|
|
* Connect LDAP as datasource |
|
16
|
|
|
* Lightweight Directory Access Protocol |
|
17
|
|
|
* LDAP is in reality a tree. So it's similar to normal volume and its content. The key is path and value is content. |
|
18
|
|
|
* @codeCoverageIgnore for now - external source |
|
19
|
|
|
*/ |
|
20
|
|
|
class Ldap extends AConnector |
|
21
|
|
|
{ |
|
22
|
|
|
use TFill; |
|
23
|
|
|
|
|
24
|
|
|
protected Storage\Database\Raw\Ldap $database; |
|
25
|
|
|
protected Storage\Database\Dialects\LdapQueries $dialect; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param ARecord $record |
|
29
|
|
|
* @param Storage\Shared\QueryBuilder|null $builder |
|
30
|
|
|
* @throws MapperException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(ARecord $record, ?Storage\Shared\QueryBuilder $builder = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->basicRecord = $record; |
|
35
|
|
|
$this->initRecordLookup($record); |
|
36
|
|
|
$config = Storage\Database\ConfigStorage::getInstance()->getConfig($record->getMapper()->getSource()); |
|
37
|
|
|
$this->database = Storage\Database\DatabaseSingleton::getInstance()->getDatabase($config); |
|
38
|
|
|
$this->dialect = new Storage\Database\Dialects\LdapQueries(); |
|
39
|
|
|
$this->queryBuilder = $builder ?: new Storage\Shared\QueryBuilder(); |
|
40
|
|
|
$this->queryBuilder->setBaseTable($record->getMapper()->getAlias()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getCount(): int |
|
44
|
|
|
{ |
|
45
|
|
|
$entries = $this->multiple(); |
|
46
|
|
|
if (empty($entries) || empty($entries['count'])) { |
|
47
|
|
|
return 0; |
|
48
|
|
|
} |
|
49
|
|
|
return intval($entries['count']); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function getResults(): array |
|
53
|
|
|
{ |
|
54
|
|
|
$lines = $this->multiple(); |
|
55
|
|
|
if (empty($lines)) { |
|
56
|
|
|
return []; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$result = []; |
|
60
|
|
|
$relationMap = array_flip($this->basicRecord->getMapper()->getRelations()); |
|
61
|
|
|
foreach ($lines as $key => $line) { |
|
62
|
|
|
if (is_numeric($key) && is_iterable($line)) { |
|
63
|
|
|
$rec = clone $this->basicRecord; |
|
64
|
|
|
foreach ($line as $index => $item) { |
|
65
|
|
|
$entry = $rec->getEntry(strval($relationMap[$index])); |
|
66
|
|
|
$entry->setData($this->typedFillSelection($entry, $this->readItem($item)), true); |
|
67
|
|
|
} |
|
68
|
|
|
$result[] = $rec; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param mixed $item |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function readItem($item) |
|
79
|
|
|
{ |
|
80
|
|
|
return (empty($item) || empty($item[0]) || ('NULL' == $item[0])) ? '' : $item[0]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @throws MapperException |
|
85
|
|
|
* @return array<string|int, string|int|array<string|int, string|int|float|null>> |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function multiple(): array |
|
88
|
|
|
{ |
|
89
|
|
|
$connect = $this->database->getConnection(); |
|
90
|
|
|
if (!(is_resource($connect) || is_object($connect))) { |
|
91
|
|
|
return []; |
|
92
|
|
|
} |
|
93
|
|
|
$result = ldap_search( |
|
94
|
|
|
$connect, |
|
|
|
|
|
|
95
|
|
|
$this->dialect->domainDn($this->database->getDomain()), |
|
96
|
|
|
$this->dialect->filter($this->queryBuilder) |
|
97
|
|
|
); |
|
98
|
|
|
if (false === $result) { |
|
99
|
|
|
return []; |
|
100
|
|
|
} |
|
101
|
|
|
$items = ldap_get_entries($connect, $result); |
|
|
|
|
|
|
102
|
|
|
return (false !== $items) ? $items : []; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|