Path: blob/1.0-develop/app/Http/Controllers/Admin/NodeAutoDeployController.php
10284 views
<?php12namespace Pterodactyl\Http\Controllers\Admin;34use Illuminate\Http\Request;5use Pterodactyl\Models\Node;6use Pterodactyl\Models\ApiKey;7use Illuminate\Http\JsonResponse;8use Pterodactyl\Http\Controllers\Controller;9use Illuminate\Contracts\Encryption\Encrypter;10use Pterodactyl\Services\Api\KeyCreationService;11use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;1213class NodeAutoDeployController extends Controller14{15/**16* NodeAutoDeployController constructor.17*/18public function __construct(19private ApiKeyRepository $repository,20private Encrypter $encrypter,21private KeyCreationService $keyCreationService,22) {23}2425/**26* Generates a new API key for the logged-in user with only permission to read27* nodes, and returns that as the deployment key for a node.28*29* @throws \Pterodactyl\Exceptions\Model\DataValidationException30*/31public function __invoke(Request $request, Node $node): JsonResponse32{33/** @var ApiKey|null $key */34$key = $this->repository->getApplicationKeys($request->user())35->filter(function (ApiKey $key) {36foreach ($key->getAttributes() as $permission => $value) {37if ($permission === 'r_nodes' && $value === 1) {38return true;39}40}4142return false;43})44->first();4546// We couldn't find a key that exists for this user with only permission for47// reading nodes. Go ahead and create it now.48if (!$key) {49$key = $this->keyCreationService->setKeyType(ApiKey::TYPE_APPLICATION)->handle([50'user_id' => $request->user()->id,51'memo' => 'Automatically generated node deployment key.',52'allowed_ips' => [],53], ['r_nodes' => 1]);54}5556return new JsonResponse([57'node' => $node->id,58'token' => $key->identifier . $this->encrypter->decrypt($key->token),59]);60}61}626364