|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fabrica\ActiveDirectory; |
|
3
|
|
|
|
|
4
|
|
|
use Adldap\Adldap; |
|
5
|
|
|
|
|
6
|
|
|
use Cartalyst\Sentinel\Users\EloquentUser; |
|
7
|
|
|
use Fabrica\Acl\Eloquent\Group; |
|
8
|
|
|
|
|
9
|
|
|
use Sentinel; |
|
10
|
|
|
use DB; |
|
11
|
|
|
use Exception; |
|
12
|
|
|
|
|
13
|
|
|
class LDAP |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* connect ldap server. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array $config |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function test($configs) |
|
23
|
|
|
{ |
|
24
|
|
|
$ad = new Adldap(self::filterConnectConfigs($configs)); |
|
25
|
|
|
|
|
26
|
|
|
$ret = []; |
|
27
|
|
|
foreach ($configs as $key => $config) |
|
28
|
|
|
{ |
|
29
|
|
|
$tmp = []; |
|
30
|
|
|
try { |
|
31
|
|
|
$provider = $ad->connect($key); |
|
32
|
|
|
} catch (Exception $e) { |
|
33
|
|
|
$ret[$key] = [ |
|
34
|
|
|
'server_connect' => false, |
|
35
|
|
|
'user_count' => 0, |
|
36
|
|
|
'group_count' => 0, |
|
37
|
|
|
'group_membership' => false ]; |
|
38
|
|
|
continue; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$tmp['server_connect'] = true; |
|
42
|
|
|
|
|
43
|
|
View Code Duplication |
if (!isset($config['user_object_class'])) { |
|
|
|
|
|
|
44
|
|
|
$tmp['user_count'] = 0; |
|
45
|
|
|
} |
|
46
|
|
|
else |
|
47
|
|
|
{ |
|
48
|
|
|
$users = $provider |
|
49
|
|
|
->search() |
|
50
|
|
|
->setDn(self::getUserDn($config)) |
|
51
|
|
|
->rawFilter(isset($config['user_object_filter']) ? $config['user_object_filter'] : ('(objectClass=' . $config['user_object_class'] . ')')) |
|
52
|
|
|
->where('objectClass', $config['user_object_class']) |
|
53
|
|
|
->get(); |
|
54
|
|
|
$tmp['user_count'] = count($users); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
if (!isset($config['group_object_class'])) { |
|
|
|
|
|
|
58
|
|
|
$tmp['group_count'] = 0; |
|
59
|
|
|
} |
|
60
|
|
|
else |
|
61
|
|
|
{ |
|
62
|
|
|
$groups = $provider |
|
63
|
|
|
->search() |
|
64
|
|
|
->setDn(self::getGroupDn($config)) |
|
65
|
|
|
->rawFilter(isset($config['group_object_filter']) ? $config['group_object_filter'] : ('(objectClass=' . $config['group_object_class'] . ')')) |
|
66
|
|
|
->where('objectClass', $configs['default']['group_object_class']) |
|
67
|
|
|
->get(); |
|
68
|
|
|
$tmp['group_count'] = count($groups); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (!isset($config['group_object_class']) || !isset($config['group_membership_attr'])) { |
|
72
|
|
|
$tmp['group_memebership'] = false; |
|
73
|
|
|
} |
|
74
|
|
|
else |
|
75
|
|
|
{ |
|
76
|
|
|
$group = $provider |
|
77
|
|
|
->search() |
|
78
|
|
|
->setDn(self::getGroupDn($config)) |
|
79
|
|
|
->where('objectClass', $config['group_object_class']) |
|
80
|
|
|
->first(); |
|
81
|
|
|
$tmp['group_membership'] = $group ? $group->hasAttribute(strtolower($config['group_membership_attr'])) : false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$ret[$key] = $tmp; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $ret; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* connect ldap server. |
|
92
|
|
|
* |
|
93
|
|
|
* @var array $config |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function sync($configs) |
|
97
|
|
|
{ |
|
98
|
|
|
$ret = []; |
|
99
|
|
|
$ad = new Adldap(self::filterConnectConfigs($configs)); |
|
100
|
|
|
|
|
101
|
|
|
foreach ($configs as $key => $config) |
|
102
|
|
|
{ |
|
103
|
|
|
$tmp = []; |
|
104
|
|
|
try { |
|
105
|
|
|
$provider = $ad->connect($key); |
|
106
|
|
|
} catch (Exception $e) { |
|
107
|
|
|
$tmp['connect'] = $tmp['user'] = $tmp['group'] = false; |
|
108
|
|
|
$ret[$key] = $tmp; |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
$tmp['connect'] = true; |
|
112
|
|
|
// sync the users |
|
113
|
|
|
$tmp['user'] = self::syncUsers($provider, $key, $config); |
|
114
|
|
|
// sync the groups |
|
115
|
|
|
$tmp['group'] = self::syncGroups($provider, $key, $config); |
|
116
|
|
|
|
|
117
|
|
|
$ret[$key] = $tmp; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $ret; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* arrange configs. |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function filterConnectConfigs($configs) |
|
129
|
|
|
{ |
|
130
|
|
|
$connect_configs = []; |
|
131
|
|
|
foreach ($configs as $key => $config) |
|
132
|
|
|
{ |
|
133
|
|
|
$connect = []; |
|
134
|
|
|
$connect['domain_controllers'] = [ $config['host'] ]; |
|
135
|
|
|
$connect['port'] = $config['port']; |
|
136
|
|
|
$connect['admin_username'] = $config['admin_username']; |
|
137
|
|
|
$connect['admin_password'] = $config['admin_password']; |
|
138
|
|
|
if (isset($config['encryption']) && $config['encryption']) { |
|
139
|
|
|
if ($config['encryption'] == 'ssl') { |
|
140
|
|
|
$connect['use_ssl'] = true; |
|
141
|
|
|
} |
|
142
|
|
|
else if ($config['encryption'] == 'tls') { |
|
143
|
|
|
$connect['use_tls'] = true; |
|
144
|
|
|
} |
|
145
|
|
|
else |
|
146
|
|
|
{ |
|
147
|
|
|
$connect['use_ssl'] = false; |
|
148
|
|
|
$connect['use_tls'] = false; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
else |
|
152
|
|
|
{ |
|
153
|
|
|
$connect['use_ssl'] = false; |
|
154
|
|
|
$connect['use_tls'] = false; |
|
155
|
|
|
} |
|
156
|
|
|
$connect_configs[$key] = $connect; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $connect_configs; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* get user dn. |
|
164
|
|
|
* |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
View Code Duplication |
public static function getUserDN($config) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$user_dn = ''; |
|
|
|
|
|
|
170
|
|
|
if (isset($config['additional_user_dn']) && $config['additional_user_dn']) { |
|
171
|
|
|
if (strpos($config['additional_user_dn'], $config['base_dn']) !== false) { |
|
172
|
|
|
$user_dn = $config['additional_user_dn']; |
|
173
|
|
|
} |
|
174
|
|
|
else |
|
175
|
|
|
{ |
|
176
|
|
|
$user_dn = $config['additional_user_dn'] . ',' . $config['base_dn']; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
else |
|
180
|
|
|
{ |
|
181
|
|
|
$user_dn = $config['base_dn']; |
|
182
|
|
|
} |
|
183
|
|
|
return $user_dn; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* get group dn. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
View Code Duplication |
public static function getGroupDN($config) |
|
|
|
|
|
|
192
|
|
|
{ |
|
193
|
|
|
$group_dn = ''; |
|
|
|
|
|
|
194
|
|
|
if (isset($config['additional_group_dn']) && $config['additional_group_dn']) { |
|
195
|
|
|
if (strpos($config['additional_group_dn'], $config['base_dn']) !== false) { |
|
196
|
|
|
$group_dn = $config['additional_group_dn']; |
|
197
|
|
|
} |
|
198
|
|
|
else |
|
199
|
|
|
{ |
|
200
|
|
|
$group_dn = $config['additional_group_dn'] . ',' . $config['base_dn']; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
else |
|
204
|
|
|
{ |
|
205
|
|
|
$group_dn = $config['base_dn']; |
|
206
|
|
|
} |
|
207
|
|
|
return $group_dn; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* sync the users. |
|
212
|
|
|
* |
|
213
|
|
|
* @return bool |
|
214
|
|
|
*/ |
|
215
|
|
|
public static function syncUsers($provider, $directory, $config) |
|
216
|
|
|
{ |
|
217
|
|
|
if (!isset($config['user_object_class'])) { |
|
218
|
|
|
return false; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
// the user sync |
|
222
|
|
|
DB::table('users')->where('directory', $directory)->update([ 'sync_flag' => 1 ]); |
|
223
|
|
|
|
|
224
|
|
|
$users = $provider |
|
225
|
|
|
->search() |
|
226
|
|
|
->setDn(self::getUserDN($config)) |
|
227
|
|
|
->rawFilter(isset($config['user_object_filter']) ? $config['user_object_filter'] : ('(objectClass=' . $config['user_object_class'] . ')')) |
|
228
|
|
|
->where('objectClass', $config['user_object_class']) |
|
229
|
|
|
->get(); |
|
230
|
|
|
foreach ($users as $user) |
|
231
|
|
|
{ |
|
232
|
|
|
$dn = $user->getDn(); |
|
233
|
|
|
$cn = $user->getFirstAttribute(strtolower($config['user_name_attr'])); |
|
234
|
|
|
$email = $user->getFirstAttribute(strtolower($config['user_email_attr'])); |
|
235
|
|
|
|
|
236
|
|
|
$eloquent_user = EloquentUser::where('directory', $directory) |
|
237
|
|
|
->where('ldap_dn', $dn) |
|
238
|
|
|
->first(); |
|
239
|
|
|
if ($eloquent_user) { |
|
240
|
|
|
Sentinel::update( |
|
241
|
|
|
$eloquent_user, [ |
|
242
|
|
|
'first_name' => $cn, |
|
243
|
|
|
'email' => $email, |
|
244
|
|
|
'invalid_flag' => 0, |
|
245
|
|
|
'sync_flag' => 0 ] |
|
246
|
|
|
); |
|
247
|
|
|
} |
|
248
|
|
|
else |
|
249
|
|
|
{ |
|
250
|
|
|
Sentinel::register( |
|
251
|
|
|
[ |
|
252
|
|
|
'directory' => $directory, |
|
253
|
|
|
'ldap_dn' => $dn, |
|
254
|
|
|
'first_name' => $cn, |
|
255
|
|
|
'email' => $email, |
|
256
|
|
|
'password' => md5(rand(10000, 99999)) ], |
|
257
|
|
|
true |
|
258
|
|
|
); |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
// disable the users |
|
262
|
|
|
DB::table('users') |
|
263
|
|
|
->where('directory', $directory) |
|
264
|
|
|
->where('sync_flag', 1) |
|
265
|
|
|
->update([ 'sync_flag' => 0, 'invalid_flag' => 1 ]); |
|
266
|
|
|
|
|
267
|
|
|
return true; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* sync the groups. |
|
272
|
|
|
* |
|
273
|
|
|
* @return bool |
|
274
|
|
|
*/ |
|
275
|
|
|
public static function syncGroups($provider, $directory, $config) |
|
276
|
|
|
{ |
|
277
|
|
|
if (!isset($config['group_object_class'])) { |
|
278
|
|
|
return false; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
$groups = $provider |
|
282
|
|
|
->search() |
|
283
|
|
|
->setDn(self::getGroupDN($config)) |
|
284
|
|
|
->rawFilter(isset($config['group_object_filter']) ? $config['group_object_filter'] : ('(objectClass=' . $config['group_object_class'] . ')')) |
|
285
|
|
|
->where('objectClass', $config['group_object_class']) |
|
286
|
|
|
->get(); |
|
287
|
|
|
|
|
288
|
|
|
foreach ($groups as $group) |
|
289
|
|
|
{ |
|
290
|
|
|
$dn = $group->getDn(); |
|
291
|
|
|
$cn = $group->getFirstAttribute(strtolower($config['group_name_attr'])); |
|
292
|
|
|
$members = $group->getAttribute(strtolower($config['group_membership_attr'])); |
|
293
|
|
|
|
|
294
|
|
|
$users = EloquentUser::where('directory', $directory) |
|
295
|
|
|
->whereIn('ldap_dn', $members) |
|
296
|
|
|
->get([ 'name' ]) |
|
297
|
|
|
->toArray(); |
|
298
|
|
|
|
|
299
|
|
|
$user_ids = []; |
|
300
|
|
|
if ($users) { |
|
301
|
|
|
$user_ids = array_column($users, '_id'); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
$eloquent_group = Group::where('directory', $directory) |
|
305
|
|
|
->where('ldap_dn', $dn) |
|
306
|
|
|
->first(); |
|
307
|
|
|
if ($eloquent_group) { |
|
308
|
|
|
$eloquent_group |
|
309
|
|
|
->fill([ 'name' => $cn, 'users' => $user_ids ]) |
|
310
|
|
|
->save(); |
|
311
|
|
|
} |
|
312
|
|
|
else |
|
313
|
|
|
{ |
|
314
|
|
|
Group::create( |
|
315
|
|
|
[ |
|
316
|
|
|
'name' => $cn, |
|
317
|
|
|
'users' => $user_ids, |
|
318
|
|
|
'ldap_dn' => $dn, |
|
319
|
|
|
'directory' => $directory ] |
|
320
|
|
|
); |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
return true; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* ldap user authenticate. |
|
329
|
|
|
* |
|
330
|
|
|
* @var array $configs |
|
331
|
|
|
* @var string $username |
|
332
|
|
|
* @var string $password |
|
333
|
|
|
* @return object |
|
334
|
|
|
*/ |
|
335
|
|
|
public static function attempt($configs, $username, $password) |
|
336
|
|
|
{ |
|
337
|
|
|
$pass = false; |
|
338
|
|
|
$user = null; |
|
339
|
|
|
|
|
340
|
|
|
$ad = new Adldap(self::filterConnectConfigs($configs)); |
|
341
|
|
|
|
|
342
|
|
|
foreach ($configs as $key => $config) |
|
343
|
|
|
{ |
|
344
|
|
|
try { |
|
345
|
|
|
$provider = $ad->connect($key); |
|
346
|
|
|
|
|
347
|
|
|
$user = EloquentUser::where('directory', $key) |
|
348
|
|
|
->where('email', $username) |
|
349
|
|
|
->first(); |
|
350
|
|
|
|
|
351
|
|
|
if (!$user || !$user->ldap_dn) { |
|
352
|
|
|
continue; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
if ($provider->auth()->attempt($user->ldap_dn, $password)) { |
|
356
|
|
|
$pass = true; |
|
357
|
|
|
break; |
|
358
|
|
|
} |
|
359
|
|
|
} catch (Exception $e) { |
|
360
|
|
|
continue; |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
return $pass ? $user : null; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.