|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @Author: bantenprov |
|
5
|
|
|
* @Date: 2017-11-28 00:12:29 |
|
6
|
|
|
* @Last Modified by: bantenprov |
|
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 Bantenprov\Workflow\Models\WorkflowModel; |
|
16
|
|
|
use Bantenprov\Workflow\Models\WorkflowState; |
|
17
|
|
|
use Bantenprov\Workflow\Models\WorkflowTransition; |
|
18
|
|
|
use Bantenprov\Workflow\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
|
|
|
public function index(Request $request) |
|
32
|
|
|
{ |
|
33
|
|
|
try { |
|
34
|
|
|
if($request->get('search') != ''){ |
|
35
|
|
|
$data['data'] = ApiKeys::with('getUserName')->where('client', 'like', '%'.$request->get('search').'%') |
|
|
|
|
|
|
36
|
|
|
->orderBy('id', 'desc') |
|
37
|
|
|
->paginate(env('PAGINATE', 10)); |
|
38
|
|
|
} else{ |
|
39
|
|
|
$data['data'] = ApiKeys::with('getUserName')->orderBy('id', 'desc')->paginate(env('PAGINATE', 10)); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
} catch (Exception $e) { |
|
42
|
|
|
$data['data'] = []; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
return view('api_manager.index', $data); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function create() |
|
48
|
|
|
{ |
|
49
|
|
|
return view('api_manager.create'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function store(Request $request) |
|
53
|
|
|
{ |
|
54
|
|
|
$validator = Validator::make($request->all(), [ |
|
55
|
|
|
'client' => 'required|unique:api_keys,client', |
|
56
|
|
|
'description' => 'required', |
|
57
|
|
|
]); |
|
58
|
|
|
if($validator->fails()) |
|
59
|
|
|
{ |
|
60
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
|
61
|
|
|
return redirect()->back() |
|
62
|
|
|
->withErrors($validator) |
|
63
|
|
|
->withInput(); |
|
64
|
|
|
} |
|
65
|
|
|
if(Auth::guest()){ $current_user = 1; } |
|
66
|
|
|
else{ $current_user = Auth::user()->id; } |
|
67
|
|
|
|
|
68
|
|
|
try { |
|
69
|
|
|
$token = $this->token(); |
|
70
|
|
|
$api = New ApiKeys; |
|
71
|
|
|
$api->client = str_replace(array('https://', 'http://'), array('',''),$request->client); |
|
72
|
|
|
$api->api_key = $token; |
|
73
|
|
|
$api->description = $request->description; |
|
74
|
|
|
$api->user_id = $current_user; |
|
75
|
|
|
|
|
76
|
|
|
//create history default |
|
77
|
|
|
$model = "ApiKeys"; |
|
78
|
|
|
$fromState = "propose"; |
|
79
|
|
|
$toState = "propose"; |
|
80
|
|
|
$workflow = $this->getWorkflow($model); |
|
81
|
|
|
$statesFrom = $this->getState($fromState); |
|
82
|
|
|
$statesTo = $this->getState($toState); |
|
83
|
|
View Code Duplication |
if($workflow->count() == 0){ |
|
|
|
|
|
|
84
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
|
85
|
|
|
return Redirect::to('api-manager'); |
|
86
|
|
|
}elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
|
87
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
|
88
|
|
|
return Redirect::to('api-manager'); |
|
89
|
|
|
}else{ |
|
90
|
|
|
$api->save(); |
|
91
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
|
92
|
|
|
|
|
93
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly'); |
|
94
|
|
|
return Redirect::to('api-manager'); |
|
95
|
|
|
} |
|
96
|
|
|
} catch (Exception $e) { |
|
97
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
|
98
|
|
|
return Redirect::to('api-manager'); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function show($id) |
|
103
|
|
|
{ |
|
104
|
|
|
try { |
|
105
|
|
|
$transition = WorkflowTransition::all(); |
|
106
|
|
|
$data['transition'] = $transition; |
|
|
|
|
|
|
107
|
|
|
$history = History::with('getApiKeys') |
|
108
|
|
|
->with('getWorkflow') |
|
109
|
|
|
->with('getStateFrom') |
|
110
|
|
|
->with('getStateTo') |
|
111
|
|
|
->with('getUserName') |
|
112
|
|
|
->where('content_id', $id) |
|
113
|
|
|
->get(); |
|
114
|
|
|
|
|
115
|
|
|
$data['history'] = $history; |
|
116
|
|
|
$data['id'] = $id; |
|
117
|
|
|
foreach ($history as $value) { |
|
118
|
|
|
$workstateto = $value->getStateTo->label; |
|
119
|
|
|
} |
|
120
|
|
|
$data['workflowstateto'] = $workstateto; |
|
|
|
|
|
|
121
|
|
|
$data['data'] = ApiKeys::where('id', $id)->first(); |
|
122
|
|
|
return view('api_manager.show', $data); |
|
123
|
|
|
} catch (Exception $e) { |
|
124
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
|
125
|
|
|
return Redirect::to('api-manager'); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function edit(Request $request, $id) |
|
130
|
|
|
{ |
|
131
|
|
|
$data['data'] = ApiKeys::findOrFail($id); |
|
|
|
|
|
|
132
|
|
|
return view('api_manager.edit', $data); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function update(Request $request, $id) |
|
136
|
|
|
{ |
|
137
|
|
|
$validator = Validator::make($request->all(), [ |
|
138
|
|
|
'client' => 'required|unique:api_keys,client,'.$id, |
|
139
|
|
|
'description' => 'required', |
|
140
|
|
|
]); |
|
141
|
|
|
if($validator->fails()) |
|
142
|
|
|
{ |
|
143
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
|
144
|
|
|
return redirect()->back() |
|
145
|
|
|
->withErrors($validator) |
|
146
|
|
|
->withInput(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
try { |
|
150
|
|
|
$api = ApiKeys::findOrFail($id); |
|
151
|
|
|
$api->client = str_replace(array('https://', 'http://'),array('',''),$request->client); |
|
152
|
|
|
$api->description = $request->description; |
|
153
|
|
|
$api->save(); |
|
154
|
|
|
Session::flash('message', 'Api Keys Data Update Successfuly'); |
|
155
|
|
|
return Redirect::to('api-manager'); |
|
156
|
|
|
} catch (Exception $e) { |
|
157
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
|
158
|
|
|
return Redirect::to('api-manager'); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function token() |
|
163
|
|
|
{ |
|
164
|
|
|
$length = 70; |
|
165
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
166
|
|
|
$charactersLength = strlen($characters); |
|
167
|
|
|
$randomString = ''; |
|
168
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
169
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
|
170
|
|
|
} |
|
171
|
|
|
return $randomString; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function destroy($id) |
|
175
|
|
|
{ |
|
176
|
|
|
try { |
|
177
|
|
|
ApiKeys::destroy($id); |
|
178
|
|
|
Session::flash('message', 'Api Keys Data Deleted Successfuly'); |
|
179
|
|
|
return Redirect::to('api-manager'); |
|
180
|
|
|
} catch (Exception $e) { |
|
181
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
|
182
|
|
|
return Redirect::to('api-manager'); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private function getWorkflow($model){ |
|
187
|
|
|
$data = WorkflowModel::where('content_type', 'like', '%' . $model . '%'); |
|
188
|
|
|
return $data; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
private function getState($state){ |
|
192
|
|
|
$name = \Transliteration::clean_filename(strtolower($state)); |
|
193
|
|
|
$data = WorkflowState::where('status', 1)->where('name', 'like', '%' . $name . '%'); |
|
194
|
|
|
return $data; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
private function getHistory($content_id){ |
|
198
|
|
|
$data = History::with('getApiKeys') |
|
199
|
|
|
->with('getWorkflow') |
|
200
|
|
|
->with('getStateFrom') |
|
201
|
|
|
->with('getStateTo') |
|
202
|
|
|
->with('getUserName') |
|
203
|
|
|
->where('content_id', $content_id); |
|
204
|
|
|
return $data; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
private function saveHistory($api, $workflow, $statesFrom, $statesTo, $user_id = ""){ |
|
208
|
|
|
if(Auth::guest()){ $current_user = 1; } |
|
209
|
|
|
else{ |
|
210
|
|
|
if($user_id == ""){ $current_user = Auth::user()->id; } |
|
211
|
|
|
else { $current_user = $user_id; } |
|
212
|
|
|
} |
|
213
|
|
|
$history = New History; |
|
214
|
|
|
$history->content_id = $api->id; |
|
215
|
|
|
$history->Workflow_id = $workflow->id; |
|
216
|
|
|
$history->from_state = $statesFrom->id; |
|
217
|
|
|
$history->to_state = $statesTo->id; |
|
218
|
|
|
$history->user_id = $current_user; |
|
219
|
|
|
$history->save(); |
|
220
|
|
|
return $history; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function request(Request $request) |
|
224
|
|
|
{ |
|
225
|
|
|
$validator = Validator::make($request->all(), [ |
|
226
|
|
|
'client' => 'required', |
|
227
|
|
|
'request' => 'required', |
|
228
|
|
|
'deskripsi' => 'required', |
|
229
|
|
|
'user_id' => 'required', |
|
230
|
|
|
]); |
|
231
|
|
|
|
|
232
|
|
View Code Duplication |
if($validator->fails()) |
|
|
|
|
|
|
233
|
|
|
{ |
|
234
|
|
|
return Response::json(array( |
|
235
|
|
|
'title' => 'Error', |
|
236
|
|
|
'type' => 'error', |
|
237
|
|
|
'message' => $validator->errors()->all() |
|
238
|
|
|
)); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
try { |
|
242
|
|
|
$host = str_replace(array('https://', 'http://'), array('',''),$request->input('host')); |
|
243
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$request->input('client')); |
|
244
|
|
|
$requests = ucwords($request->input('request')); |
|
245
|
|
|
$deskripsi = $request->input('deskripsi'); |
|
246
|
|
|
$user_id = $request->input('user_id'); |
|
247
|
|
|
$data = ApiKeys::where('client', 'like', '%' . $client . '%'); |
|
248
|
|
|
if($data->count() == 0){ |
|
249
|
|
|
$token = $this->token(); |
|
250
|
|
|
$api = New ApiKeys; |
|
251
|
|
|
$api->client = $client; |
|
252
|
|
|
$api->api_key = $token; |
|
253
|
|
|
$api->description = $deskripsi; |
|
254
|
|
|
$api->user_id = $user_id; |
|
255
|
|
|
|
|
256
|
|
|
//create history default |
|
257
|
|
|
$model = "ApiKeys"; |
|
258
|
|
|
$fromState = "propose"; |
|
259
|
|
|
$toState = "propose"; |
|
260
|
|
|
$workflow = $this->getWorkflow($model); |
|
261
|
|
|
$statesFrom = $this->getState($fromState); |
|
262
|
|
|
$statesTo = $this->getState($toState); |
|
263
|
|
|
if($workflow->count() == 0){ |
|
264
|
|
|
$error = true; |
|
265
|
|
|
$statusCode = 404; |
|
266
|
|
|
$title = 'Error'; |
|
267
|
|
|
$type = 'error'; |
|
268
|
|
|
$message = 'Error Workflow not found'; |
|
269
|
|
|
$result = 'Not Found'; |
|
270
|
|
|
} |
|
271
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
|
272
|
|
|
$error = true; |
|
273
|
|
|
$statusCode = 404; |
|
274
|
|
|
$title = 'Error'; |
|
275
|
|
|
$type = 'error'; |
|
276
|
|
|
$message = 'Error State not active or State not found'; |
|
277
|
|
|
$result = 'Not Found'; |
|
278
|
|
|
} |
|
279
|
|
|
else{ |
|
280
|
|
|
$api->save(); |
|
281
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
|
282
|
|
View Code Duplication |
if(env('URL_APIMANAGER') != NULL){ |
|
|
|
|
|
|
283
|
|
|
$url_apimanager = str_replace('"', '',env('URL_APIMANAGER')); |
|
284
|
|
|
if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){ |
|
285
|
|
|
$transition = "Propose to Propose"; |
|
286
|
|
|
$this->send_apimanager($url_apimanager,$client,$host,$transition); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
View Code Duplication |
if($requests == 'Request'){ |
|
|
|
|
|
|
290
|
|
|
$model = "ApiKeys"; |
|
291
|
|
|
$fromState = "propose"; |
|
292
|
|
|
$toState = $requests; |
|
293
|
|
|
$workflow = $this->getWorkflow($model); |
|
294
|
|
|
$statesFrom = $this->getState($fromState); |
|
295
|
|
|
$statesTo = $this->getState($toState); |
|
296
|
|
|
if($workflow->count() == 0){ |
|
297
|
|
|
$error = true; |
|
298
|
|
|
$statusCode = 404; |
|
299
|
|
|
$title = 'Error'; |
|
300
|
|
|
$type = 'error'; |
|
301
|
|
|
$message = 'Error Workflow not found'; |
|
302
|
|
|
$result = 'Not Found'; |
|
303
|
|
|
} |
|
304
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
|
305
|
|
|
$error = true; |
|
306
|
|
|
$statusCode = 404; |
|
307
|
|
|
$title = 'Error'; |
|
308
|
|
|
$type = 'error'; |
|
309
|
|
|
$message = 'Error State not active or State not found'; |
|
310
|
|
|
$result = 'Not Found'; |
|
311
|
|
|
} |
|
312
|
|
|
else{ |
|
313
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
|
314
|
|
|
$error = false; |
|
315
|
|
|
$statusCode = 200; |
|
316
|
|
|
$title = 'Success'; |
|
317
|
|
|
$type = 'success'; |
|
318
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
|
319
|
|
|
$result = $request->all(); |
|
320
|
|
|
} |
|
321
|
|
|
} |
|
322
|
|
|
else{ |
|
323
|
|
|
$error = true; |
|
324
|
|
|
$statusCode = 404; |
|
325
|
|
|
$title = 'Error'; |
|
326
|
|
|
$type = 'error'; |
|
327
|
|
|
$message = 'Value Request must be Request.'; |
|
328
|
|
|
$result = $request->all(); |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
else { |
|
333
|
|
|
$get = $data->first(); |
|
334
|
|
|
$history = $this->getHistory($get->id)->get(); |
|
335
|
|
|
foreach ($history as $value) { |
|
336
|
|
|
$workstateto = $value->getStateTo->label; |
|
337
|
|
|
} |
|
338
|
|
|
if($workstateto == $requests){ |
|
339
|
|
|
$error = true; |
|
340
|
|
|
$statusCode = 404; |
|
341
|
|
|
$title = 'Error'; |
|
342
|
|
|
$type = 'error'; |
|
343
|
|
|
$message = 'Data has already been taken.'; |
|
344
|
|
|
$result = $request->all(); |
|
345
|
|
|
} |
|
346
|
|
View Code Duplication |
elseif($workstateto == 'Approved'){ |
|
|
|
|
|
|
347
|
|
|
$error = true; |
|
348
|
|
|
$statusCode = 404; |
|
349
|
|
|
$title = 'Error'; |
|
350
|
|
|
$type = 'error'; |
|
351
|
|
|
$message = 'Data has already been Approved.'; |
|
352
|
|
|
$result = $request->all(); |
|
353
|
|
|
} |
|
354
|
|
View Code Duplication |
elseif($workstateto == 'Rejected'){ |
|
|
|
|
|
|
355
|
|
|
$error = true; |
|
356
|
|
|
$statusCode = 404; |
|
357
|
|
|
$title = 'Error'; |
|
358
|
|
|
$type = 'error'; |
|
359
|
|
|
$message = 'Data has already been Rejected.'; |
|
360
|
|
|
$result = $request->all(); |
|
361
|
|
|
} |
|
362
|
|
View Code Duplication |
else { |
|
|
|
|
|
|
363
|
|
|
if($requests == 'Request'){ |
|
364
|
|
|
$model = "ApiKeys"; |
|
365
|
|
|
$fromState = "propose"; |
|
366
|
|
|
$toState = $requests; |
|
367
|
|
|
$workflow = $this->getWorkflow($model); |
|
368
|
|
|
$statesFrom = $this->getState($fromState); |
|
369
|
|
|
$statesTo = $this->getState($toState); |
|
370
|
|
|
if($workflow->count() == 0){ |
|
371
|
|
|
$error = true; |
|
372
|
|
|
$statusCode = 404; |
|
373
|
|
|
$title = 'Error'; |
|
374
|
|
|
$type = 'error'; |
|
375
|
|
|
$message = 'Error Workflow not found'; |
|
376
|
|
|
$result = 'Not Found'; |
|
377
|
|
|
} |
|
378
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
|
379
|
|
|
$error = true; |
|
380
|
|
|
$statusCode = 404; |
|
381
|
|
|
$title = 'Error'; |
|
382
|
|
|
$type = 'error'; |
|
383
|
|
|
$message = 'Error State not active or State not found'; |
|
384
|
|
|
$result = 'Not Found'; |
|
385
|
|
|
} |
|
386
|
|
|
else{ |
|
387
|
|
|
$this->saveHistory($get, $workflow->first(), $statesFrom->first(), $statesTo->first(), $user_id); |
|
388
|
|
|
$error = false; |
|
389
|
|
|
$statusCode = 200; |
|
390
|
|
|
$title = 'Success'; |
|
391
|
|
|
$type = 'success'; |
|
392
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
|
393
|
|
|
$result = $request->all(); |
|
394
|
|
|
} |
|
395
|
|
|
} |
|
396
|
|
|
else { |
|
397
|
|
|
$error = true; |
|
398
|
|
|
$statusCode = 404; |
|
399
|
|
|
$title = 'Error'; |
|
400
|
|
|
$type = 'error'; |
|
401
|
|
|
$message = 'Value Request must be Request.'; |
|
402
|
|
|
$result = $request->all(); |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
} catch (Exception $e) { |
|
407
|
|
|
$error = true; |
|
408
|
|
|
$statusCode = 404; |
|
409
|
|
|
$title = 'Error'; |
|
410
|
|
|
$type = 'error'; |
|
411
|
|
|
$message = 'Error'; |
|
412
|
|
|
$result = 'Not Found'; |
|
413
|
|
|
} finally { |
|
414
|
|
|
return Response::json(array( |
|
415
|
|
|
'error' => $error, |
|
|
|
|
|
|
416
|
|
|
'status' => $statusCode, |
|
|
|
|
|
|
417
|
|
|
'title' => $title, |
|
|
|
|
|
|
418
|
|
|
'type' => $type, |
|
|
|
|
|
|
419
|
|
|
'message' => $message, |
|
|
|
|
|
|
420
|
|
|
'result' => $result |
|
|
|
|
|
|
421
|
|
|
)); |
|
422
|
|
|
} |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
public function transition(Request $request) |
|
426
|
|
|
{ |
|
427
|
|
|
$validator = Validator::make($request->all(), [ |
|
428
|
|
|
'client' => 'required', |
|
429
|
|
|
'request' => 'required', |
|
430
|
|
|
]); |
|
431
|
|
|
|
|
432
|
|
View Code Duplication |
if($validator->fails()) |
|
|
|
|
|
|
433
|
|
|
{ |
|
434
|
|
|
return Response::json(array( |
|
435
|
|
|
'title' => 'Error', |
|
436
|
|
|
'type' => 'error', |
|
437
|
|
|
'message' => $validator->errors()->all() |
|
438
|
|
|
)); |
|
439
|
|
|
} |
|
440
|
|
|
if(Auth::guest()){ $current_user = 1; } |
|
441
|
|
|
else{ $current_user = Auth::user()->id; } |
|
442
|
|
|
|
|
443
|
|
|
try { |
|
444
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$request->input('client')); |
|
445
|
|
|
$host = str_replace(array('https://', 'http://'), array('',''),$request->input('host')); |
|
446
|
|
|
$requests = ucwords($request->input('request')); |
|
447
|
|
|
$data = ApiKeys::where('client', 'like', '%' . $client . '%'); |
|
448
|
|
|
if($data->count() == 0){ |
|
449
|
|
|
$token = $this->token(); |
|
450
|
|
|
$api = New ApiKeys; |
|
451
|
|
|
$api->client = $client; |
|
452
|
|
|
$api->api_key = $token; |
|
453
|
|
|
$api->description = $requests; |
|
454
|
|
|
$api->user_id = $current_user; |
|
455
|
|
|
|
|
456
|
|
|
//create history default |
|
457
|
|
|
$model = "ApiKeys"; |
|
458
|
|
|
$fromState = "propose"; |
|
459
|
|
|
$toState = "propose"; |
|
460
|
|
|
$workflow = $this->getWorkflow($model); |
|
461
|
|
|
$statesFrom = $this->getState($fromState); |
|
462
|
|
|
$statesTo = $this->getState($toState); |
|
463
|
|
|
if($workflow->count() == 0){ |
|
464
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
|
465
|
|
|
return Redirect::to('api-manager'); |
|
466
|
|
|
} |
|
467
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
|
468
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
|
469
|
|
|
return Redirect::to('api-manager'); |
|
470
|
|
|
} |
|
471
|
|
|
else{ |
|
472
|
|
|
$api->save(); |
|
473
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
|
474
|
|
View Code Duplication |
if(env('URL_APIMANAGER') != NULL){ |
|
|
|
|
|
|
475
|
|
|
$url_apimanager = str_replace('"', '',env('URL_APIMANAGER')); |
|
476
|
|
|
if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){ |
|
477
|
|
|
$transition = "Propose to Propose"; |
|
478
|
|
|
$this->send_apimanager($url_apimanager,$client,$host,$transition); |
|
479
|
|
|
} |
|
480
|
|
|
} |
|
481
|
|
|
if($requests == 'Request'){ |
|
482
|
|
|
$model = "ApiKeys"; |
|
483
|
|
|
$fromState = "propose"; |
|
484
|
|
|
$toState = $requests; |
|
485
|
|
|
$workflow = $this->getWorkflow($model); |
|
486
|
|
|
$statesFrom = $this->getState($fromState); |
|
487
|
|
|
$statesTo = $this->getState($toState); |
|
488
|
|
View Code Duplication |
if($workflow->count() == 0){ |
|
|
|
|
|
|
489
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
|
490
|
|
|
return Redirect::to('api-manager'); |
|
491
|
|
|
} |
|
492
|
|
|
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
|
493
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
|
494
|
|
|
return Redirect::to('api-manager'); |
|
495
|
|
|
} |
|
496
|
|
|
else{ |
|
497
|
|
|
$this->saveHistory($api, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
|
498
|
|
|
|
|
499
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly. Your request has already been send.'); |
|
500
|
|
|
return Redirect::to('api-manager'); |
|
501
|
|
|
} |
|
502
|
|
|
} |
|
503
|
|
|
else{ |
|
504
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
|
505
|
|
|
return Redirect::to('api-manager'); |
|
506
|
|
|
} |
|
507
|
|
|
} |
|
508
|
|
|
} |
|
509
|
|
|
else { |
|
510
|
|
|
$get = $data->first(); |
|
511
|
|
|
$history = $this->getHistory($get->id)->get(); |
|
512
|
|
|
foreach ($history as $value) { |
|
513
|
|
|
$workstateto = $value->getStateTo->label; |
|
514
|
|
|
} |
|
515
|
|
|
if($workstateto == $requests){ |
|
516
|
|
|
// kirim ke client |
|
517
|
|
|
$error = true; |
|
518
|
|
|
$statusCode = 404; |
|
519
|
|
|
$title = 'Error'; |
|
520
|
|
|
$type = 'error'; |
|
521
|
|
|
$message = 'Data has already been taken.'; |
|
522
|
|
|
$result = $request->all(); |
|
523
|
|
|
|
|
524
|
|
|
Session::flash('message', 'Error 404 #error Data has already been taken.'); |
|
525
|
|
|
} |
|
526
|
|
View Code Duplication |
elseif($workstateto == 'Approved'){ |
|
|
|
|
|
|
527
|
|
|
// kirim ke client |
|
528
|
|
|
$error = true; |
|
529
|
|
|
$statusCode = 404; |
|
530
|
|
|
$title = 'Error'; |
|
531
|
|
|
$type = 'error'; |
|
532
|
|
|
$message = 'Data has already been Approved.'; |
|
533
|
|
|
$result = $request->all(); |
|
534
|
|
|
|
|
535
|
|
|
Session::flash('message', 'Error 101 #error Data has already been Approved.'); |
|
536
|
|
|
} |
|
537
|
|
View Code Duplication |
elseif($workstateto == 'Rejected'){ |
|
|
|
|
|
|
538
|
|
|
// kirim ke client |
|
539
|
|
|
$error = true; |
|
540
|
|
|
$statusCode = 404; |
|
541
|
|
|
$title = 'Error'; |
|
542
|
|
|
$type = 'error'; |
|
543
|
|
|
$message = 'Data has already been Rejected.'; |
|
544
|
|
|
$result = $request->all(); |
|
545
|
|
|
|
|
546
|
|
|
Session::flash('message', 'Error 101 #error Data has already been Rejected.'); |
|
547
|
|
|
} |
|
548
|
|
|
else { |
|
549
|
|
|
$model = "ApiKeys"; |
|
550
|
|
|
$fromState = $workstateto; |
|
551
|
|
|
$toState = $requests; |
|
552
|
|
|
$workflow = $this->getWorkflow($model); |
|
553
|
|
|
$statesFrom = $this->getState($fromState); |
|
554
|
|
|
$statesTo = $this->getState($toState); |
|
555
|
|
|
if($workflow->count() == 0){ |
|
556
|
|
|
// kirim ke client |
|
557
|
|
|
$error = true; |
|
558
|
|
|
$statusCode = 404; |
|
559
|
|
|
$title = 'Error'; |
|
560
|
|
|
$type = 'error'; |
|
561
|
|
|
$message = 'workflow not found.'; |
|
562
|
|
|
$result = $request->all(); |
|
563
|
|
|
|
|
564
|
|
|
Session::flash('message', 'Error 101 #error workflow not found'); |
|
565
|
|
|
} |
|
566
|
|
View Code Duplication |
elseif($statesTo->count() == 0 || $statesFrom->count() == 0){ |
|
|
|
|
|
|
567
|
|
|
// kirim ke client |
|
568
|
|
|
$error = true; |
|
569
|
|
|
$statusCode = 404; |
|
570
|
|
|
$title = 'Error'; |
|
571
|
|
|
$type = 'error'; |
|
572
|
|
|
$message = 'state not active or state not found.'; |
|
573
|
|
|
$result = $request->all(); |
|
574
|
|
|
|
|
575
|
|
|
Session::flash('message', 'Error 102 #error state not active or state not found'); |
|
576
|
|
|
} |
|
577
|
|
|
else{ |
|
578
|
|
|
$this->saveHistory($get, $workflow->first(), $statesFrom->first(), $statesTo->first()); |
|
579
|
|
|
// kirim ke client |
|
580
|
|
|
$error = false; |
|
581
|
|
|
$statusCode = 200; |
|
582
|
|
|
$title = 'Success'; |
|
583
|
|
|
$type = 'success'; |
|
584
|
|
|
$message = 'Data created successfully. Your request has already been send.'; |
|
585
|
|
|
$result = $get; |
|
586
|
|
|
|
|
587
|
|
|
Session::flash('message', 'Api Keys Data Saved Successfuly. Your request has already been send.'); |
|
588
|
|
|
} |
|
589
|
|
|
} |
|
590
|
|
|
$history = $this->getHistory($get->id)->get(); |
|
591
|
|
|
foreach ($history as $value) { |
|
592
|
|
|
$workstatefromid = $value->getStateFrom->id; |
|
|
|
|
|
|
593
|
|
|
$workstatetoid = $value->getStateTo->id; |
|
|
|
|
|
|
594
|
|
|
$workstatefrom = $value->getStateFrom->label; |
|
595
|
|
|
$workstateto = $value->getStateTo->label; |
|
596
|
|
|
} |
|
597
|
|
|
$state = $workstateto; |
|
598
|
|
|
$transition = $workstatefrom.' To '.$workstateto; |
|
|
|
|
|
|
599
|
|
|
$this->SendClient($client, $host, $error, $statusCode, $title, $type, $message, $result, $state, $transition); |
|
600
|
|
View Code Duplication |
if(env('URL_APIMANAGER') != NULL){ |
|
|
|
|
|
|
601
|
|
|
$url_apimanager = str_replace('"', '',env('URL_APIMANAGER')); |
|
602
|
|
|
if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){ |
|
603
|
|
|
$this->send_apimanager($url_apimanager,$client,$host,$transition); |
|
604
|
|
|
} |
|
605
|
|
|
} |
|
606
|
|
|
return Redirect::to('api-manager'); |
|
607
|
|
|
} |
|
608
|
|
|
} catch (Exception $e) { |
|
609
|
|
|
Session::flash('message', 'Error 404 #error not found'); |
|
610
|
|
|
return Redirect::to('api-manager'); |
|
611
|
|
|
} |
|
612
|
|
|
} |
|
613
|
|
|
|
|
614
|
|
|
private function SendClient($client, $host, $error, $statusCode, $title, $type, $message, $result, $state, $transition){ |
|
615
|
|
|
if(Auth::guest()){ $current_user = 1; } |
|
616
|
|
|
else{ $current_user = Auth::user()->id; } |
|
617
|
|
|
$headers = ['Content-Type' => 'application/json']; |
|
618
|
|
|
$data = [ |
|
619
|
|
|
'error' => $error, |
|
620
|
|
|
'status' => $statusCode, |
|
621
|
|
|
'title' => $title, |
|
622
|
|
|
'type' => $type, |
|
623
|
|
|
'message' => $message, |
|
624
|
|
|
'result' => $result, |
|
625
|
|
|
'hostname' => $host, |
|
626
|
|
|
'keys' => $result->api_key, |
|
627
|
|
|
'state' => $state, |
|
628
|
|
|
'transition' => $transition, |
|
629
|
|
|
'user_id' => $current_user |
|
630
|
|
|
]; |
|
631
|
|
|
$body = json_encode($data); |
|
632
|
|
|
|
|
633
|
|
|
//kalo udah rilis |
|
634
|
|
|
$urlget = $client."/api/v1/host-keys/".$host."/get"; |
|
635
|
|
|
|
|
636
|
|
|
//untuk local |
|
637
|
|
|
// $url = "bloger.local/api/v1/host-keys"; |
|
|
|
|
|
|
638
|
|
|
|
|
639
|
|
|
$clients = new \GuzzleHttp\Client(); |
|
640
|
|
|
$resget = $clients->request('GET', $urlget,['headers'=>$headers]); |
|
641
|
|
|
$responseget = $resget->getBody(); |
|
642
|
|
|
$responsesget = json_decode($responseget); |
|
643
|
|
|
|
|
644
|
|
|
if($responsesget->result != 'Not Found'){ |
|
645
|
|
|
$clients = new \GuzzleHttp\Client(); |
|
646
|
|
|
$url = $client."/api/v1/host-keys/".$responsesget->id; |
|
647
|
|
|
$res = $clients->request('PUT', $url,['headers'=>$headers,'body'=>$body]); |
|
648
|
|
|
$response = $res->getBody(); |
|
649
|
|
|
$responses = json_decode($response); |
|
650
|
|
|
}else { |
|
651
|
|
|
$clients = new \GuzzleHttp\Client(); |
|
652
|
|
|
$url = $client."/api/v1/host-keys"; |
|
653
|
|
|
$res = $clients->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
|
654
|
|
|
$response = $res->getBody(); |
|
655
|
|
|
$responses = json_decode($response); |
|
656
|
|
|
} |
|
657
|
|
|
return $responses; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
private function send_apimanager($url_apimanager,$client,$host,$keterangan){ |
|
661
|
|
|
if(Auth::guest()){ $current_user = 1; } |
|
662
|
|
|
else{ $current_user = Auth::user()->id; } |
|
663
|
|
|
$headers = ['Content-Type' => 'application/json']; |
|
664
|
|
|
$host = str_replace(array('https://', 'http://'), array('',''),$host); |
|
665
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$client); |
|
666
|
|
|
$data = [ |
|
667
|
|
|
'host' => $host, |
|
668
|
|
|
'client' => $client, |
|
669
|
|
|
'keterangan' => $keterangan, |
|
670
|
|
|
'user_id' => $current_user |
|
671
|
|
|
]; |
|
672
|
|
|
$body = json_encode($data); |
|
673
|
|
|
|
|
674
|
|
|
//kalo udah rilis |
|
675
|
|
|
$url = $url_apimanager."/api/store"; |
|
676
|
|
|
|
|
677
|
|
|
//untuk local |
|
678
|
|
|
// $url = "bloger.local/api/v1/host-keys"; |
|
|
|
|
|
|
679
|
|
|
|
|
680
|
|
|
$client = new \GuzzleHttp\Client(); |
|
681
|
|
|
$res = $client->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
|
682
|
|
|
$response = $res->getBody(); |
|
683
|
|
|
$responses = json_decode($response); |
|
684
|
|
|
return $responses; |
|
685
|
|
|
} |
|
686
|
|
|
|
|
687
|
|
|
public function receive(Request $request){ |
|
688
|
|
|
return Response::json(array( |
|
689
|
|
|
'error' => $request->error, |
|
690
|
|
|
'status' => $request->status, |
|
691
|
|
|
'title' => $request->title, |
|
692
|
|
|
'type' => $request->type, |
|
693
|
|
|
'message' => $request->message, |
|
694
|
|
|
'result' => $request->result |
|
695
|
|
|
)); |
|
696
|
|
|
} |
|
697
|
|
|
} |
|
698
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.