2022-05-14 21:31:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2022-05-29 22:48:35 +00:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2022-05-14 21:31:53 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
2022-05-14 22:08:48 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\UserSSHKeyTransformer;
|
2022-05-14 21:31:53 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Account\StoreSSHKeyRequest;
|
|
|
|
|
|
|
|
class SSHKeyController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Returns all the SSH keys that have been configured for the logged-in
|
2022-05-14 21:31:53 +00:00
|
|
|
* user account.
|
|
|
|
*/
|
|
|
|
public function index(ClientApiRequest $request): array
|
|
|
|
{
|
|
|
|
return $this->fractal->collection($request->user()->sshKeys)
|
2022-12-15 00:05:46 +00:00
|
|
|
->transformWith(UserSSHKeyTransformer::class)
|
2022-05-14 21:31:53 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores a new SSH key for the authenticated user's account.
|
|
|
|
*/
|
|
|
|
public function store(StoreSSHKeyRequest $request): array
|
|
|
|
{
|
|
|
|
$model = $request->user()->sshKeys()->create([
|
|
|
|
'name' => $request->input('name'),
|
2022-05-15 20:41:15 +00:00
|
|
|
'public_key' => $request->getPublicKey(),
|
2022-05-14 21:31:53 +00:00
|
|
|
'fingerprint' => $request->getKeyFingerprint(),
|
|
|
|
]);
|
|
|
|
|
2022-05-29 22:48:35 +00:00
|
|
|
Activity::event('user:ssh-key.create')
|
|
|
|
->subject($model)
|
|
|
|
->property('fingerprint', $request->getKeyFingerprint())
|
|
|
|
->log();
|
|
|
|
|
2022-05-14 21:31:53 +00:00
|
|
|
return $this->fractal->item($model)
|
2022-12-15 00:05:46 +00:00
|
|
|
->transformWith(UserSSHKeyTransformer::class)
|
2022-05-14 21:31:53 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes an SSH key from the user's account.
|
|
|
|
*/
|
2022-05-30 21:28:42 +00:00
|
|
|
public function delete(ClientApiRequest $request): JsonResponse
|
2022-05-14 21:31:53 +00:00
|
|
|
{
|
2022-05-30 21:28:42 +00:00
|
|
|
$this->validate($request, ['fingerprint' => ['required', 'string']]);
|
2022-05-29 22:48:35 +00:00
|
|
|
|
2022-05-30 21:28:42 +00:00
|
|
|
$key = $request->user()->sshKeys()
|
|
|
|
->where('fingerprint', $request->input('fingerprint'))
|
|
|
|
->first();
|
2022-05-29 22:48:35 +00:00
|
|
|
|
2022-05-30 21:28:42 +00:00
|
|
|
if (!is_null($key)) {
|
|
|
|
$key->delete();
|
|
|
|
|
|
|
|
Activity::event('user:ssh-key.delete')
|
|
|
|
->subject($key)
|
|
|
|
->property('fingerprint', $key->fingerprint)
|
|
|
|
->log();
|
|
|
|
}
|
2022-05-14 21:31:53 +00:00
|
|
|
|
|
|
|
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
|
|
|
|
}
|
|
|
|
}
|