|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://github.com/chrmorandi/yii2-ldap for the source repository |
|
4
|
|
|
* @package yii2-ldap |
|
5
|
|
|
* @author Christopher Mota <[email protected]> |
|
6
|
|
|
* @license MIT License - view the LICENSE file that was distributed with this source code. |
|
7
|
|
|
* @since 1.0.0 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace chrmorandi\ldap; |
|
11
|
|
|
|
|
12
|
|
|
use chrmorandi\ldap\Connection; |
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\base\Component; |
|
15
|
|
|
use yii\base\InvalidValueException; |
|
16
|
|
|
use yii\db\Expression; |
|
17
|
|
|
use yii\db\QueryInterface; |
|
18
|
|
|
use yii\db\QueryTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Query represents a SEARCH in LDAP database directory. |
|
22
|
|
|
* |
|
23
|
|
|
* Query provides a set of methods to facilitate the specification of different clauses |
|
24
|
|
|
* in a SEARCH statement. These methods can be chained together. |
|
25
|
|
|
* |
|
26
|
|
|
* For example, |
|
27
|
|
|
* |
|
28
|
|
|
* ```php |
|
29
|
|
|
* $query = new Query; |
|
30
|
|
|
* // compose the query |
|
31
|
|
|
* $query->select('id, name') |
|
32
|
|
|
* ->from('user') |
|
33
|
|
|
* ->limit(10); |
|
34
|
|
|
* // build and execute the query |
|
35
|
|
|
* $rows = $query->all(); |
|
36
|
|
|
* ``` |
|
37
|
|
|
* |
|
38
|
|
|
* Query internally uses the [[FilterBuilder]] class to generate the LDAP filters. |
|
39
|
|
|
* |
|
40
|
|
|
*/ |
|
41
|
|
|
class Query extends Component implements QueryInterface |
|
42
|
|
|
{ |
|
43
|
|
|
|
|
44
|
|
|
use QueryTrait; |
|
45
|
|
|
const SEARCH_SCOPE_SUB = 'ldap_search'; |
|
46
|
|
|
const SEARCH_SCOPE_ONE = 'ldap_list'; |
|
47
|
|
|
const SEARCH_SCOPE_BASE = 'ldap_read'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string the scope of search |
|
51
|
|
|
* The search scope: |
|
52
|
|
|
* Query::SEARCH_SCOPE_SUB searches the complete subtree including the $baseDn node. This is the default value. |
|
53
|
|
|
* Query::SEARCH_SCOPE_ONE restricts search to one level below $baseDn. |
|
54
|
|
|
* Query::SEARCH_SCOPE_BASE restricts search to the $baseDn itself; this can be used to efficiently retrieve a single entry by its DN. |
|
55
|
|
|
*/ |
|
56
|
|
|
public $scope = self::SEARCH_SCOPE_SUB; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array the columns being selected. For example, `['id', 'name']`. |
|
60
|
|
|
* This is used to construct the SEARCH function in a LDAP statement. If not set, it means selecting all columns. |
|
61
|
|
|
* @see select() |
|
62
|
|
|
*/ |
|
63
|
|
|
public $select; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var string The search filter. Format is described in the LDAP documentation. |
|
67
|
|
|
* @see http://www.faqs.org/rfcs/rfc4515.html |
|
68
|
|
|
*/ |
|
69
|
|
|
public $filter; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The distinguished name to perform searches upon. |
|
73
|
|
|
* @var string|null |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $dn; |
|
76
|
|
|
protected $dataReader; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Creates a LDAP command that can be used to execute this query. |
|
80
|
|
|
* @param Connection $db the database connection. |
|
81
|
|
|
* If this parameter is not given, the `db` application component will be used. |
|
82
|
|
|
* @return DataReader |
|
83
|
|
|
*/ |
|
84
|
|
|
public function execute($db = null) |
|
85
|
|
|
{ |
|
86
|
|
|
if ($db === null) { |
|
87
|
|
|
$db = Yii::$app->get('ldap'); |
|
88
|
|
|
} |
|
89
|
|
|
$this->dn = isset($this->dn) ? $this->dn : $db->baseDn; |
|
90
|
|
|
|
|
91
|
|
|
$this->filter = (new FilterBuilder)->build($this->where); |
|
92
|
|
|
if (empty($this->filter)) { |
|
93
|
|
|
throw new InvalidValueException('You must define a filter for the search.'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$select = (is_array($this->select)) ? $this->select : []; |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
if (ctype_digit((string) $this->limit)) { |
|
99
|
|
|
$db->pageSize = $this->limit; |
|
100
|
|
|
} |
|
101
|
|
|
if (ctype_digit((string) $this->offset)) { |
|
102
|
|
|
$db->offset = $this->offset == 0 ? 1 : $this->offset; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$params = [$this->dn, $this->filter, $select, 0, $this->limit, 0]; |
|
106
|
|
|
|
|
107
|
|
|
return $db->executeQuery($this->scope, $params); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Executes the query and returns all results as an array. |
|
112
|
|
|
* @param Connection $db the database connection. |
|
113
|
|
|
* If this parameter is not given, the `db` application component will be used. |
|
114
|
|
|
* @return array the query results. If the query results in nothing, an empty array will be returned. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function all($db = null) |
|
117
|
|
|
{ |
|
118
|
|
|
if (!($this->dataReader instanceof DataReader)) { |
|
119
|
|
|
/** @var $result DataReader */ |
|
120
|
|
|
$this->dataReader = $this->execute($db); |
|
121
|
|
|
} else { |
|
122
|
|
|
if ($db === null) { |
|
123
|
|
|
$db = Yii::$app->get('ldap'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if (ctype_digit((string) $this->limit)) { |
|
127
|
|
|
$db->pageSize = $this->limit; |
|
128
|
|
|
} |
|
129
|
|
|
if (ctype_digit((string) $this->offset)) { |
|
130
|
|
|
$db->offset = $this->offset == 0 ? 1 : $this->offset; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $this->populate($this->dataReader->toArray()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Converts the raw query results into the format as specified by this query. |
|
139
|
|
|
* This method is internally used to convert the data fetched from database |
|
140
|
|
|
* into the format as required by this query. |
|
141
|
|
|
* @param array $rows the raw query result from database |
|
142
|
|
|
* @return array the converted query result |
|
143
|
|
|
*/ |
|
144
|
|
|
public function populate($rows) |
|
145
|
|
|
{ |
|
146
|
|
|
if ($this->indexBy === null) { |
|
147
|
|
|
return $rows; |
|
148
|
|
|
} |
|
149
|
|
|
$result = []; |
|
150
|
|
|
foreach ($rows as $row) { |
|
151
|
|
|
if (is_string($this->indexBy)) { |
|
152
|
|
|
$key = $row[$this->indexBy]; |
|
153
|
|
|
} else { |
|
154
|
|
|
$key = call_user_func($this->indexBy, $row); |
|
155
|
|
|
} |
|
156
|
|
|
$result[$key] = $row; |
|
157
|
|
|
} |
|
158
|
|
|
return $result; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Executes the query and returns a single row of result. |
|
163
|
|
|
* @param Connection $db the database connection. |
|
164
|
|
|
* If this parameter is not given, the `db` application component will be used. |
|
165
|
|
|
* @return array|bool the first row (in terms of an array) of the query result. False is returned if the query |
|
166
|
|
|
* results in nothing. |
|
167
|
|
|
*/ |
|
168
|
|
|
public function one($db = null) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->limit = 1; |
|
171
|
|
|
$result = $this->execute($db); |
|
172
|
|
|
return $result->toArray(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Returns the number of entries in a search. |
|
177
|
|
|
* @param string $q do not use |
|
178
|
|
|
* @param Connection $db the database connection |
|
179
|
|
|
* If this parameter is not given (or null), the `db` application component will be used. |
|
180
|
|
|
* @return integer number of entries. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function count($q = null, $db = NULL) |
|
183
|
|
|
{ |
|
184
|
|
|
$this->limit = 20; |
|
185
|
|
|
$this->dataReader = $this->execute($db); |
|
186
|
|
|
return $this->dataReader->count(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Returns a value indicating whether the query result contains any row of data. |
|
191
|
|
|
* @param Connection $db the database connection. |
|
192
|
|
|
* If this parameter is not given, the `db` application component will be used. |
|
193
|
|
|
* @return bool whether the query result contains any row of entries. |
|
194
|
|
|
*/ |
|
195
|
|
|
public function exists($db = null) |
|
196
|
|
|
{ |
|
197
|
|
|
$result = $this->execute($db); |
|
198
|
|
|
return (bool) $result->count(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Sets the SELECT part of the query. |
|
203
|
|
|
* @param string|array $columns the columns to be selected. |
|
204
|
|
|
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). |
|
205
|
|
|
* |
|
206
|
|
|
* ```php |
|
207
|
|
|
* $query->addSelect(['cn, mail'])->one(); |
|
208
|
|
|
* ``` |
|
209
|
|
|
* |
|
210
|
|
|
* @return $this the query object itself |
|
211
|
|
|
*/ |
|
212
|
|
|
public function select($columns) |
|
213
|
|
|
{ |
|
214
|
|
|
if (!is_array($columns)) { |
|
215
|
|
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
216
|
|
|
} |
|
217
|
|
|
$this->select = $columns; |
|
|
|
|
|
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Add more columns to the select part of the query. |
|
223
|
|
|
* |
|
224
|
|
|
* ```php |
|
225
|
|
|
* $query->addSelect(['cn, mail'])->one(); |
|
226
|
|
|
* ``` |
|
227
|
|
|
* |
|
228
|
|
|
* @param string|array|Expression $columns the columns to add to the select. See [[select()]] for more |
|
229
|
|
|
* details about the format of this parameter. |
|
230
|
|
|
* @return $this the query object itself |
|
231
|
|
|
* @see select() |
|
232
|
|
|
*/ |
|
233
|
|
|
public function addSelect($columns) |
|
234
|
|
|
{ |
|
235
|
|
|
if (!is_array($columns)) { |
|
236
|
|
|
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY); |
|
237
|
|
|
} |
|
238
|
|
|
if ($this->select === null) { |
|
239
|
|
|
$this->select = $columns; |
|
|
|
|
|
|
240
|
|
|
} else { |
|
241
|
|
|
$this->select = array_merge($this->select, $columns); |
|
|
|
|
|
|
242
|
|
|
} |
|
243
|
|
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Adds a filtering condition for a specific column and allow the user to choose a filter operator. |
|
248
|
|
|
* |
|
249
|
|
|
* It adds an additional WHERE condition for the given field and determines the comparison operator |
|
250
|
|
|
* based on the first few characters of the given value. |
|
251
|
|
|
* The condition is added in the same way as in [[andFilterWhere]] so [[isEmpty()|empty values]] are ignored. |
|
252
|
|
|
* The new condition and the existing one will be joined using the 'AND' operator. |
|
253
|
|
|
* |
|
254
|
|
|
* The comparison operator is intelligently determined based on the first few characters in the given value. |
|
255
|
|
|
* In particular, it recognizes the following operators if they appear as the leading characters in the given value: |
|
256
|
|
|
* |
|
257
|
|
|
* - `<`: the column must be less than the given value. |
|
258
|
|
|
* - `>`: the column must be greater than the given value. |
|
259
|
|
|
* - `<=`: the column must be less than or equal to the given value. |
|
260
|
|
|
* - `>=`: the column must be greater than or equal to the given value. |
|
261
|
|
|
* - `~=`: the column must approximate the given value. |
|
262
|
|
|
* - `=`: the column must be equal to the given value. |
|
263
|
|
|
* - If none of the above operators is detected, the `$defaultOperator` will be used. |
|
264
|
|
|
* |
|
265
|
|
|
* @param string $name the column name. |
|
266
|
|
|
* @param string $value the column value optionally prepended with the comparison operator. |
|
267
|
|
|
* @param string $defaultOperator The operator to use, when no operator is given in `$value`. |
|
268
|
|
|
* Defaults to `=`, performing an exact match. |
|
269
|
|
|
* @return $this The query object itself |
|
270
|
|
|
*/ |
|
271
|
|
|
public function andFilterCompare($name, $value, $defaultOperator = '=') |
|
272
|
|
|
{ |
|
273
|
|
|
if (preg_match("/^(~=|>=|>|<=|<|=)/", $value, $matches)) { |
|
274
|
|
|
$operator = $matches[1]; |
|
275
|
|
|
$value = substr($value, strlen($operator)); |
|
276
|
|
|
} else { |
|
277
|
|
|
$operator = $defaultOperator; |
|
278
|
|
|
} |
|
279
|
|
|
return $this->andFilterWhere([$operator, $name, $value]); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Creates a new Query object and copies its property values from an existing one. |
|
284
|
|
|
* The properties being copies are the ones to be used by query builders. |
|
285
|
|
|
* @param Query $from the source query object |
|
286
|
|
|
* @return Query the new Query object |
|
287
|
|
|
*/ |
|
288
|
|
|
public static function create(Query $from) |
|
289
|
|
|
{ |
|
290
|
|
|
return new self([ |
|
291
|
|
|
'where' => $from->where, |
|
292
|
|
|
'limit' => $from->limit, |
|
293
|
|
|
'offset' => $from->offset, |
|
294
|
|
|
'orderBy' => $from->orderBy, |
|
295
|
|
|
'indexBy' => $from->indexBy, |
|
296
|
|
|
'select' => $from->select, |
|
297
|
|
|
]); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
} |
|
301
|
|
|
|