Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Admin/ApiController.php
14044 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Admin;
4
5
use Illuminate\View\View;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Pterodactyl\Models\ApiKey;
9
use Illuminate\Http\RedirectResponse;
10
use Prologue\Alerts\AlertsMessageBag;
11
use Pterodactyl\Services\Acl\Api\AdminAcl;
12
use Pterodactyl\Http\Controllers\Controller;
13
use Pterodactyl\Services\Api\KeyCreationService;
14
use Pterodactyl\Http\Requests\Admin\Api\StoreApplicationApiKeyRequest;
15
16
class ApiController extends Controller
17
{
18
/**
19
* ApiController constructor.
20
*/
21
public function __construct(
22
private AlertsMessageBag $alert,
23
private KeyCreationService $keyCreationService,
24
) {
25
}
26
27
/**
28
* Render view showing all of a user's application API keys.
29
*/
30
public function index(Request $request): View
31
{
32
return view('admin.api.index', [
33
'keys' => ApiKey::query()->where('key_type', ApiKey::TYPE_APPLICATION)->get(),
34
]);
35
}
36
37
/**
38
* Render view allowing an admin to create a new application API key.
39
*
40
* @throws \ReflectionException
41
*/
42
public function create(): View
43
{
44
$resources = AdminAcl::getResourceList();
45
sort($resources);
46
47
return view('admin.api.new', [
48
'resources' => $resources,
49
'permissions' => [
50
'r' => AdminAcl::READ,
51
'rw' => AdminAcl::READ | AdminAcl::WRITE,
52
'n' => AdminAcl::NONE,
53
],
54
]);
55
}
56
57
/**
58
* Store the new key and redirect the user back to the application key listing.
59
*
60
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
61
*/
62
public function store(StoreApplicationApiKeyRequest $request): RedirectResponse
63
{
64
$this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([
65
'memo' => $request->input('memo'),
66
'user_id' => $request->user()->id,
67
], $request->getKeyPermissions());
68
69
$this->alert->success('A new application API key has been generated for your account.')->flash();
70
71
return redirect()->route('admin.api.index');
72
}
73
74
/**
75
* Delete an application API key from the database.
76
*/
77
public function delete(Request $request, string $identifier): Response
78
{
79
ApiKey::query()
80
->where('key_type', ApiKey::TYPE_APPLICATION)
81
->where('identifier', $identifier)
82
->delete();
83
84
return response('', 204);
85
}
86
}
87
88