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
10279 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\Contracts\Repository\ApiKeyRepositoryInterface;
15
use Pterodactyl\Http\Requests\Admin\Api\StoreApplicationApiKeyRequest;
16
17
class ApiController extends Controller
18
{
19
/**
20
* ApiController constructor.
21
*/
22
public function __construct(
23
private AlertsMessageBag $alert,
24
private ApiKeyRepositoryInterface $repository,
25
private KeyCreationService $keyCreationService,
26
) {
27
}
28
29
/**
30
* Render view showing all of a user's application API keys.
31
*/
32
public function index(Request $request): View
33
{
34
return view('admin.api.index', [
35
'keys' => $this->repository->getApplicationKeys($request->user()),
36
]);
37
}
38
39
/**
40
* Render view allowing an admin to create a new application API key.
41
*
42
* @throws \ReflectionException
43
*/
44
public function create(): View
45
{
46
$resources = AdminAcl::getResourceList();
47
sort($resources);
48
49
return view('admin.api.new', [
50
'resources' => $resources,
51
'permissions' => [
52
'r' => AdminAcl::READ,
53
'rw' => AdminAcl::READ | AdminAcl::WRITE,
54
'n' => AdminAcl::NONE,
55
],
56
]);
57
}
58
59
/**
60
* Store the new key and redirect the user back to the application key listing.
61
*
62
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
63
*/
64
public function store(StoreApplicationApiKeyRequest $request): RedirectResponse
65
{
66
$this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([
67
'memo' => $request->input('memo'),
68
'user_id' => $request->user()->id,
69
], $request->getKeyPermissions());
70
71
$this->alert->success('A new application API key has been generated for your account.')->flash();
72
73
return redirect()->route('admin.api.index');
74
}
75
76
/**
77
* Delete an application API key from the database.
78
*/
79
public function delete(Request $request, string $identifier): Response
80
{
81
$this->repository->deleteApplicationKey($request->user(), $identifier);
82
83
return response('', 204);
84
}
85
}
86
87