1
|
|
|
<?php |
2
|
|
|
namespace Auth; |
3
|
|
|
|
4
|
|
|
trait LDAPCachableObject |
5
|
|
|
{ |
6
|
|
|
protected function update($obj) |
7
|
|
|
{ |
8
|
|
|
try |
9
|
|
|
{ |
10
|
|
|
return $this->server->update($obj); |
|
|
|
|
11
|
|
|
} |
12
|
|
|
catch(\Exception $ex) |
13
|
|
|
{ |
14
|
|
|
$auth = \AuthProvider::getInstance(); |
15
|
|
|
$ldap = $auth->getAuthenticator('Auth\LDAPAuthenticator'); |
16
|
|
|
if($ldap === false) return false; |
17
|
|
|
$this->server = $ldap->get_and_bind_server(true); |
18
|
|
|
return $this->server->update($obj); |
19
|
|
|
} |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
protected function getField($fieldName) |
23
|
|
|
{ |
24
|
|
|
if(!is_object($this->ldapObj)) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
return $this->getFieldLocal($fieldName); |
27
|
|
|
} |
28
|
|
|
return $this->getFieldServer($fieldName); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function getFieldSingleValue($fieldName) |
32
|
|
|
{ |
33
|
|
|
if(!is_object($this->ldapObj)) |
34
|
|
|
{ |
35
|
|
|
return $this->getFieldLocalSingleValue($fieldName); |
36
|
|
|
} |
37
|
|
|
return $this->getFieldServerSingleValue($fieldName); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function setField($fieldName, $fieldValue) |
41
|
|
|
{ |
42
|
|
|
if(!is_object($this->ldapObj)) |
43
|
|
|
{ |
44
|
|
|
return $this->setFieldLocal($fieldName, $fieldValue); |
45
|
|
|
} |
46
|
|
|
return $this->setFieldServer($fieldName, $fieldValue); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function appendField($fieldName, $fieldValue) |
50
|
|
|
{ |
51
|
|
|
if(!is_object($this->ldapObj)) |
52
|
|
|
{ |
53
|
|
|
return $this->appendFieldLocal($fieldName, $fieldValue); |
54
|
|
|
} |
55
|
|
|
return $this->appendFieldServer($fieldName, $fieldValue); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function getFieldLocal($fieldName) |
59
|
|
|
{ |
60
|
|
|
if($this->ldapObj === false) |
61
|
|
|
{ |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
if(!isset($this->ldapObj[$fieldName])) |
65
|
|
|
{ |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
return $this->ldapObj[$fieldName]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getFieldServer($fieldName) |
72
|
|
|
{ |
73
|
|
|
$lowerName = strtolower($fieldName); |
74
|
|
|
if(!isset($this->ldapObj->{$lowerName})) |
75
|
|
|
{ |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
return $this->ldapObj->{$lowerName}; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function getFieldLocalSingleValue($fieldName) |
82
|
|
|
{ |
83
|
|
|
if($this->ldapObj === false) |
84
|
|
|
{ |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
if(!isset($this->ldapObj[$fieldName])) |
88
|
|
|
{ |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
if(is_array($this->ldapObj[$fieldName])) |
92
|
|
|
{ |
93
|
|
|
return $this->ldapObj[$fieldName][0]; |
94
|
|
|
} |
95
|
|
|
return $this->ldapObj[$fieldName]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function getFieldServerSingleValue($fieldName) |
99
|
|
|
{ |
100
|
|
|
$lowerName = strtolower($fieldName); |
101
|
|
|
if(!isset($this->ldapObj->{$lowerName})) |
102
|
|
|
{ |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
$field = $this->ldapObj->{$lowerName}; |
106
|
|
|
if(!isset($field[0])) |
107
|
|
|
{ |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
return $field[0]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function setFieldServer($fieldName, $fieldValue) |
114
|
|
|
{ |
115
|
|
|
$obj = array('dn'=>$this->ldapObj->dn); |
116
|
|
|
if($fieldValue !== null && strlen($fieldValue) > 0) |
117
|
|
|
{ |
118
|
|
|
$obj[$fieldName] = $fieldValue; |
119
|
|
|
} |
120
|
|
|
else |
121
|
|
|
{ |
122
|
|
|
$obj[$fieldName] = null; |
123
|
|
|
} |
124
|
|
|
$lowerName = strtolower($fieldName); |
125
|
|
|
$this->ldapObj->{$lowerName} = array($fieldValue); |
126
|
|
|
return $this->update($obj); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function appendFieldServer($fieldName, $fieldValue) |
130
|
|
|
{ |
131
|
|
|
$obj = array('dn'=>$this->ldapObj->dn); |
132
|
|
|
if(isset($this->ldapObj->{$fieldName})) |
133
|
|
|
{ |
134
|
|
|
$obj[$fieldName] = $this->ldapObj->{$fieldName}; |
135
|
|
|
$obj[$fieldName][$obj[$fieldName]['count']] = $fieldValue; |
136
|
|
|
$obj[$fieldName]['count']++; |
137
|
|
|
} |
138
|
|
|
else |
139
|
|
|
{ |
140
|
|
|
$obj[$fieldName] = $fieldValue; |
141
|
|
|
} |
142
|
|
|
return $this->update($obj); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function setFieldLocal($fieldName, $fieldValue) |
146
|
|
|
{ |
147
|
|
|
if($this->ldapObj === false) |
148
|
|
|
{ |
149
|
|
|
$this->ldapObj = array(); |
150
|
|
|
} |
151
|
|
|
if($fieldValue === null || strlen($fieldValue) === 0) |
152
|
|
|
{ |
153
|
|
|
if(isset($this->ldapObj[$fieldName])) |
154
|
|
|
{ |
155
|
|
|
unset($this->ldapObj[$fieldName]); |
156
|
|
|
} |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
$this->ldapObj[$fieldName] = $fieldValue; |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function appendFieldLocal($fieldName, $fieldValue) |
164
|
|
|
{ |
165
|
|
|
if($this->ldapObj === false) |
166
|
|
|
{ |
167
|
|
|
$this->ldapObj = array(); |
168
|
|
|
} |
169
|
|
|
if(!isset($this->ldapObj[$fieldName])) |
170
|
|
|
{ |
171
|
|
|
$this->ldapObj[$fieldName] = array(); |
172
|
|
|
} |
173
|
|
|
$this->ldapObj[$fieldName][] = $fieldValue; |
174
|
|
|
return true; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
179
|
|
|
?> |
|
|
|
|
180
|
|
|
|
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: