|
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
|
|
|
unset($directories[$k]['configs']['admin_password']); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
return response()->json([ 'ecode' => 0, 'data' => $directories ]); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Store a newly created resource in storage. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \Illuminate\Http\Request $request |
|
48
|
|
|
* @return \Illuminate\Http\Response |
|
49
|
|
|
*/ |
|
50
|
|
|
public function store(Request $request) |
|
51
|
|
|
{ |
|
52
|
|
|
if (!($name = $request->input('name'))) { |
|
53
|
|
|
throw new \UnexpectedValueException('the name can not be empty.', -10300); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$configs = []; |
|
57
|
|
|
if (!($host = $request->input('host'))) { |
|
58
|
|
|
throw new \UnexpectedValueException('the host can not be empty.', -10301); |
|
59
|
|
|
} |
|
60
|
|
|
$configs['host'] = $host; |
|
61
|
|
|
|
|
62
|
|
|
if (!($port = $request->input('port'))) { |
|
63
|
|
|
throw new \UnexpectedValueException('the port can not be empty.', -10302); |
|
64
|
|
|
} |
|
65
|
|
|
$configs['port'] = intval($port); |
|
66
|
|
|
|
|
67
|
|
|
$configs['encryption'] = $request->input('encryption') ?: ''; |
|
68
|
|
|
|
|
69
|
|
|
if (!($admin_username = $request->input('admin_username'))) { |
|
70
|
|
|
throw new \UnexpectedValueException('the username can not be empty.', -10303); |
|
71
|
|
|
} |
|
72
|
|
|
$configs['admin_username'] = $admin_username; |
|
73
|
|
|
|
|
74
|
|
|
if (!($admin_password = $request->input('admin_password'))) { |
|
75
|
|
|
throw new \UnexpectedValueException('the user password can not be empty.', -10304); |
|
76
|
|
|
} |
|
77
|
|
|
$configs['admin_password'] = $admin_password; |
|
78
|
|
|
|
|
79
|
|
|
if (!($base_dn = $request->input('base_dn'))) { |
|
80
|
|
|
throw new \UnexpectedValueException('the base_dn can not be empty.', -10305); |
|
81
|
|
|
} |
|
82
|
|
|
$configs['base_dn'] = $base_dn; |
|
83
|
|
|
|
|
84
|
|
|
$configs['additional_user_dn'] = $request->input('additional_user_dn') ?: ''; |
|
85
|
|
|
$configs['additional_group_dn'] = $request->input('additional_group_dn') ?: ''; |
|
86
|
|
|
|
|
87
|
|
|
if (!($user_object_class = $request->input('user_object_class'))) { |
|
88
|
|
|
throw new \UnexpectedValueException('the user object class can not be empty.', -10306); |
|
89
|
|
|
} |
|
90
|
|
|
$configs['user_object_class'] = $user_object_class; |
|
91
|
|
|
|
|
92
|
|
|
if (!($user_object_filter = $request->input('user_object_filter'))) { |
|
93
|
|
|
throw new \UnexpectedValueException('the user object filter can not be empty.', -10307); |
|
94
|
|
|
} |
|
95
|
|
|
$configs['user_object_filter'] = $user_object_filter; |
|
96
|
|
|
|
|
97
|
|
|
if (!($user_name_attr = $request->input('user_name_attr'))) { |
|
98
|
|
|
throw new \UnexpectedValueException('the user name attributte can not be empty.', -10308); |
|
99
|
|
|
} |
|
100
|
|
|
$configs['user_name_attr'] = $user_name_attr; |
|
101
|
|
|
|
|
102
|
|
|
if (!($user_email_attr = $request->input('user_email_attr'))) { |
|
103
|
|
|
throw new \UnexpectedValueException('the user email attributte can not be empty.', -10309); |
|
104
|
|
|
} |
|
105
|
|
|
$configs['user_email_attr'] = $user_email_attr; |
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
if (!($group_object_class = $request->input('group_object_class'))) { |
|
109
|
|
|
throw new \UnexpectedValueException('the group object class can not be empty.', -10310); |
|
110
|
|
|
} |
|
111
|
|
|
$configs['group_object_class'] = $group_object_class; |
|
112
|
|
|
|
|
113
|
|
|
if (!($group_object_filter = $request->input('group_object_filter'))) { |
|
114
|
|
|
throw new \UnexpectedValueException('the group object filter can not be empty.', -10311); |
|
115
|
|
|
} |
|
116
|
|
|
$configs['group_object_filter'] = $group_object_filter; |
|
117
|
|
|
|
|
118
|
|
|
if (!($group_name_attr = $request->input('group_name_attr'))) { |
|
119
|
|
|
throw new \UnexpectedValueException('the group name attributte can not be empty.', -10312); |
|
120
|
|
|
} |
|
121
|
|
|
$configs['group_name_attr'] = $group_name_attr; |
|
122
|
|
|
|
|
123
|
|
|
if (!($group_membership_attr = $request->input('group_membership_attr'))) { |
|
124
|
|
|
throw new \UnexpectedValueException('the group membership attributte can not be empty.', -10313); |
|
125
|
|
|
} |
|
126
|
|
|
$configs['group_membership_attr'] = $group_membership_attr; |
|
127
|
|
|
|
|
128
|
|
|
$directory = Directory::create([ 'name' => $name, 'type' => 'OpenLDAP', 'invalid_flag' => 0, 'configs' => $configs ]); |
|
129
|
|
|
return response()->json([ 'ecode' => 0, 'data' => $directory ]); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Display the specified resource. |
|
134
|
|
|
* |
|
135
|
|
|
* @param int $id |
|
136
|
|
|
* @return \Illuminate\Http\Response |
|
137
|
|
|
*/ |
|
138
|
|
View Code Duplication |
public function show(Request $request, $id) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$directory = Directory::find($id); |
|
141
|
|
|
if (!$directory) { |
|
142
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10314); |
|
143
|
|
|
} |
|
144
|
|
|
return response()->json([ 'ecode' => 0, 'data' => $directory ]); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Update the specified resource in storage. |
|
149
|
|
|
* |
|
150
|
|
|
* @param \Illuminate\Http\Request $request |
|
151
|
|
|
* @param int $id |
|
152
|
|
|
* @return \Illuminate\Http\Response |
|
153
|
|
|
*/ |
|
154
|
|
|
public function update(Request $request, $id) |
|
155
|
|
|
{ |
|
156
|
|
|
$directory = Directory::find($id); |
|
157
|
|
|
if (!$directory) { |
|
158
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10314); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$updValues = []; |
|
162
|
|
|
|
|
163
|
|
|
$name = $request->input('name'); |
|
164
|
|
View Code Duplication |
if (isset($name)) { |
|
|
|
|
|
|
165
|
|
|
if (!$name) { |
|
166
|
|
|
throw new \UnexpectedValueException('the name can not be empty.', -10300); |
|
167
|
|
|
} |
|
168
|
|
|
$updValues['name'] = $name; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$configs = []; |
|
172
|
|
|
|
|
173
|
|
|
$host = $request->input('host'); |
|
174
|
|
View Code Duplication |
if (isset($host)) { |
|
|
|
|
|
|
175
|
|
|
if (!$host) { |
|
176
|
|
|
throw new \UnexpectedValueException('the host can not be empty.', -10301); |
|
177
|
|
|
} |
|
178
|
|
|
$configs['host'] = $host; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$port = $request->input('port'); |
|
182
|
|
View Code Duplication |
if (isset($port)) { |
|
|
|
|
|
|
183
|
|
|
if (!$port) { |
|
184
|
|
|
throw new \UnexpectedValueException('the port can not be empty.', -10302); |
|
185
|
|
|
} |
|
186
|
|
|
$configs['port'] = intval($port); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$encryption = $request->input('encryption'); |
|
190
|
|
|
if (isset($encryption)) { |
|
191
|
|
|
$configs['encryption'] = $encryption; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$admin_username = $request->input('admin_username'); |
|
195
|
|
View Code Duplication |
if (isset($admin_username)) { |
|
|
|
|
|
|
196
|
|
|
if (!$admin_username) { |
|
197
|
|
|
throw new \UnexpectedValueException('the username can not be empty.', -10303); |
|
198
|
|
|
} |
|
199
|
|
|
$configs['admin_username'] = $admin_username; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
$admin_password = $request->input('admin_password'); |
|
203
|
|
View Code Duplication |
if (isset($admin_password)) { |
|
|
|
|
|
|
204
|
|
|
if (!$admin_password) { |
|
205
|
|
|
throw new \UnexpectedValueException('the user password can not be empty.', -10304); |
|
206
|
|
|
} |
|
207
|
|
|
$configs['admin_password'] = $admin_password; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$base_dn = $request->input('base_dn'); |
|
211
|
|
View Code Duplication |
if (isset($base_dn)) { |
|
|
|
|
|
|
212
|
|
|
if (!$base_dn) { |
|
213
|
|
|
throw new \UnexpectedValueException('the base_dn can not be empty.', -10305); |
|
214
|
|
|
} |
|
215
|
|
|
$configs['base_dn'] = $base_dn; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$additional_user_dn = $request->input('additional_user_dn'); |
|
219
|
|
|
if (isset($additional_user_dn)) { |
|
220
|
|
|
$configs['additional_user_dn'] = $additional_user_dn; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$additional_group_dn = $request->input('additional_group_dn'); |
|
224
|
|
|
if (isset($additional_group_dn)) { |
|
225
|
|
|
$configs['additional_group_dn'] = $additional_group_dn; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
$user_object_class = $request->input('user_object_class'); |
|
229
|
|
View Code Duplication |
if (isset($user_object_class)) { |
|
|
|
|
|
|
230
|
|
|
if (!$user_object_class) { |
|
231
|
|
|
throw new \UnexpectedValueException('the user object class can not be empty.', -10306); |
|
232
|
|
|
} |
|
233
|
|
|
$configs['user_object_class'] = $user_object_class; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$user_object_filter = $request->input('user_object_filter'); |
|
237
|
|
View Code Duplication |
if (isset($user_object_filter)) { |
|
|
|
|
|
|
238
|
|
|
if (!$user_object_filter) { |
|
239
|
|
|
throw new \UnexpectedValueException('the user object filter can not be empty.', -10307); |
|
240
|
|
|
} |
|
241
|
|
|
$configs['user_object_filter'] = $user_object_filter; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$user_name_attr = $request->input('user_name_attr'); |
|
245
|
|
View Code Duplication |
if (isset($user_name_attr)) { |
|
|
|
|
|
|
246
|
|
|
if (!$user_name_attr) { |
|
247
|
|
|
throw new \UnexpectedValueException('the user name attributte can not be empty.', -10308); |
|
248
|
|
|
} |
|
249
|
|
|
$configs['user_name_attr'] = $user_name_attr; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$user_email_attr = $request->input('user_email_attr'); |
|
253
|
|
View Code Duplication |
if (isset($user_email_attr)) { |
|
|
|
|
|
|
254
|
|
|
if (!$user_email_attr) { |
|
255
|
|
|
throw new \UnexpectedValueException('the user email attributte can not be empty.', -10309); |
|
256
|
|
|
} |
|
257
|
|
|
$configs['user_email_attr'] = $user_email_attr; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$group_object_class = $request->input('group_object_class'); |
|
261
|
|
View Code Duplication |
if (isset($group_object_class)) { |
|
|
|
|
|
|
262
|
|
|
if (!$group_object_class) { |
|
263
|
|
|
throw new \UnexpectedValueException('the group object class can not be empty.', -10310); |
|
264
|
|
|
} |
|
265
|
|
|
$configs['group_object_class'] = $group_object_class; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
$group_object_filter = $request->input('group_object_filter'); |
|
269
|
|
View Code Duplication |
if (isset($group_object_filter)) { |
|
|
|
|
|
|
270
|
|
|
if (!$group_object_filter) { |
|
271
|
|
|
throw new \UnexpectedValueException('the group object filter can not be empty.', -10311); |
|
272
|
|
|
} |
|
273
|
|
|
$configs['group_object_filter'] = $group_object_filter; |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$group_name_attr = $request->input('group_name_attr'); |
|
277
|
|
View Code Duplication |
if (isset($group_name_attr)) { |
|
|
|
|
|
|
278
|
|
|
if (!$group_name_attr) { |
|
279
|
|
|
throw new \UnexpectedValueException('the group name attributte can not be empty.', -10312); |
|
280
|
|
|
} |
|
281
|
|
|
$configs['group_name_attr'] = $group_name_attr; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$group_membership_attr = $request->input('group_membership_attr'); |
|
285
|
|
View Code Duplication |
if (isset($group_membership_attr)) { |
|
|
|
|
|
|
286
|
|
|
if (!$group_membership_attr) { |
|
287
|
|
|
throw new \UnexpectedValueException('the group membership attributte can not be empty.', -10313); |
|
288
|
|
|
} |
|
289
|
|
|
$configs['group_membership_attr'] = $group_membership_attr; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
if ($configs) { |
|
|
|
|
|
|
293
|
|
|
$updValues['configs'] = isset($directory->configs) ? array_merge($directory->configs ?: [], $configs) : $configs; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$invalid_flag = $request->input('invalid_flag'); |
|
297
|
|
|
if (isset($invalid_flag)) { |
|
298
|
|
|
$updValues['invalid_flag'] = intval($invalid_flag); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
$directory->fill($updValues)->save(); |
|
302
|
|
|
|
|
303
|
|
|
//if (isset($invalid_flag)) |
|
304
|
|
|
//{ |
|
305
|
|
|
// EloquentUser::where('directory', $id)->update([ 'invalid_flag' => intval($invalid_flag) ]); |
|
306
|
|
|
//} |
|
307
|
|
|
|
|
308
|
|
|
return $this->show($id); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Remove the specified resource from storage. |
|
313
|
|
|
* |
|
314
|
|
|
* @param int $id |
|
315
|
|
|
* @return \Illuminate\Http\Response |
|
316
|
|
|
*/ |
|
317
|
|
|
public function destroy($id) |
|
318
|
|
|
{ |
|
319
|
|
|
ini_set('memory_limit', '-1'); |
|
320
|
|
|
ini_set('max_execution_time', '0'); |
|
321
|
|
|
|
|
322
|
|
|
$directory = Directory::find($id); |
|
323
|
|
|
if (!$directory) { |
|
324
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10314); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
// delete the related groups |
|
328
|
|
|
$groups = Group::where('directory', $id)->get(); |
|
329
|
|
|
foreach ($groups as $group) |
|
330
|
|
|
{ |
|
331
|
|
|
$group->delete(); |
|
332
|
|
|
Event::fire(new DelGroupEvent($group->id)); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
$users = EloquentUser::where('directory', $id)->get(); |
|
336
|
|
|
foreach ($users as $user) |
|
337
|
|
|
{ |
|
338
|
|
|
$user->delete(); |
|
339
|
|
|
Event::fire(new DelUserEvent($user->id)); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
Directory::destroy($id); |
|
343
|
|
|
return response()->json([ 'ecode' => 0, 'data' => [ 'id' => $id ] ]); |
|
|
|
|
|
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
/** |
|
347
|
|
|
* test the ldap. |
|
348
|
|
|
* |
|
349
|
|
|
* @param int $id |
|
350
|
|
|
* @return \Illuminate\Http\Response |
|
351
|
|
|
*/ |
|
352
|
|
|
public function test($id) |
|
353
|
|
|
{ |
|
354
|
|
|
ini_set('memory_limit', '-1'); |
|
355
|
|
|
ini_set('max_execution_time', '0'); |
|
356
|
|
|
|
|
357
|
|
|
$directory = Directory::find($id); |
|
358
|
|
|
if (!$directory) { |
|
359
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10201); |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
$configs = [ |
|
363
|
|
|
'default' => $directory->configs |
|
364
|
|
|
]; |
|
365
|
|
|
|
|
366
|
|
|
$ret = LDAP::test($configs); |
|
367
|
|
|
return response()->json([ 'ecode' => 0, 'data' => array_pop($ret) ]); |
|
|
|
|
|
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* sync the users and group. |
|
372
|
|
|
* |
|
373
|
|
|
* @param int $id |
|
374
|
|
|
* @return \Illuminate\Http\Response |
|
375
|
|
|
*/ |
|
376
|
|
|
public function sync($id) |
|
377
|
|
|
{ |
|
378
|
|
|
ini_set('memory_limit', '-1'); |
|
379
|
|
|
ini_set('max_execution_time', '0'); |
|
380
|
|
|
|
|
381
|
|
|
$directory = Directory::find($id); |
|
382
|
|
|
if (!$directory) { |
|
383
|
|
|
throw new \UnexpectedValueException('the directory does not exist.', -10314); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
$configs = [ |
|
387
|
|
|
$id => $directory->configs |
|
388
|
|
|
]; |
|
389
|
|
|
|
|
390
|
|
|
$ret = LDAP::sync($configs); |
|
391
|
|
|
$sync_info = array_pop($ret); |
|
392
|
|
|
if (!$sync_info['connect']) { |
|
393
|
|
|
throw new \UnexpectedValueException('the connect server failed.', -10315); |
|
394
|
|
|
} |
|
395
|
|
|
else if (!$sync_info['user']) { |
|
396
|
|
|
throw new \UnexpectedValueException('the user sync failed.', -10316); |
|
397
|
|
|
} |
|
398
|
|
|
else if (!$sync_info['group']) { |
|
399
|
|
|
throw new \UnexpectedValueException('the group sync failed.', -10317); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
|
|
return response()->json([ 'ecode' => 0, 'data' => [ 'user' => $sync_info['user'], 'group' => $sync_info['group'] ] ]); |
|
|
|
|
|
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
|
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: