1
|
|
|
<?php |
2
|
|
|
namespace Auth; |
3
|
|
|
|
4
|
|
|
trait LDAPCachableObject |
5
|
|
|
{ |
6
|
|
|
protected function initialize($data) |
7
|
|
|
{ |
8
|
|
|
if($data === false) |
9
|
|
|
{ |
10
|
|
|
return; |
11
|
|
|
} |
12
|
|
|
if(is_string($data)) |
13
|
|
|
{ |
14
|
|
|
$this->ldapObj = $this->initializeFromDN($data); |
|
|
|
|
15
|
|
|
return; |
16
|
|
|
} |
17
|
|
|
if($data instanceof \LDAP\LDAPObject) |
18
|
|
|
{ |
19
|
|
|
$this->ldapObj = $data; |
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
$this->ldapObj = $this->initializeFromArray($data); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function initializeFromDN($dn) |
26
|
|
|
{ |
27
|
|
|
$filter = new \Data\Filter("dn eq $dn"); |
28
|
|
|
$data = $this->server->read($this->server->user_base, $filter); |
|
|
|
|
29
|
|
|
if($data === false || !isset($data[0])) |
30
|
|
|
{ |
31
|
|
|
$data = $this->server->read($this->server->group_base, $filter); |
32
|
|
|
if($data === false || !isset($data[0])) |
33
|
|
|
{ |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
return $data[0]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function initializeFromArray($array) |
41
|
|
|
{ |
42
|
|
|
if(isset($array['extended'])) |
43
|
|
|
{ |
44
|
|
|
return $array['extended']; |
45
|
|
|
} |
46
|
|
|
//Generic user object |
47
|
|
|
$filter = new \Data\Filter('mail eq '.$data['mail']); |
|
|
|
|
48
|
|
|
$users = $this->server->read($this->server->user_base, $filter); |
49
|
|
|
if($users === false || !isset($users[0])) |
50
|
|
|
{ |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
return $users[0]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function update($obj) |
57
|
|
|
{ |
58
|
|
|
try |
59
|
|
|
{ |
60
|
|
|
return $this->server->update($obj); |
61
|
|
|
} |
62
|
|
|
catch(\Exception $ex) |
63
|
|
|
{ |
64
|
|
|
$auth = \AuthProvider::getInstance(); |
65
|
|
|
$ldap = $auth->getMethodByName('Auth\LDAPAuthenticator'); |
66
|
|
|
if($ldap === false) |
67
|
|
|
{ |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
$this->server = $ldap->get_and_bind_server(true); |
71
|
|
|
return $this->server->update($obj); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the specified field from the cached object or LDAPObject |
77
|
|
|
* |
78
|
|
|
* @param string $fieldName The name of the field to retrieve |
79
|
|
|
* |
80
|
|
|
* @return mixed string|array the value of the field |
81
|
|
|
*/ |
82
|
|
|
protected function getField($fieldName) |
83
|
|
|
{ |
84
|
|
|
if(!is_object($this->ldapObj)) |
85
|
|
|
{ |
86
|
|
|
return $this->getFieldLocal($fieldName); |
87
|
|
|
} |
88
|
|
|
return $this->getFieldServer($fieldName); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the value of the specified field from the cached object or LDAPObject |
93
|
|
|
* |
94
|
|
|
* @param string $fieldName The name of the field to retrieve |
95
|
|
|
* |
96
|
|
|
* @return mixed string the value of the field |
97
|
|
|
*/ |
98
|
|
|
protected function getFieldSingleValue($fieldName) |
99
|
|
|
{ |
100
|
|
|
if(!is_object($this->ldapObj)) |
101
|
|
|
{ |
102
|
|
|
return $this->getFieldLocalSingleValue($fieldName); |
103
|
|
|
} |
104
|
|
|
return $this->getFieldServerSingleValue($fieldName); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the value of the specified field in the cached object or LDAPObject |
109
|
|
|
* |
110
|
|
|
* @param string $fieldName The name of the field to set |
111
|
|
|
* @param mixed $fieldValue The value to set in the field |
112
|
|
|
* |
113
|
|
|
* @return boolean true if the field is set and false otherwise |
114
|
|
|
*/ |
115
|
|
|
protected function setField($fieldName, $fieldValue) |
116
|
|
|
{ |
117
|
|
|
if(!is_object($this->ldapObj)) |
118
|
|
|
{ |
119
|
|
|
return $this->setFieldLocal($fieldName, $fieldValue); |
120
|
|
|
} |
121
|
|
|
return $this->setFieldServer($fieldName, $fieldValue); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Append a value of the specified field in the cached object or LDAPObject |
126
|
|
|
* |
127
|
|
|
* @param string $fieldName The name of the field to set |
128
|
|
|
* @param mixed $fieldValue The value to append to the field |
129
|
|
|
* |
130
|
|
|
* @return boolean true if the field is set and false otherwise |
131
|
|
|
*/ |
132
|
|
|
protected function appendField($fieldName, $fieldValue) |
133
|
|
|
{ |
134
|
|
|
if(!is_object($this->ldapObj)) |
135
|
|
|
{ |
136
|
|
|
return $this->appendFieldLocal($fieldName, $fieldValue); |
137
|
|
|
} |
138
|
|
|
return $this->appendFieldServer($fieldName, $fieldValue); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the value of the field in the local cache |
143
|
|
|
* |
144
|
|
|
* @param string $fieldName The name of the field to retrieve |
145
|
|
|
* |
146
|
|
|
* @return mixed the value of the field |
147
|
|
|
*/ |
148
|
|
|
private function getFieldLocal($fieldName) |
149
|
|
|
{ |
150
|
|
|
if($this->ldapObj === false) |
151
|
|
|
{ |
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
if(!isset($this->ldapObj[$fieldName])) |
155
|
|
|
{ |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
return $this->ldapObj[$fieldName]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the value of the field in the server object |
163
|
|
|
* |
164
|
|
|
* @param string $fieldName The name of the field to retrieve |
165
|
|
|
* |
166
|
|
|
* @return mixed the value of the field |
167
|
|
|
*/ |
168
|
|
|
private function getFieldServer($fieldName) |
169
|
|
|
{ |
170
|
|
|
$lowerName = strtolower($fieldName); |
171
|
|
|
if(!isset($this->ldapObj->{$lowerName})) |
172
|
|
|
{ |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
return $this->ldapObj->{$lowerName}; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the value of the specified field from the local cache |
180
|
|
|
* |
181
|
|
|
* @param string $fieldName The name of the field to retrieve |
182
|
|
|
* |
183
|
|
|
* @return string the value of the field |
184
|
|
|
*/ |
185
|
|
|
private function getFieldLocalSingleValue($fieldName) |
186
|
|
|
{ |
187
|
|
|
if($this->ldapObj === false) |
188
|
|
|
{ |
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
if(!isset($this->ldapObj[$fieldName])) |
192
|
|
|
{ |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
if(is_array($this->ldapObj[$fieldName])) |
196
|
|
|
{ |
197
|
|
|
return $this->ldapObj[$fieldName][0]; |
198
|
|
|
} |
199
|
|
|
return $this->ldapObj[$fieldName]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get the value of the specified field from the server |
204
|
|
|
* |
205
|
|
|
* @param string $fieldName The name of the field to retrieve |
206
|
|
|
* |
207
|
|
|
* @return string the value of the field |
208
|
|
|
*/ |
209
|
|
|
private function getFieldServerSingleValue($fieldName) |
210
|
|
|
{ |
211
|
|
|
$lowerName = strtolower($fieldName); |
212
|
|
|
if(!isset($this->ldapObj->{$lowerName})) |
213
|
|
|
{ |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
$field = $this->ldapObj->{$lowerName}; |
217
|
|
|
if(!isset($field[0])) |
218
|
|
|
{ |
219
|
|
|
return false; |
220
|
|
|
} |
221
|
|
|
return $field[0]; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Set the specified field in the server |
226
|
|
|
* |
227
|
|
|
* @param string $fieldName The name of the field to set |
228
|
|
|
* @param mixed $fieldValue The value to write to the field |
229
|
|
|
* |
230
|
|
|
* @return boolean true if the field is set and false otherwise |
231
|
|
|
*/ |
232
|
|
|
private function setFieldServer($fieldName, $fieldValue) |
233
|
|
|
{ |
234
|
|
|
$obj = array('dn'=>$this->ldapObj->dn); |
235
|
|
|
if($fieldValue !== null && strlen($fieldValue) > 0) |
236
|
|
|
{ |
237
|
|
|
$obj[$fieldName] = $fieldValue; |
238
|
|
|
} |
239
|
|
|
else |
240
|
|
|
{ |
241
|
|
|
$obj[$fieldName] = null; |
242
|
|
|
} |
243
|
|
|
$lowerName = strtolower($fieldName); |
244
|
|
|
$this->ldapObj->{$lowerName} = array($fieldValue); |
245
|
|
|
return $this->update($obj); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Append a value of the specified field in the server |
250
|
|
|
* |
251
|
|
|
* @param string $fieldName The name of the field to set |
252
|
|
|
* @param mixed $fieldValue The value to append to the field |
253
|
|
|
* |
254
|
|
|
* @return boolean true if the field is set and false otherwise |
255
|
|
|
*/ |
256
|
|
|
private function appendFieldServer($fieldName, $fieldValue) |
257
|
|
|
{ |
258
|
|
|
$obj = array('dn'=>$this->ldapObj->dn); |
259
|
|
|
if(isset($this->ldapObj->{$fieldName})) |
260
|
|
|
{ |
261
|
|
|
$obj[$fieldName] = $this->ldapObj->{$fieldName}; |
262
|
|
|
$obj[$fieldName][$obj[$fieldName]['count']] = $fieldValue; |
263
|
|
|
$obj[$fieldName]['count']++; |
264
|
|
|
} |
265
|
|
|
else |
266
|
|
|
{ |
267
|
|
|
$obj[$fieldName] = $fieldValue; |
268
|
|
|
} |
269
|
|
|
return $this->update($obj); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Set the specified field in the local cache |
274
|
|
|
* |
275
|
|
|
* @param string $fieldName The name of the field to set |
276
|
|
|
* @param mixed $fieldValue The value to write to the field |
277
|
|
|
* |
278
|
|
|
* @return boolean true if the field is set and false otherwise |
279
|
|
|
*/ |
280
|
|
|
private function setFieldLocal($fieldName, $fieldValue) |
281
|
|
|
{ |
282
|
|
|
if($this->ldapObj === false) |
283
|
|
|
{ |
284
|
|
|
$this->ldapObj = array(); |
285
|
|
|
} |
286
|
|
|
if($fieldValue === null || (is_string($fieldValue) && strlen($fieldValue) === 0)) |
287
|
|
|
{ |
288
|
|
|
if(isset($this->ldapObj[$fieldName])) |
289
|
|
|
{ |
290
|
|
|
unset($this->ldapObj[$fieldName]); |
291
|
|
|
} |
292
|
|
|
return true; |
293
|
|
|
} |
294
|
|
|
$this->ldapObj[$fieldName] = $fieldValue; |
295
|
|
|
return true; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Append a value of the specified field in the local cache |
300
|
|
|
* |
301
|
|
|
* @param string $fieldName The name of the field to set |
302
|
|
|
* @param mixed $fieldValue The value to append to the field |
303
|
|
|
* |
304
|
|
|
* @return boolean true if the field is set and false otherwise |
305
|
|
|
*/ |
306
|
|
|
private function appendFieldLocal($fieldName, $fieldValue) |
307
|
|
|
{ |
308
|
|
|
if($this->ldapObj === false) |
309
|
|
|
{ |
310
|
|
|
$this->ldapObj = array(); |
311
|
|
|
} |
312
|
|
|
if(!isset($this->ldapObj[$fieldName])) |
313
|
|
|
{ |
314
|
|
|
$this->ldapObj[$fieldName] = array(); |
315
|
|
|
} |
316
|
|
|
$this->ldapObj[$fieldName][] = $fieldValue; |
317
|
|
|
return true; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
322
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: