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