1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @Author: etanasia |
5
|
|
|
* @Date: 2017-11-28 00:12:29 |
6
|
|
|
* @Last Modified by: etanasia |
7
|
|
|
* @Last Modified time: 2017-11-28 09:47:54 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace App\Http\Controllers; |
11
|
|
|
|
12
|
|
|
use App\Http\Controllers\Controller; |
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
use App\ApiKeys; |
15
|
|
|
use Jawaraegov\Workflows\Models\WorkflowModel; |
16
|
|
|
use Jawaraegov\Workflows\Models\WorkflowState; |
17
|
|
|
use Jawaraegov\Workflows\Models\WorkflowTransition; |
18
|
|
|
use Jawaraegov\Workflows\Models\History; |
19
|
|
|
use That0n3guy\Transliteration; |
20
|
|
|
|
21
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
22
|
|
|
use GuzzleHttp\Client; |
23
|
|
|
use GuzzleHttp\Pool; |
24
|
|
|
use GuzzleHttp\Psr7; |
25
|
|
|
|
26
|
|
|
use Validator, Image, Session, File, Response, Redirect, Exception; |
27
|
|
|
use Auth; |
28
|
|
|
|
29
|
|
|
class ApiManagerController extends Controller |
30
|
|
|
{ |
31
|
|
View Code Duplication |
public function index(Request $request) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
try { |
34
|
|
|
if($request->get('search') != ''){ |
35
|
|
|
$data['data'] = ApiKeys::with('getUserName')->with('getHistory') |
|
|
|
|
36
|
|
|
->where('client', 'like', '%'.$request->get('search').'%') |
37
|
|
|
->orderBy('id', 'desc') |
38
|
|
|
->paginate(env('PAGINATE', 10)); |
39
|
|
|
} else{ |
40
|
|
|
$data['data'] = ApiKeys::with('getUserName')->with('getHistory') |
|
|
|
|
41
|
|
|
->orderBy('id', 'desc')->paginate(env('PAGINATE', 10)); |
42
|
|
|
} |
43
|
|
|
} catch (Exception $e) { |
44
|
|
|
$data['data'] = []; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
return view('api_manager.index', $data); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function create() |
50
|
|
|
{ |
51
|
|
|
return view('api_manager.create'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function store(Request $request) |
55
|
|
|
{ |
56
|
|
|
Validator::extend('without_spaces', function($attr, $value){ |
57
|
|
|
return preg_match('/^\S*$/u', $value); |
58
|
|
|
}); |
59
|
|
|
$validator = Validator::make($request->all(), [ |
60
|
|
|
'client' => 'required|without_spaces|unique:api_keys,client', |
61
|
|
|
'description' => 'required', |
62
|
|
|
]); |
63
|
|
|
if($validator->fails()) |
64
|
|
|
{ |
65
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
66
|
|
|
return redirect()->back() |
67
|
|
|
->withErrors($validator) |
68
|
|
|
->withInput(); |
69
|
|
|
} |
70
|
|
View Code Duplication |
if(Auth::guest()){ $current_user = 1; } |
|
|
|
|
71
|
|
|
else{ $current_user = (Auth::user())?((Auth::user()->id)?Auth::user()->id:1):1; } |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
$token = $this->token(); |
75
|
|
|
$api = New ApiKeys; |
76
|
|
|
$api->client = str_replace(array('https://', 'http://'), array('',''),$request->client); |
77
|
|
|
$api->api_key = $token; |
78
|
|
|
$api->description = $request->description; |
79
|
|
|
$api->user_id = $current_user; |
80
|
|
|
|
81
|
|
|
//create history default |
82
|
|
|
$model = "ApiKeys"; |
83
|
|
|
$fromState = "propose"; |
84
|
|
|
$toState = "propose"; |
85
|
|
|
$workflow = $this->getWorkflow($model); |
86
|
|
|
$statesFrom = $this->getState($fromState); |
87
|
|
|
$statesTo = $this->getState($toState); |
88
|
|
View Code Duplication |
if($workflow->count() == 0){ |
|
|
|
|
89
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
90
|
|
|
return Redirect::to('api-manager'); |
91
|
|
|
}elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
92
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
93
|
|
|
return Redirect::to('api-manager'); |
94
|
|
|
}else{ |
95
|
|
|
$api->save(); |
96
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
97
|
|
|
|
98
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly'); |
99
|
|
|
return Redirect::to('api-manager'); |
100
|
|
|
} |
101
|
|
|
} catch (Exception $e) { |
102
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
103
|
|
|
return Redirect::to('api-manager'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function show($id) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
$data['transition'] = WorkflowTransition::all(); |
|
|
|
|
111
|
|
|
$data['history'] = History::where('content_id', $id)->paginate(10); |
112
|
|
|
$data['histories'] = History::where('content_id', $id)->orderBy('id', 'desc')->first(); |
113
|
|
|
$data['id'] = $id; |
114
|
|
|
$data['data'] = ApiKeys::with('getHistory')->where('id', $id)->orderBy('id', 'desc')->first(); |
115
|
|
|
return view('api_manager.show', $data); |
116
|
|
|
} catch (Exception $e) { |
117
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
118
|
|
|
return Redirect::to('api-manager'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function edit(Request $request, $id) |
123
|
|
|
{ |
124
|
|
|
$data['data'] = ApiKeys::findOrFail($id); |
|
|
|
|
125
|
|
|
return view('api_manager.edit', $data); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function update(Request $request, $id) |
129
|
|
|
{ |
130
|
|
|
Validator::extend('without_spaces', function($attr, $value){ |
131
|
|
|
return preg_match('/^\S*$/u', $value); |
132
|
|
|
}); |
133
|
|
|
$validator = Validator::make($request->all(), [ |
134
|
|
|
'client' => 'required|without_spaces|unique:api_keys,client,'.$id, |
135
|
|
|
'description' => 'required', |
136
|
|
|
]); |
137
|
|
|
if($validator->fails()) |
138
|
|
|
{ |
139
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
140
|
|
|
return redirect()->back() |
141
|
|
|
->withErrors($validator) |
142
|
|
|
->withInput(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
try { |
146
|
|
|
$api = ApiKeys::findOrFail($id); |
147
|
|
|
$api->client = str_replace(array('https://', 'http://'),array('',''),$request->client); |
148
|
|
|
$api->description = $request->description; |
149
|
|
|
$api->save(); |
150
|
|
|
Session::flash('message', 'Api Keys Data Update Successfuly'); |
151
|
|
|
return Redirect::to('api-manager'); |
152
|
|
|
} catch (Exception $e) { |
153
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
154
|
|
|
return Redirect::to('api-manager'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function token() |
159
|
|
|
{ |
160
|
|
|
$length = 50; |
161
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
162
|
|
|
$charactersLength = strlen($characters); |
163
|
|
|
$randomString = ''; |
164
|
|
|
for ($i = 0; $i < $length; $i++) { |
165
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
166
|
|
|
} |
167
|
|
|
return $randomString; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function destroy($id) |
171
|
|
|
{ |
172
|
|
|
try { |
173
|
|
|
ApiKeys::destroy($id); |
174
|
|
|
Session::flash('message', 'Api Keys Data Deleted Successfuly'); |
175
|
|
|
return Redirect::to('api-manager'); |
176
|
|
|
} catch (Exception $e) { |
177
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
178
|
|
|
return Redirect::to('api-manager'); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
private function getWorkflow($model){ |
183
|
|
|
$data = WorkflowModel::where('content_type', 'like', '%' . $model . '%'); |
184
|
|
|
return $data; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
private function getState($state){ |
188
|
|
|
$name = \Transliteration::clean_filename(strtolower($state)); |
189
|
|
|
$data = WorkflowState::where('status', 1)->where('name', 'like', '%' . $name . '%'); |
190
|
|
|
return $data; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
private function getHistory($content_id){ |
194
|
|
|
$data = History::with('getApiKeys') |
195
|
|
|
->with('getWorkflow') |
196
|
|
|
->with('getStateFrom') |
197
|
|
|
->with('getStateTo') |
198
|
|
|
->with('getUserName') |
199
|
|
|
->where('content_id', $content_id); |
200
|
|
|
return $data; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
private function saveHistory($api, $workflow, $statesFrom, $statesTo, $user_id = ""){ |
204
|
|
|
if(Auth::guest()){ $current_user = 1; } |
205
|
|
|
else{ |
206
|
|
|
if($user_id == ""){ $current_user = (Auth::user())?((Auth::user()->id)?Auth::user()->id:1):1; } |
207
|
|
|
else { $current_user = $user_id; } |
208
|
|
|
} |
209
|
|
|
$history = New History; |
210
|
|
|
$history->content_id = $api->id; |
211
|
|
|
$history->Workflow_id = $workflow->id; |
212
|
|
|
$history->from_state = $statesFrom->id; |
213
|
|
|
$history->to_state = $statesTo->id; |
214
|
|
|
$history->user_id = $current_user; |
215
|
|
|
$history->save(); |
216
|
|
|
return $history; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function request(Request $request) |
220
|
|
|
{ |
221
|
|
|
Validator::extend('without_spaces', function($attr, $value){ |
222
|
|
|
return preg_match('/^\S*$/u', $value); |
223
|
|
|
}); |
224
|
|
|
$validator = Validator::make($request->all(), [ |
225
|
|
|
'client' => 'required|without_spaces', |
226
|
|
|
'request' => 'required', |
227
|
|
|
'deskripsi' => 'required', |
228
|
|
|
'user_id' => 'required', |
229
|
|
|
]); |
230
|
|
|
|
231
|
|
View Code Duplication |
if($validator->fails()) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
return Response::json(array( |
234
|
|
|
'title' => 'Error', |
235
|
|
|
'type' => 'error', |
236
|
|
|
'message' => $validator->errors()->all() |
237
|
|
|
)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
try { |
241
|
|
|
$host = str_replace(array('https://', 'http://'), array('',''),$request->input('host')); |
242
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$request->input('client')); |
243
|
|
|
$requests = ucwords($request->input('request')); |
244
|
|
|
$deskripsi = $request->input('deskripsi'); |
245
|
|
|
$user_id = $request->input('user_id'); |
246
|
|
|
$data = ApiKeys::where('client', 'like', '%' . $client . '%'); |
247
|
|
|
if($data->count() == 0){ |
248
|
|
|
$token = $this->token(); |
249
|
|
|
$api = New ApiKeys; |
250
|
|
|
$api->client = $client; |
251
|
|
|
$api->api_key = $token; |
252
|
|
|
$api->description = $deskripsi; |
253
|
|
|
$api->user_id = $user_id; |
254
|
|
|
|
255
|
|
|
//create history default |
256
|
|
|
$model = "ApiKeys"; |
257
|
|
|
$fromState = "propose"; |
258
|
|
|
$toState = "propose"; |
259
|
|
|
$workflow = $this->getWorkflow($model); |
260
|
|
|
$statesFrom = $this->getState($fromState); |
261
|
|
|
$statesTo = $this->getState($toState); |
262
|
|
|
if($workflow->count() == 0){ |
263
|
|
|
$error = true; |
264
|
|
|
$statusCode = 404; |
265
|
|
|
$title = 'Error'; |
266
|
|
|
$type = 'error'; |
267
|
|
|
$message = 'Error Workflow not found'; |
268
|
|
|
$result = 'Not Found'; |
269
|
|
|
} |
270
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
271
|
|
|
$error = true; |
272
|
|
|
$statusCode = 404; |
273
|
|
|
$title = 'Error'; |
274
|
|
|
$type = 'error'; |
275
|
|
|
$message = 'Error State not active or State not found'; |
276
|
|
|
$result = 'Not Found'; |
277
|
|
|
} |
278
|
|
|
else{ |
279
|
|
|
$api->save(); |
280
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
281
|
|
View Code Duplication |
if(env('URL_APIMANAGER') != NULL){ |
|
|
|
|
282
|
|
|
$url_apimanager = str_replace('"', '',env('URL_APIMANAGER')); |
283
|
|
|
if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){ |
284
|
|
|
$transition = "Propose to Propose"; |
285
|
|
|
$this->send_apimanager($url_apimanager,$client,$host,$transition,$api->api_key); |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
View Code Duplication |
if($requests == 'Request'){ |
|
|
|
|
289
|
|
|
$model = "ApiKeys"; |
290
|
|
|
$fromState = "propose"; |
291
|
|
|
$toState = $requests; |
292
|
|
|
$workflow = $this->getWorkflow($model); |
293
|
|
|
$statesFrom = $this->getState($fromState); |
294
|
|
|
$statesTo = $this->getState($toState); |
295
|
|
|
if($workflow->count() == 0){ |
296
|
|
|
$error = true; |
297
|
|
|
$statusCode = 404; |
298
|
|
|
$title = 'Error'; |
299
|
|
|
$type = 'error'; |
300
|
|
|
$message = 'Error Workflow not found'; |
301
|
|
|
$result = 'Not Found'; |
302
|
|
|
} |
303
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
304
|
|
|
$error = true; |
305
|
|
|
$statusCode = 404; |
306
|
|
|
$title = 'Error'; |
307
|
|
|
$type = 'error'; |
308
|
|
|
$message = 'Error State not active or State not found'; |
309
|
|
|
$result = 'Not Found'; |
310
|
|
|
} |
311
|
|
|
else{ |
312
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
313
|
|
|
$error = false; |
314
|
|
|
$statusCode = 200; |
315
|
|
|
$title = 'Success'; |
316
|
|
|
$type = 'success'; |
317
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
318
|
|
|
$result = $request->all(); |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
else{ |
322
|
|
|
$error = true; |
323
|
|
|
$statusCode = 404; |
324
|
|
|
$title = 'Error'; |
325
|
|
|
$type = 'error'; |
326
|
|
|
$message = 'Value Request must be Request.'; |
327
|
|
|
$result = $request->all(); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
else { |
332
|
|
|
$get = $data->first(); |
333
|
|
|
$history = $this->getHistory($get->id)->get(); |
334
|
|
|
foreach ($history as $value) { |
335
|
|
|
$workstateto = $value->getStateTo->label; |
336
|
|
|
} |
337
|
|
|
if($workstateto == $requests){ |
338
|
|
|
$error = true; |
339
|
|
|
$statusCode = 404; |
340
|
|
|
$title = 'Error'; |
341
|
|
|
$type = 'error'; |
342
|
|
|
$message = 'Data has already been taken.'; |
343
|
|
|
$result = $request->all(); |
344
|
|
|
} |
345
|
|
|
elseif($workstateto == 'Document Submitted'){ |
|
|
|
|
346
|
|
|
$error = true; |
347
|
|
|
$statusCode = 404; |
348
|
|
|
$title = 'Error'; |
349
|
|
|
$type = 'error'; |
350
|
|
|
$message = 'Data has already been taken.'; |
351
|
|
|
$result = $request->all(); |
352
|
|
|
} |
353
|
|
|
else { |
354
|
|
|
if($requests == 'Request'){ |
355
|
|
|
$model = "ApiKeys"; |
356
|
|
|
$fromState = "propose"; |
357
|
|
|
$toState = $requests; |
358
|
|
|
$workflow = $this->getWorkflow($model); |
359
|
|
|
$statesFrom = $this->getState($fromState); |
360
|
|
|
$statesTo = $this->getState($toState); |
361
|
|
|
if($workflow->count() == 0){ |
362
|
|
|
$error = true; |
363
|
|
|
$statusCode = 404; |
364
|
|
|
$title = 'Error'; |
365
|
|
|
$type = 'error'; |
366
|
|
|
$message = 'Error Workflow not found'; |
367
|
|
|
$result = 'Not Found'; |
368
|
|
|
} |
369
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
370
|
|
|
$error = true; |
371
|
|
|
$statusCode = 404; |
372
|
|
|
$title = 'Error'; |
373
|
|
|
$type = 'error'; |
374
|
|
|
$message = 'Error State not active or State not found'; |
375
|
|
|
$result = 'Not Found'; |
376
|
|
|
} |
377
|
|
|
else{ |
378
|
|
|
if($workstateto != 'Propose'){ |
379
|
|
|
$error = true; |
380
|
|
|
$statusCode = 404; |
381
|
|
|
$title = 'Error'; |
382
|
|
|
$type = 'error'; |
383
|
|
|
$message = 'Data has already been taken.'; |
384
|
|
|
$result = $request->all(); |
385
|
|
|
}else { |
386
|
|
|
$this->saveHistory($get, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
387
|
|
|
$error = false; |
388
|
|
|
$statusCode = 200; |
389
|
|
|
$title = 'Success'; |
390
|
|
|
$type = 'success'; |
391
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
392
|
|
|
$result = $request->all(); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
View Code Duplication |
elseif($requests == 'Document Submitted'){ |
|
|
|
|
397
|
|
|
$model = "ApiKeys"; |
398
|
|
|
$fromState = $workstateto; |
399
|
|
|
$toState = $requests; |
400
|
|
|
$workflow = $this->getWorkflow($model); |
401
|
|
|
$statesFrom = $this->getState($fromState); |
402
|
|
|
$statesTo = $this->getState($toState); |
403
|
|
|
if($workflow->count() == 0){ |
404
|
|
|
$error = true; |
405
|
|
|
$statusCode = 404; |
406
|
|
|
$title = 'Error'; |
407
|
|
|
$type = 'error'; |
408
|
|
|
$message = 'Error Workflow not found'; |
409
|
|
|
$result = 'Not Found'; |
410
|
|
|
} |
411
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
412
|
|
|
$error = true; |
413
|
|
|
$statusCode = 404; |
414
|
|
|
$title = 'Error'; |
415
|
|
|
$type = 'error'; |
416
|
|
|
$message = 'Error State not active or State not found'; |
417
|
|
|
$result = 'Not Found'; |
418
|
|
|
} |
419
|
|
|
else{ |
420
|
|
|
if($workstateto != 'Needs Completed Document'){ |
421
|
|
|
$error = true; |
422
|
|
|
$statusCode = 404; |
423
|
|
|
$title = 'Error'; |
424
|
|
|
$type = 'error'; |
425
|
|
|
$message = 'Data has already been taken.'; |
426
|
|
|
$result = $request->all(); |
427
|
|
|
}else { |
428
|
|
|
$this->saveHistory($get, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
429
|
|
|
$error = false; |
430
|
|
|
$statusCode = 200; |
431
|
|
|
$title = 'Success'; |
432
|
|
|
$type = 'success'; |
433
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
434
|
|
|
$result = $request->all(); |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
else { |
439
|
|
|
$error = true; |
440
|
|
|
$statusCode = 404; |
441
|
|
|
$title = 'Error'; |
442
|
|
|
$type = 'error'; |
443
|
|
|
$message = 'Value Request must be Request.'; |
444
|
|
|
$result = $request->all(); |
445
|
|
|
} |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
} catch (Exception $e) { |
449
|
|
|
$error = true; |
450
|
|
|
$statusCode = 404; |
451
|
|
|
$title = 'Error'; |
452
|
|
|
$type = 'error'; |
453
|
|
|
$message = 'Error'; |
454
|
|
|
$result = 'Not Found'; |
455
|
|
|
} finally { |
456
|
|
|
return Response::json(array( |
457
|
|
|
'error' => $error, |
|
|
|
|
458
|
|
|
'status' => $statusCode, |
|
|
|
|
459
|
|
|
'title' => $title, |
|
|
|
|
460
|
|
|
'type' => $type, |
|
|
|
|
461
|
|
|
'message' => $message, |
|
|
|
|
462
|
|
|
'result' => $result |
|
|
|
|
463
|
|
|
)); |
464
|
|
|
} |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
public function transition(Request $request) |
468
|
|
|
{ |
469
|
|
|
Validator::extend('without_spaces', function($attr, $value){ |
470
|
|
|
return preg_match('/^\S*$/u', $value); |
471
|
|
|
}); |
472
|
|
|
$validator = Validator::make($request->all(), [ |
473
|
|
|
'client' => 'required|without_spaces', |
474
|
|
|
'request' => 'required', |
475
|
|
|
]); |
476
|
|
|
|
477
|
|
View Code Duplication |
if($validator->fails()) |
|
|
|
|
478
|
|
|
{ |
479
|
|
|
return Response::json(array( |
480
|
|
|
'title' => 'Error', |
481
|
|
|
'type' => 'error', |
482
|
|
|
'message' => $validator->errors()->all() |
483
|
|
|
)); |
484
|
|
|
} |
485
|
|
View Code Duplication |
if(Auth::guest()){ $current_user = 1; } |
|
|
|
|
486
|
|
|
else{ $current_user = (Auth::user())?((Auth::user()->id)?Auth::user()->id:1):1; } |
487
|
|
|
|
488
|
|
|
try { |
489
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$request->input('client')); |
490
|
|
|
$host = str_replace(array('https://', 'http://'), array('',''),$request->input('host')); |
491
|
|
|
$requests = ucwords($request->input('request')); |
492
|
|
|
$data = ApiKeys::where('client', 'like', '%' . $client . '%'); |
493
|
|
|
if($data->count() == 0){ |
494
|
|
|
$token = $this->token(); |
495
|
|
|
$api = New ApiKeys; |
496
|
|
|
$api->client = $client; |
497
|
|
|
$api->api_key = $token; |
498
|
|
|
$api->description = $requests; |
499
|
|
|
$api->user_id = $current_user; |
500
|
|
|
|
501
|
|
|
//create history default |
502
|
|
|
$model = "ApiKeys"; |
503
|
|
|
$fromState = "propose"; |
504
|
|
|
$toState = "propose"; |
505
|
|
|
$workflow = $this->getWorkflow($model); |
506
|
|
|
$statesFrom = $this->getState($fromState); |
507
|
|
|
$statesTo = $this->getState($toState); |
508
|
|
|
if($workflow->count() == 0){ |
509
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
510
|
|
|
return Redirect::to('api-manager'); |
511
|
|
|
} |
512
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
513
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
514
|
|
|
return Redirect::to('api-manager'); |
515
|
|
|
} |
516
|
|
|
else{ |
517
|
|
|
$api->save(); |
518
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
519
|
|
View Code Duplication |
if(env('URL_APIMANAGER') != NULL){ |
|
|
|
|
520
|
|
|
$url_apimanager = str_replace('"', '',env('URL_APIMANAGER')); |
521
|
|
|
if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){ |
522
|
|
|
$transition = "Propose to Propose"; |
523
|
|
|
$this->send_apimanager($url_apimanager,$client,$host,$transition,$api->api_key); |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
if($requests == 'Request'){ |
527
|
|
|
$model = "ApiKeys"; |
528
|
|
|
$fromState = "propose"; |
529
|
|
|
$toState = $requests; |
530
|
|
|
$workflow = $this->getWorkflow($model); |
531
|
|
|
$statesFrom = $this->getState($fromState); |
532
|
|
|
$statesTo = $this->getState($toState); |
533
|
|
View Code Duplication |
if($workflow->count() == 0){ |
|
|
|
|
534
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
535
|
|
|
return Redirect::to('api-manager'); |
536
|
|
|
} |
537
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
538
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
539
|
|
|
return Redirect::to('api-manager'); |
540
|
|
|
} |
541
|
|
|
else{ |
542
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
543
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly. Your request has already been send.'); |
544
|
|
|
return Redirect::to('api-manager'); |
545
|
|
|
} |
546
|
|
|
} |
547
|
|
|
else{ |
548
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
549
|
|
|
return Redirect::to('api-manager'); |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
else { |
554
|
|
|
$get = $data->first(); |
555
|
|
|
$history = $this->getHistory($get->id)->get(); |
556
|
|
|
foreach ($history as $value) { |
557
|
|
|
$workstateto = $value->getStateTo->label; |
558
|
|
|
} |
559
|
|
|
if($workstateto == $requests){ |
560
|
|
|
// kirim ke client |
561
|
|
|
$error = true; |
562
|
|
|
$statusCode = 404; |
563
|
|
|
$title = 'Error'; |
564
|
|
|
$type = 'error'; |
565
|
|
|
$message = 'Data has already been taken.'; |
566
|
|
|
$result = $request->all(); |
567
|
|
|
Session::flash('message', 'Error 404 #error Data has already been taken.'); |
568
|
|
|
} |
569
|
|
|
else { |
570
|
|
|
$model = "ApiKeys"; |
571
|
|
|
$fromState = $workstateto; |
|
|
|
|
572
|
|
|
$toState = $requests; |
573
|
|
|
$workflow = $this->getWorkflow($model); |
574
|
|
|
$statesFrom = $this->getState($fromState); |
575
|
|
|
$statesTo = $this->getState($toState); |
576
|
|
|
if($workflow->count() == 0){ |
577
|
|
|
// kirim ke client |
578
|
|
|
$error = true; |
579
|
|
|
$statusCode = 404; |
580
|
|
|
$title = 'Error'; |
581
|
|
|
$type = 'error'; |
582
|
|
|
$message = 'workflow not found.'; |
583
|
|
|
$result = $request->all(); |
584
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
585
|
|
|
} |
586
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
587
|
|
|
// kirim ke client |
588
|
|
|
$error = true; |
589
|
|
|
$statusCode = 404; |
590
|
|
|
$title = 'Error'; |
591
|
|
|
$type = 'error'; |
592
|
|
|
$message = 'state not active or state not found.'; |
593
|
|
|
$result = $request->all(); |
594
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
595
|
|
|
} |
596
|
|
|
else{ |
597
|
|
|
$this->saveHistory($get, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
598
|
|
|
// kirim ke client |
599
|
|
|
$error = false; |
600
|
|
|
$statusCode = 200; |
601
|
|
|
$title = 'Success'; |
602
|
|
|
$type = 'success'; |
603
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
604
|
|
|
$result = $get; |
605
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly. Your request has already been send.'); |
606
|
|
|
} |
607
|
|
|
} |
608
|
|
|
$history = $this->getHistory($get->id)->get(); |
609
|
|
|
foreach ($history as $value) { |
610
|
|
|
$workstatefromid = $value->getStateFrom->id; |
|
|
|
|
611
|
|
|
$workstatetoid = $value->getStateTo->id; |
|
|
|
|
612
|
|
|
$workstatefrom = $value->getStateFrom->label; |
613
|
|
|
$workstateto = $value->getStateTo->label; |
614
|
|
|
} |
615
|
|
|
$state = $workstateto; |
616
|
|
|
$transition = $workstatefrom.' To '.$workstateto; |
|
|
|
|
617
|
|
|
if($requests != 'Approved'){ |
618
|
|
|
$result = $request->all(); |
619
|
|
|
} |
620
|
|
|
$this->SendClient($client, $host, $error, $statusCode, $title, $type, $message, $result, $state, $transition); |
621
|
|
View Code Duplication |
if(env('URL_APIMANAGER') != NULL){ |
|
|
|
|
622
|
|
|
$url_apimanager = str_replace('"', '',env('URL_APIMANAGER')); |
623
|
|
|
if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){ |
624
|
|
|
$this->send_apimanager($url_apimanager,$client,$host,$transition,$get->api_key); |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
return Redirect::to('api-manager'); |
628
|
|
|
} |
629
|
|
|
} catch (Exception $e) { |
630
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
631
|
|
|
return Redirect::to('api-manager'); |
632
|
|
|
} |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
private function SendClient($client, $host, $error, $statusCode, $title, $type, $message, $result, $state, $transition){ |
636
|
|
View Code Duplication |
if(Auth::guest()){ $current_user = 1; } |
|
|
|
|
637
|
|
|
else{ $current_user = (Auth::user())?((Auth::user()->id)?Auth::user()->id:1):1; } |
638
|
|
|
if($state == 'Approved'){ |
639
|
|
|
$apikey = $result->api_key; |
640
|
|
|
}else{ |
641
|
|
|
$apikey = NULL; |
642
|
|
|
} |
643
|
|
|
$headers = ['Content-Type' => 'application/json']; |
644
|
|
|
$data = [ |
645
|
|
|
'error' => $error, |
646
|
|
|
'status' => $statusCode, |
647
|
|
|
'title' => $title, |
648
|
|
|
'type' => $type, |
649
|
|
|
'message' => $message, |
650
|
|
|
'result' => $result, |
651
|
|
|
'hostname' => $host, |
652
|
|
|
'keys' => $apikey, |
653
|
|
|
'state' => $state, |
654
|
|
|
'transition' => $transition, |
655
|
|
|
'user_id' => $current_user |
656
|
|
|
]; |
657
|
|
|
$body = json_encode($data); |
658
|
|
|
$host = explode(':',$host); |
659
|
|
|
$host = $host[0]; |
660
|
|
|
|
661
|
|
|
try { |
662
|
|
|
$urlget = "https://".$client."/api/v1/host-keys/".$host."/get"; |
663
|
|
|
$clients = new \GuzzleHttp\Client(); |
664
|
|
|
$resget = $clients->request('GET', $urlget,['headers'=>$headers]); |
665
|
|
|
$responseget = $resget->getBody(); |
666
|
|
|
$responsesget = json_decode($responseget); |
667
|
|
|
$msg = "success"; |
668
|
|
|
} catch (GuzzleException $e) { |
669
|
|
|
$msg = "error"; |
670
|
|
|
$responsesget = $e; |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
if($msg == "success"){ |
674
|
|
|
$responsesget = $responsesget; |
|
|
|
|
675
|
|
|
$resultget = $responsesget->result; |
676
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$message; |
677
|
|
|
}else { |
678
|
|
|
try { |
679
|
|
|
$urlget = "http://".$client."/api/v1/host-keys/".$host."/get"; |
680
|
|
|
$clients = new \GuzzleHttp\Client(); |
681
|
|
|
$resget = $clients->request('GET', $urlget,['headers'=>$headers]); |
682
|
|
|
$responseget = $resget->getBody(); |
683
|
|
|
$responsesget = json_decode($responseget); |
684
|
|
|
$msgz = "success"; |
685
|
|
|
} catch (GuzzleException $er) { |
686
|
|
|
$msgz = "error"; |
687
|
|
|
$responsesget = $er; |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
if($msgz == "success"){ |
691
|
|
|
$responsesget = $responsesget; |
|
|
|
|
692
|
|
|
$resultget = $responsesget->result; |
693
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$message; |
694
|
|
|
}else { |
695
|
|
|
$responsesget = $responsesget->getMessage(); |
696
|
|
|
$resultget = 'Not Found'; |
697
|
|
|
$message = $msgz.' - '.$responsesget; |
698
|
|
|
} |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
if($resultget != 'Not Found'){ |
702
|
|
|
try { |
703
|
|
|
$clients = new \GuzzleHttp\Client(); |
704
|
|
|
$url = "https://".$client."/api/v1/host-keys/".$responsesget->id; |
705
|
|
|
$res = $clients->request('PUT', $url,['headers'=>$headers,'body'=>$body]); |
706
|
|
|
$response = $res->getBody(); |
707
|
|
|
$responses = json_decode($response); |
708
|
|
|
$msg = "success"; |
709
|
|
|
} catch (GuzzleException $e) { |
710
|
|
|
$msg = "error"; |
711
|
|
|
$responses = $e; |
712
|
|
|
} |
713
|
|
|
|
714
|
|
View Code Duplication |
if($msg == "success"){ |
|
|
|
|
715
|
|
|
$responses = $responses; |
|
|
|
|
716
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$message; |
|
|
|
|
717
|
|
|
}else { |
718
|
|
|
try { |
719
|
|
|
$clients = new \GuzzleHttp\Client(); |
720
|
|
|
$url = "http://".$client."/api/v1/host-keys/".$responsesget->id; |
721
|
|
|
$res = $clients->request('PUT', $url,['headers'=>$headers,'body'=>$body]); |
722
|
|
|
$response = $res->getBody(); |
723
|
|
|
$responses = json_decode($response); |
724
|
|
|
$msgz = "success"; |
725
|
|
|
} catch (GuzzleException $er) { |
726
|
|
|
$msgz = "error"; |
727
|
|
|
$responses = $er; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
if($msgz == "success"){ |
731
|
|
|
$responses = $responses; |
|
|
|
|
732
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$message; |
|
|
|
|
733
|
|
|
}else { |
734
|
|
|
$responses = $responses->getMessage(); |
735
|
|
|
$message = $msgz.' - '.$responses; |
|
|
|
|
736
|
|
|
} |
737
|
|
|
} |
738
|
|
|
}else { |
739
|
|
|
try { |
740
|
|
|
$clients = new \GuzzleHttp\Client(); |
741
|
|
|
$url = "https://".$client."/api/v1/host-keys"; |
742
|
|
|
$res = $clients->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
743
|
|
|
$response = $res->getBody(); |
744
|
|
|
$responses = json_decode($response); |
745
|
|
|
$msg = "success"; |
746
|
|
|
} catch (GuzzleException $e) { |
747
|
|
|
$msg = "error"; |
748
|
|
|
$responses = $e; |
749
|
|
|
} |
750
|
|
|
|
751
|
|
View Code Duplication |
if($msg == "success"){ |
|
|
|
|
752
|
|
|
$responses = $responses; |
|
|
|
|
753
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$message; |
|
|
|
|
754
|
|
|
}else { |
755
|
|
|
try { |
756
|
|
|
$clients = new \GuzzleHttp\Client(); |
757
|
|
|
$url = "http://".$client."/api/v1/host-keys"; |
758
|
|
|
$res = $clients->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
759
|
|
|
$response = $res->getBody(); |
760
|
|
|
$responses = json_decode($response); |
761
|
|
|
$msgz = "success"; |
762
|
|
|
} catch (GuzzleException $er) { |
763
|
|
|
$msgz = "error"; |
764
|
|
|
$responses = $er; |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
if($msgz == "success"){ |
768
|
|
|
$responses = $responses; |
|
|
|
|
769
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$message; |
|
|
|
|
770
|
|
|
}else { |
771
|
|
|
$responses = $responses->getMessage(); |
772
|
|
|
$message = $msgz.' - '.$responses; |
|
|
|
|
773
|
|
|
} |
774
|
|
|
} |
775
|
|
|
} |
776
|
|
|
return $responses; |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
private function send_apimanager($url_apimanager,$client,$host,$keterangan,$apikey=""){ |
780
|
|
View Code Duplication |
if(Auth::guest()){ $current_user = 1; } |
|
|
|
|
781
|
|
|
else{ $current_user = (Auth::user())?((Auth::user()->id)?Auth::user()->id:1):1; } |
782
|
|
|
$headers = ['Content-Type' => 'application/json']; |
783
|
|
|
$host = str_replace(array('https://', 'http://'), array('',''),$host); |
784
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$client); |
785
|
|
|
$data = [ |
786
|
|
|
'host' => $host, |
787
|
|
|
'client' => $client, |
788
|
|
|
'keterangan' => $keterangan, |
789
|
|
|
'user_id' => $current_user, |
790
|
|
|
'api_key' => $apikey |
791
|
|
|
]; |
792
|
|
|
$body = json_encode($data); |
793
|
|
|
$url_apimanager = str_replace(array('https://', 'http://'), array('',''),$url_apimanager); |
794
|
|
|
|
795
|
|
|
try { |
796
|
|
|
$url = "https://".$url_apimanager."/api/store"; |
797
|
|
|
$client = new \GuzzleHttp\Client(); |
798
|
|
|
$res = $client->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
799
|
|
|
$response = $res->getBody(); |
800
|
|
|
$responses = json_decode($response); |
801
|
|
|
$msg = "success"; |
802
|
|
|
} catch (GuzzleException $e) { |
803
|
|
|
$msg = "error"; |
804
|
|
|
$responses = $e; |
805
|
|
|
} |
806
|
|
|
|
807
|
|
View Code Duplication |
if($msg == "success"){ |
|
|
|
|
808
|
|
|
$responses = $responses; |
|
|
|
|
809
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$keterangan; |
|
|
|
|
810
|
|
|
}else { |
811
|
|
|
try { |
812
|
|
|
$urlz = "http://".$url_apimanager."/api/store"; |
813
|
|
|
$clientz = new \GuzzleHttp\Client(); |
814
|
|
|
$resz = $clientz->request('POST', $urlz,['headers'=>$headers,'body'=>$body]); |
815
|
|
|
$responsez = $resz->getBody(); |
816
|
|
|
$responsesz = json_decode($responsez); |
817
|
|
|
$msgz = "success"; |
818
|
|
|
} catch (GuzzleException $er) { |
819
|
|
|
$msgz = "error"; |
820
|
|
|
$responsesz = $er; |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
if($msgz == "success"){ |
824
|
|
|
$responses = $responsesz; |
825
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$keterangan; |
|
|
|
|
826
|
|
|
}else { |
827
|
|
|
$responses = $responsesz->getMessage(); |
828
|
|
|
$message = $msgz.' - '.$responsesz->getMessage(); |
|
|
|
|
829
|
|
|
} |
830
|
|
|
} |
831
|
|
|
return $responses; |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
public function receive(Request $request){ |
835
|
|
|
return Response::json(array( |
836
|
|
|
'error' => $request->error, |
837
|
|
|
'status' => $request->status, |
838
|
|
|
'title' => $request->title, |
839
|
|
|
'type' => $request->type, |
840
|
|
|
'message' => $request->message, |
841
|
|
|
'result' => $request->result |
842
|
|
|
)); |
843
|
|
|
} |
844
|
|
|
} |
845
|
|
|
|
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.