|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace crocodicstudio\crudbooster\Modules\ApiGeneratorModule; |
|
4
|
|
|
|
|
5
|
|
|
use crocodicstudio\crudbooster\controllers\CBController; |
|
6
|
|
|
use crocodicstudio\crudbooster\helpers\CbValidator; |
|
7
|
|
|
|
|
8
|
|
|
class AdminApiKeyController extends CBController |
|
9
|
|
|
{ |
|
10
|
|
|
private $apiKeysRepository; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* AdminApiKeyController constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param \crocodicstudio\crudbooster\Modules\ApiGeneratorModule\ApiKeysRepository $apiKeysRepository |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(ApiKeysRepository $apiKeysRepository) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->apiKeysRepository = $apiKeysRepository; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function cbInit() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->table = 'cms_apicustom'; |
|
25
|
|
|
$this->primaryKey = "id"; |
|
26
|
|
|
$this->titleField = "name"; |
|
27
|
|
|
$this->buttonShow = false; |
|
28
|
|
|
$this->deleteBtn = false; |
|
29
|
|
|
$this->buttonAdd = false; |
|
30
|
|
|
$this->buttonImport = false; |
|
31
|
|
|
$this->buttonExport = false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getSecretKey() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->cbLoader(); |
|
37
|
|
|
$data = [ |
|
38
|
|
|
'page_title' => 'API Generator', |
|
39
|
|
|
'apikeys' => $this->apiKeysRepository->get(), |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
return view('CbApiGen::api_key', $data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
function getGenerateSecretKey() |
|
|
|
|
|
|
46
|
|
|
{ |
|
47
|
|
|
$this->cbLoader(); |
|
48
|
|
|
//Generate a random string. |
|
49
|
|
|
$token = openssl_random_pseudo_bytes(16); |
|
50
|
|
|
|
|
51
|
|
|
//Convert the binary data into hexadecimal representation. |
|
52
|
|
|
$token = bin2hex($token); |
|
53
|
|
|
$id = $this->apiKeysRepository->insertGetId($token); |
|
54
|
|
|
|
|
55
|
|
|
$response = [ |
|
56
|
|
|
'id' => $id, |
|
57
|
|
|
'key' => $token, |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
return response()->json($response); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getStatusApikey() |
|
64
|
|
|
{ |
|
65
|
|
|
CbValidator::valid(['id' => 'required', 'status' => 'required'], 'view'); |
|
66
|
|
|
|
|
67
|
|
|
$id = request('id'); |
|
68
|
|
|
$status = (request('status') == 1) ? "active" : "non active"; |
|
69
|
|
|
|
|
70
|
|
|
$this->apiKeysRepository->updateById($status, $id); |
|
71
|
|
|
|
|
72
|
|
|
backWithMsg('You have been update api key status !'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getDeleteApiKey() |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->apiKeysRepository->deleteById(request('id'))) { |
|
78
|
|
|
return response()->json(['status' => 1]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return response()->json(['status' => 0]); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.