1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fabrica\Http\Api; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Support\Facades\Event; |
7
|
|
|
|
8
|
|
|
use Fabrica\Http\Requests; |
9
|
|
|
use Fabrica\Http\Api\Controller; |
10
|
|
|
|
11
|
|
|
use Fabrica\Acl\Eloquent\Group; |
12
|
|
|
use Cartalyst\Sentinel\Users\EloquentUser; |
13
|
|
|
use Fabrica\ActiveDirectory\Eloquent\Directory; |
14
|
|
|
use Fabrica\ActiveDirectory\LDAP; |
15
|
|
|
|
16
|
|
|
use Fabrica\Events\DelGroupEvent; |
17
|
|
|
use Fabrica\Events\DelUserEvent; |
18
|
|
|
|
19
|
|
|
class DirectoryController extends Controller |
20
|
|
|
{ |
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->middleware('privilege:sys_admin'); |
24
|
|
|
parent::__construct(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Display a listing of the resource. |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Http\Response |
31
|
|
|
*/ |
32
|
|
|
public function index(Request $request) |
33
|
|
|
{ |
34
|
|
|
$directories = Directory::all()->toArray(); |
35
|
|
|
foreach($directories as $k => $d) |
36
|
|
|
{ |
37
|
|
|
if (isset($d['configs']) && $d['configs'] && isset($d['configs']['admin_password'])) |
38
|
|
|
{ |
39
|
|
|
unset($directories[$k]['configs']['admin_password']); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => $directories ]); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Store a newly created resource in storage. |
47
|
|
|
* |
48
|
|
|
* @param \Illuminate\Http\Request $request |
49
|
|
|
* @return \Illuminate\Http\Response |
50
|
|
|
*/ |
51
|
|
|
public function store(Request $request) |
52
|
|
|
{ |
53
|
|
|
if (!($name = $request->input('name'))) |
54
|
|
|
{ |
55
|
|
|
throw new \UnexpectedValueException('the name can not be empty.', -10300); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$configs = []; |
59
|
|
|
if (!($host = $request->input('host'))) |
60
|
|
|
{ |
61
|
|
|
throw new \UnexpectedValueException('the host can not be empty.', -10301); |
62
|
|
|
} |
63
|
|
|
$configs['host'] = $host; |
64
|
|
|
|
65
|
|
|
if (!($port = $request->input('port'))) |
66
|
|
|
{ |
67
|
|
|
throw new \UnexpectedValueException('the port can not be empty.', -10302); |
68
|
|
|
} |
69
|
|
|
$configs['port'] = intval($port); |
70
|
|
|
|
71
|
|
|
$configs['encryption'] = $request->input('encryption') ?: ''; |
72
|
|
|
|
73
|
|
|
if (!($admin_username = $request->input('admin_username'))) |
74
|
|
|
{ |
75
|
|
|
throw new \UnexpectedValueException('the username can not be empty.', -10303); |
76
|
|
|
} |
77
|
|
|
$configs['admin_username'] = $admin_username; |
78
|
|
|
|
79
|
|
|
if (!($admin_password = $request->input('admin_password'))) |
80
|
|
|
{ |
81
|
|
|
throw new \UnexpectedValueException('the user password can not be empty.', -10304); |
82
|
|
|
} |
83
|
|
|
$configs['admin_password'] = $admin_password; |
84
|
|
|
|
85
|
|
|
if (!($base_dn = $request->input('base_dn'))) |
86
|
|
|
{ |
87
|
|
|
throw new \UnexpectedValueException('the base_dn can not be empty.', -10305); |
88
|
|
|
} |
89
|
|
|
$configs['base_dn'] = $base_dn; |
90
|
|
|
|
91
|
|
|
$configs['additional_user_dn'] = $request->input('additional_user_dn') ?: ''; |
92
|
|
|
$configs['additional_group_dn'] = $request->input('additional_group_dn') ?: ''; |
93
|
|
|
|
94
|
|
|
if (!($user_object_class = $request->input('user_object_class'))) |
95
|
|
|
{ |
96
|
|
|
throw new \UnexpectedValueException('the user object class can not be empty.', -10306); |
97
|
|
|
} |
98
|
|
|
$configs['user_object_class'] = $user_object_class; |
99
|
|
|
|
100
|
|
|
if (!($user_object_filter = $request->input('user_object_filter'))) |
101
|
|
|
{ |
102
|
|
|
throw new \UnexpectedValueException('the user object filter can not be empty.', -10307); |
103
|
|
|
} |
104
|
|
|
$configs['user_object_filter'] = $user_object_filter; |
105
|
|
|
|
106
|
|
|
if (!($user_name_attr = $request->input('user_name_attr'))) |
107
|
|
|
{ |
108
|
|
|
throw new \UnexpectedValueException('the user name attributte can not be empty.', -10308); |
109
|
|
|
} |
110
|
|
|
$configs['user_name_attr'] = $user_name_attr; |
111
|
|
|
|
112
|
|
|
if (!($user_email_attr = $request->input('user_email_attr'))) |
113
|
|
|
{ |
114
|
|
|
throw new \UnexpectedValueException('the user email attributte can not be empty.', -10309); |
115
|
|
|
} |
116
|
|
|
$configs['user_email_attr'] = $user_email_attr; |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
if (!($group_object_class = $request->input('group_object_class'))) |
120
|
|
|
{ |
121
|
|
|
throw new \UnexpectedValueException('the group object class can not be empty.', -10310); |
122
|
|
|
} |
123
|
|
|
$configs['group_object_class'] = $group_object_class; |
124
|
|
|
|
125
|
|
|
if (!($group_object_filter = $request->input('group_object_filter'))) |
126
|
|
|
{ |
127
|
|
|
throw new \UnexpectedValueException('the group object filter can not be empty.', -10311); |
128
|
|
|
} |
129
|
|
|
$configs['group_object_filter'] = $group_object_filter; |
130
|
|
|
|
131
|
|
|
if (!($group_name_attr = $request->input('group_name_attr'))) |
132
|
|
|
{ |
133
|
|
|
throw new \UnexpectedValueException('the group name attributte can not be empty.', -10312); |
134
|
|
|
} |
135
|
|
|
$configs['group_name_attr'] = $group_name_attr; |
136
|
|
|
|
137
|
|
|
if (!($group_membership_attr = $request->input('group_membership_attr'))) |
138
|
|
|
{ |
139
|
|
|
throw new \UnexpectedValueException('the group membership attributte can not be empty.', -10313); |
140
|
|
|
} |
141
|
|
|
$configs['group_membership_attr'] = $group_membership_attr; |
142
|
|
|
|
143
|
|
|
$directory = Directory::create([ 'name' => $name, 'type' => 'OpenLDAP', 'invalid_flag' => 0, 'configs' => $configs ]); |
144
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => $directory ]); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Display the specified resource. |
149
|
|
|
* |
150
|
|
|
* @param int $id |
151
|
|
|
* @return \Illuminate\Http\Response |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$directory = Directory::find($id); |
156
|
|
|
if (!$directory) |
157
|
|
|
{ |
158
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10314); |
159
|
|
|
} |
160
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => $directory ]); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Update the specified resource in storage. |
165
|
|
|
* |
166
|
|
|
* @param \Illuminate\Http\Request $request |
167
|
|
|
* @param int $id |
168
|
|
|
* @return \Illuminate\Http\Response |
169
|
|
|
*/ |
170
|
|
|
public function update(Request $request, $id) |
171
|
|
|
{ |
172
|
|
|
$directory = Directory::find($id); |
173
|
|
|
if (!$directory) |
174
|
|
|
{ |
175
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10314); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$updValues = []; |
179
|
|
|
|
180
|
|
|
$name = $request->input('name'); |
181
|
|
View Code Duplication |
if (isset($name)) |
|
|
|
|
182
|
|
|
{ |
183
|
|
|
if (!$name) |
184
|
|
|
{ |
185
|
|
|
throw new \UnexpectedValueException('the name can not be empty.', -10300); |
186
|
|
|
} |
187
|
|
|
$updValues['name'] = $name; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$configs = []; |
191
|
|
|
|
192
|
|
|
$host = $request->input('host'); |
193
|
|
View Code Duplication |
if (isset($host)) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
if (!$host) |
196
|
|
|
{ |
197
|
|
|
throw new \UnexpectedValueException('the host can not be empty.', -10301); |
198
|
|
|
} |
199
|
|
|
$configs['host'] = $host; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$port = $request->input('port'); |
203
|
|
View Code Duplication |
if (isset($port)) |
|
|
|
|
204
|
|
|
{ |
205
|
|
|
if (!$port) |
206
|
|
|
{ |
207
|
|
|
throw new \UnexpectedValueException('the port can not be empty.', -10302); |
208
|
|
|
} |
209
|
|
|
$configs['port'] = intval($port); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$encryption = $request->input('encryption'); |
213
|
|
|
if (isset($encryption)) |
214
|
|
|
{ |
215
|
|
|
$configs['encryption'] = $encryption; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$admin_username = $request->input('admin_username'); |
219
|
|
View Code Duplication |
if (isset($admin_username)) |
|
|
|
|
220
|
|
|
{ |
221
|
|
|
if (!$admin_username) |
222
|
|
|
{ |
223
|
|
|
throw new \UnexpectedValueException('the username can not be empty.', -10303); |
224
|
|
|
} |
225
|
|
|
$configs['admin_username'] = $admin_username; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$admin_password = $request->input('admin_password'); |
229
|
|
View Code Duplication |
if (isset($admin_password)) |
|
|
|
|
230
|
|
|
{ |
231
|
|
|
if (!$admin_password) |
232
|
|
|
{ |
233
|
|
|
throw new \UnexpectedValueException('the user password can not be empty.', -10304); |
234
|
|
|
} |
235
|
|
|
$configs['admin_password'] = $admin_password; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$base_dn = $request->input('base_dn'); |
239
|
|
View Code Duplication |
if (isset($base_dn)) |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
if (!$base_dn) |
242
|
|
|
{ |
243
|
|
|
throw new \UnexpectedValueException('the base_dn can not be empty.', -10305); |
244
|
|
|
} |
245
|
|
|
$configs['base_dn'] = $base_dn; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
$additional_user_dn = $request->input('additional_user_dn'); |
249
|
|
|
if (isset($additional_user_dn)) |
250
|
|
|
{ |
251
|
|
|
$configs['additional_user_dn'] = $additional_user_dn; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$additional_group_dn = $request->input('additional_group_dn'); |
255
|
|
|
if (isset($additional_group_dn)) |
256
|
|
|
{ |
257
|
|
|
$configs['additional_group_dn'] = $additional_group_dn; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$user_object_class = $request->input('user_object_class'); |
261
|
|
View Code Duplication |
if (isset($user_object_class)) |
|
|
|
|
262
|
|
|
{ |
263
|
|
|
if (!$user_object_class) |
264
|
|
|
{ |
265
|
|
|
throw new \UnexpectedValueException('the user object class can not be empty.', -10306); |
266
|
|
|
} |
267
|
|
|
$configs['user_object_class'] = $user_object_class; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$user_object_filter = $request->input('user_object_filter'); |
271
|
|
View Code Duplication |
if (isset($user_object_filter)) |
|
|
|
|
272
|
|
|
{ |
273
|
|
|
if (!$user_object_filter) |
274
|
|
|
{ |
275
|
|
|
throw new \UnexpectedValueException('the user object filter can not be empty.', -10307); |
276
|
|
|
} |
277
|
|
|
$configs['user_object_filter'] = $user_object_filter; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$user_name_attr = $request->input('user_name_attr'); |
281
|
|
View Code Duplication |
if (isset($user_name_attr)) |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
if (!$user_name_attr) |
284
|
|
|
{ |
285
|
|
|
throw new \UnexpectedValueException('the user name attributte can not be empty.', -10308); |
286
|
|
|
} |
287
|
|
|
$configs['user_name_attr'] = $user_name_attr; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$user_email_attr = $request->input('user_email_attr'); |
291
|
|
View Code Duplication |
if (isset($user_email_attr)) |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
if (!$user_email_attr) |
294
|
|
|
{ |
295
|
|
|
throw new \UnexpectedValueException('the user email attributte can not be empty.', -10309); |
296
|
|
|
} |
297
|
|
|
$configs['user_email_attr'] = $user_email_attr; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$group_object_class = $request->input('group_object_class'); |
301
|
|
View Code Duplication |
if (isset($group_object_class)) |
|
|
|
|
302
|
|
|
{ |
303
|
|
|
if (!$group_object_class) |
304
|
|
|
{ |
305
|
|
|
throw new \UnexpectedValueException('the group object class can not be empty.', -10310); |
306
|
|
|
} |
307
|
|
|
$configs['group_object_class'] = $group_object_class; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$group_object_filter = $request->input('group_object_filter'); |
311
|
|
View Code Duplication |
if (isset($group_object_filter)) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
if (!$group_object_filter) |
314
|
|
|
{ |
315
|
|
|
throw new \UnexpectedValueException('the group object filter can not be empty.', -10311); |
316
|
|
|
} |
317
|
|
|
$configs['group_object_filter'] = $group_object_filter; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
$group_name_attr = $request->input('group_name_attr'); |
321
|
|
View Code Duplication |
if (isset($group_name_attr)) |
|
|
|
|
322
|
|
|
{ |
323
|
|
|
if (!$group_name_attr) |
324
|
|
|
{ |
325
|
|
|
throw new \UnexpectedValueException('the group name attributte can not be empty.', -10312); |
326
|
|
|
} |
327
|
|
|
$configs['group_name_attr'] = $group_name_attr; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$group_membership_attr = $request->input('group_membership_attr'); |
331
|
|
View Code Duplication |
if (isset($group_membership_attr)) |
|
|
|
|
332
|
|
|
{ |
333
|
|
|
if (!$group_membership_attr) |
334
|
|
|
{ |
335
|
|
|
throw new \UnexpectedValueException('the group membership attributte can not be empty.', -10313); |
336
|
|
|
} |
337
|
|
|
$configs['group_membership_attr'] = $group_membership_attr; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
if ($configs) |
|
|
|
|
341
|
|
|
{ |
342
|
|
|
$updValues['configs'] = isset($directory->configs) ? array_merge($directory->configs ?: [], $configs) : $configs; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$invalid_flag = $request->input('invalid_flag'); |
346
|
|
|
if (isset($invalid_flag)) |
347
|
|
|
{ |
348
|
|
|
$updValues['invalid_flag'] = intval($invalid_flag); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
$directory->fill($updValues)->save(); |
352
|
|
|
|
353
|
|
|
//if (isset($invalid_flag)) |
354
|
|
|
//{ |
355
|
|
|
// EloquentUser::where('directory', $id)->update([ 'invalid_flag' => intval($invalid_flag) ]); |
356
|
|
|
//} |
357
|
|
|
|
358
|
|
|
return $this->show($id); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Remove the specified resource from storage. |
363
|
|
|
* |
364
|
|
|
* @param int $id |
365
|
|
|
* @return \Illuminate\Http\Response |
366
|
|
|
*/ |
367
|
|
|
public function destroy($id) |
368
|
|
|
{ |
369
|
|
|
ini_set('memory_limit', '-1'); |
370
|
|
|
ini_set('max_execution_time', '0'); |
371
|
|
|
|
372
|
|
|
$directory = Directory::find($id); |
373
|
|
|
if (!$directory) |
374
|
|
|
{ |
375
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10314); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
// delete the related groups |
379
|
|
|
$groups = Group::where('directory', $id)->get(); |
380
|
|
|
foreach ($groups as $group) |
381
|
|
|
{ |
382
|
|
|
$group->delete(); |
383
|
|
|
Event::fire(new DelGroupEvent($group->id)); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$users = EloquentUser::where('directory', $id)->get(); |
387
|
|
|
foreach ($users as $user) |
388
|
|
|
{ |
389
|
|
|
$user->delete(); |
390
|
|
|
Event::fire(new DelUserEvent($user->id)); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
Directory::destroy($id); |
394
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => [ 'id' => $id ] ]); |
|
|
|
|
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* test the ldap. |
399
|
|
|
* |
400
|
|
|
* @param int $id |
401
|
|
|
* @return \Illuminate\Http\Response |
402
|
|
|
*/ |
403
|
|
|
public function test($id) |
404
|
|
|
{ |
405
|
|
|
ini_set('memory_limit', '-1'); |
406
|
|
|
ini_set('max_execution_time', '0'); |
407
|
|
|
|
408
|
|
|
$directory = Directory::find($id); |
409
|
|
|
if (!$directory) |
410
|
|
|
{ |
411
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10201); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
$configs = [ |
415
|
|
|
'default' => $directory->configs |
416
|
|
|
]; |
417
|
|
|
|
418
|
|
|
$ret = LDAP::test($configs); |
419
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => array_pop($ret) ]); |
|
|
|
|
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* sync the users and group. |
424
|
|
|
* |
425
|
|
|
* @param int $id |
426
|
|
|
* @return \Illuminate\Http\Response |
427
|
|
|
*/ |
428
|
|
|
public function sync($id) |
429
|
|
|
{ |
430
|
|
|
ini_set('memory_limit', '-1'); |
431
|
|
|
ini_set('max_execution_time', '0'); |
432
|
|
|
|
433
|
|
|
$directory = Directory::find($id); |
434
|
|
|
if (!$directory) |
435
|
|
|
{ |
436
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10314); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
$configs = [ |
440
|
|
|
$id => $directory->configs |
441
|
|
|
]; |
442
|
|
|
|
443
|
|
|
$ret = LDAP::sync($configs); |
444
|
|
|
$sync_info = array_pop($ret); |
445
|
|
|
if (!$sync_info['connect']) |
446
|
|
|
{ |
447
|
|
|
throw new \UnexpectedValueException('the connect server failed.', -10315); |
448
|
|
|
} |
449
|
|
|
else if (!$sync_info['user']) |
450
|
|
|
{ |
451
|
|
|
throw new \UnexpectedValueException('the user sync failed.', -10316); |
452
|
|
|
} |
453
|
|
|
else if (!$sync_info['group']) |
454
|
|
|
{ |
455
|
|
|
throw new \UnexpectedValueException('the group sync failed.', -10317); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
return Response()->json([ 'ecode' => 0, 'data' => [ 'user' => $sync_info['user'], 'group' => $sync_info['group'] ] ]); |
|
|
|
|
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: