Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Admin/NodeAutoDeployController.php
10284 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Admin;
4
5
use Illuminate\Http\Request;
6
use Pterodactyl\Models\Node;
7
use Pterodactyl\Models\ApiKey;
8
use Illuminate\Http\JsonResponse;
9
use Pterodactyl\Http\Controllers\Controller;
10
use Illuminate\Contracts\Encryption\Encrypter;
11
use Pterodactyl\Services\Api\KeyCreationService;
12
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
13
14
class NodeAutoDeployController extends Controller
15
{
16
/**
17
* NodeAutoDeployController constructor.
18
*/
19
public function __construct(
20
private ApiKeyRepository $repository,
21
private Encrypter $encrypter,
22
private KeyCreationService $keyCreationService,
23
) {
24
}
25
26
/**
27
* Generates a new API key for the logged-in user with only permission to read
28
* nodes, and returns that as the deployment key for a node.
29
*
30
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
31
*/
32
public function __invoke(Request $request, Node $node): JsonResponse
33
{
34
/** @var ApiKey|null $key */
35
$key = $this->repository->getApplicationKeys($request->user())
36
->filter(function (ApiKey $key) {
37
foreach ($key->getAttributes() as $permission => $value) {
38
if ($permission === 'r_nodes' && $value === 1) {
39
return true;
40
}
41
}
42
43
return false;
44
})
45
->first();
46
47
// We couldn't find a key that exists for this user with only permission for
48
// reading nodes. Go ahead and create it now.
49
if (!$key) {
50
$key = $this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([
51
'user_id' => $request->user()->id,
52
'memo' => 'Automatically generated node deployment key.',
53
'allowed_ips' => [],
54
], ['r_nodes' => 1]);
55
}
56
57
return new JsonResponse([
58
'node' => $node->id,
59
'token' => $key->identifier . $this->encrypter->decrypt($key->token),
60
]);
61
}
62
}
63
64