forked from chrmorandi/yii2-ldap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query.php
305 lines (275 loc) · 10.4 KB
/
Query.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
<?php
/**
* @link https://github.com/chrmorandi/yii2-ldap for the canonical source repository
* @package yii2-ldap
* @author Christopher Mota <[email protected]>
* @license Mit License - view the LICENSE file that was distributed with this source code.
*/
namespace chrmorandi\ldap;
use chrmorandi\ldap\Connection;
use Yii;
use yii\base\Component;
use yii\base\InvalidValueException;
use yii\db\Expression;
use yii\db\QueryInterface;
use yii\db\QueryTrait;
/**
* Query represents a SEARCH in LDAP database directory.
*
* Query provides a set of methods to facilitate the specification of different clauses
* in a SEARCH statement. These methods can be chained together.
*
* For example,
*
* ```php
* $query = new Query;
* // compose the query
* $query->select('id, name')
* ->from('user')
* ->limit(10);
* // build and execute the query
* $rows = $query->all();
* ```
*
* Query internally uses the [[FilterBuilder]] class to generate the LDAP filters.
*
* @author Christopher Mota <[email protected]>
* @since 1.0.0
*/
class Query extends Component implements QueryInterface
{
use QueryTrait;
const SEARCH_SCOPE_SUB = 'ldap_search';
const SEARCH_SCOPE_ONE = 'ldap_list';
const SEARCH_SCOPE_BASE = 'ldap_read';
/**
* @var string the scope of search
* The search scope:
* Query::SEARCH_SCOPE_SUB searches the complete subtree including the $baseDn node. This is the default value.
* Query::SEARCH_SCOPE_ONE restricts search to one level below $baseDn.
* Query::SEARCH_SCOPE_BASE restricts search to the $baseDn itself; this can be used to efficiently retrieve a single entry by its DN.
*/
public $scope = self::SEARCH_SCOPE_SUB;
/**
* @var array the columns being selected. For example, `['id', 'name']`.
* This is used to construct the SEARCH function in a LDAP statement. If not set, it means selecting all columns.
* @see select()
*/
public $select;
/**
* @var string The search filter. Format is described in the LDAP documentation.
* @see http://www.faqs.org/rfcs/rfc4515.html
*/
public $filter;
/**
* The distinguished name to perform searches upon.
* @var string|null
*/
protected $dn;
protected $dataReader;
/**
* Creates a LDAP command that can be used to execute this query.
* @param Connection $db the database connection.
* If this parameter is not given, the `db` application component will be used.
* @return DataReader
*/
public function execute($db = null)
{
if ($db === null) {
$db = Yii::$app->get('ldap');
}
$this->dn = isset($this->dn) ? $this->dn : $db->baseDn;
$this->filter = (new FilterBuilder)->build($this->where);
if (empty($this->filter)) {
throw new InvalidValueException('You must define a filter for the search.');
}
$select = (is_array($this->select)) ? $this->select : [];
if(ctype_digit((string) $this->limit)){
$db->pageSize = $this->limit;
}
if(ctype_digit((string) $this->offset)){
$db->offset = $this->offset == 0 ? 1 : $this->offset;
}
$params = [$this->dn, $this->filter, $select, 0, $this->limit, 0];
return $db->executeQuery($this->scope, $params);
}
/**
* Executes the query and returns all results as an array.
* @param Connection $db the database connection.
* If this parameter is not given, the `db` application component will be used.
* @return array the query results. If the query results in nothing, an empty array will be returned.
*/
public function all($db = null)
{
if(!($this->dataReader instanceof DataReader)) {
/** @var $result DataReader */
$this->dataReader = $this->execute($db);
} else {
if ($db === null) {
$db = Yii::$app->get('ldap');
}
if(ctype_digit((string) $this->limit)){
$db->pageSize = $this->limit;
}
if(ctype_digit((string) $this->offset)){
$db->offset = $this->offset == 0 ? 1 : $this->offset;
}
}
return $this->populate($this->dataReader->toArray());
}
/**
* Converts the raw query results into the format as specified by this query.
* This method is internally used to convert the data fetched from database
* into the format as required by this query.
* @param array $rows the raw query result from database
* @return array the converted query result
*/
public function populate($rows)
{
if ($this->indexBy === null) {
return $rows;
}
$result = [];
foreach ($rows as $row) {
if (is_string($this->indexBy)) {
$key = $row[$this->indexBy];
} else {
$key = call_user_func($this->indexBy, $row);
}
$result[$key] = $row;
}
return $result;
}
/**
* Executes the query and returns a single row of result.
* @param Connection $db the database connection.
* If this parameter is not given, the `db` application component will be used.
* @return array|boolean the first row (in terms of an array) of the query result. False is returned if the query
* results in nothing.
*/
public function one($db = null)
{
$this->limit = 1;
$result = $this->execute($db);
return $result->toArray();
}
/**
* Returns the number of entries in a search.
* @param string $q do not use
* @param Connection $db the database connection
* If this parameter is not given (or null), the `db` application component will be used.
* @return integer number of entries.
*/
public function count($q = null, $db = NULL)
{
$this->limit = 20;
$this->dataReader = $this->execute($db);
return $this->dataReader->count();
}
/**
* Returns a value indicating whether the query result contains any row of data.
* @param Connection $db the database connection.
* If this parameter is not given, the `db` application component will be used.
* @return boolean whether the query result contains any row of entries.
*/
public function exists($db = null)
{
$result = $this->execute($db);
return (boolean) $result->count();
}
/**
* Sets the SELECT part of the query.
* @param string|array $columns the columns to be selected.
* Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']).
*
* ```php
* $query->addSelect(['cn, mail'])->one();
* ```
*
* @return $this the query object itself
*/
public function select($columns)
{
if (!is_array($columns)) {
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
}
$this->select = $columns;
return $this;
}
/**
* Add more columns to the select part of the query.
*
* ```php
* $query->addSelect(['cn, mail'])->one();
* ```
*
* @param string|array|Expression $columns the columns to add to the select. See [[select()]] for more
* details about the format of this parameter.
* @return $this the query object itself
* @see select()
*/
public function addSelect($columns)
{
if (!is_array($columns)) {
$columns = preg_split('/\s*,\s*/', trim($columns), -1, PREG_SPLIT_NO_EMPTY);
}
if ($this->select === null) {
$this->select = $columns;
} else {
$this->select = array_merge($this->select, $columns);
}
return $this;
}
/**
* Adds a filtering condition for a specific column and allow the user to choose a filter operator.
*
* It adds an additional WHERE condition for the given field and determines the comparison operator
* based on the first few characters of the given value.
* The condition is added in the same way as in [[andFilterWhere]] so [[isEmpty()|empty values]] are ignored.
* The new condition and the existing one will be joined using the 'AND' operator.
*
* The comparison operator is intelligently determined based on the first few characters in the given value.
* In particular, it recognizes the following operators if they appear as the leading characters in the given value:
*
* - `<`: the column must be less than the given value.
* - `>`: the column must be greater than the given value.
* - `<=`: the column must be less than or equal to the given value.
* - `>=`: the column must be greater than or equal to the given value.
* - `~=`: the column must approximate the given value.
* - `=`: the column must be equal to the given value.
* - If none of the above operators is detected, the `$defaultOperator` will be used.
*
* @param string $name the column name.
* @param string $value the column value optionally prepended with the comparison operator.
* @param string $defaultOperator The operator to use, when no operator is given in `$value`.
* Defaults to `=`, performing an exact match.
* @return $this The query object itself
*/
public function andFilterCompare($name, $value, $defaultOperator = '=')
{
if (preg_match("/^(~=|>=|>|<=|<|=)/", $value, $matches)) {
$operator = $matches[1];
$value = substr($value, strlen($operator));
} else {
$operator = $defaultOperator;
}
return $this->andFilterWhere([$operator, $name, $value]);
}
/**
* Creates a new Query object and copies its property values from an existing one.
* The properties being copies are the ones to be used by query builders.
* @param Query $from the source query object
* @return Query the new Query object
*/
public static function create(Query $from)
{
return new self([
'where' => $from->where,
'limit' => $from->limit,
'offset' => $from->offset,
'orderBy' => $from->orderBy,
'indexBy' => $from->indexBy,
'select' => $from->select,
]);
}
}