Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Client/SSHKeyController.php
10279 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Client;
4
5
use Illuminate\Http\JsonResponse;
6
use Pterodactyl\Facades\Activity;
7
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
8
use Pterodactyl\Transformers\Api\Client\UserSSHKeyTransformer;
9
use Pterodactyl\Http\Requests\Api\Client\Account\StoreSSHKeyRequest;
10
11
class SSHKeyController extends ClientApiController
12
{
13
/**
14
* Returns all the SSH keys that have been configured for the logged-in
15
* user account.
16
*/
17
public function index(ClientApiRequest $request): array
18
{
19
return $this->fractal->collection($request->user()->sshKeys)
20
->transformWith($this->getTransformer(UserSSHKeyTransformer::class))
21
->toArray();
22
}
23
24
/**
25
* Stores a new SSH key for the authenticated user's account.
26
*/
27
public function store(StoreSSHKeyRequest $request): array
28
{
29
$model = $request->user()->sshKeys()->create([
30
'name' => $request->input('name'),
31
'public_key' => $request->getPublicKey(),
32
'fingerprint' => $request->getKeyFingerprint(),
33
]);
34
35
Activity::event('user:ssh-key.create')
36
->subject($model)
37
->property('fingerprint', $request->getKeyFingerprint())
38
->log();
39
40
return $this->fractal->item($model)
41
->transformWith($this->getTransformer(UserSSHKeyTransformer::class))
42
->toArray();
43
}
44
45
/**
46
* Deletes an SSH key from the user's account.
47
*/
48
public function delete(ClientApiRequest $request): JsonResponse
49
{
50
$this->validate($request, ['fingerprint' => ['required', 'string']]);
51
52
$key = $request->user()->sshKeys()
53
->where('fingerprint', $request->input('fingerprint'))
54
->first();
55
56
if (!is_null($key)) {
57
$key->delete();
58
59
Activity::event('user:ssh-key.delete')
60
->subject($key)
61
->property('fingerprint', $key->fingerprint)
62
->log();
63
}
64
65
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
66
}
67
}
68
69