feat(ssh-keys): add ssh key endpoints and ui components
This commit is contained in:
parent
9d64c6751b
commit
f9114e2de0
17 changed files with 375 additions and 7 deletions
|
@ -3,11 +3,9 @@
|
|||
namespace Pterodactyl\Http\Controllers\Api\Application\Roles;
|
||||
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Models\Location;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Pterodactyl\Models\AdminRole;
|
||||
use Spatie\QueryBuilder\QueryBuilder;
|
||||
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
|
||||
use Pterodactyl\Transformers\Api\Application\AdminRoleTransformer;
|
||||
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
|
||||
use Pterodactyl\Http\Requests\Api\Application\Roles\GetRoleRequest;
|
||||
|
|
58
app/Http/Controllers/Api/Client/SSHKeyController.php
Normal file
58
app/Http/Controllers/Api/Client/SSHKeyController.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\Api\Client;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Pterodactyl\Models\UserSSHKey;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Transformers\Api\Client\UserSSHKeyTransformer;
|
||||
use Pterodactyl\Http\Requests\Api\Client\Account\StoreSSHKeyRequest;
|
||||
|
||||
class SSHKeyController extends ClientApiController
|
||||
{
|
||||
/**
|
||||
* ?
|
||||
*
|
||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
||||
*/
|
||||
public function index(Request $request): \Pterodactyl\Extensions\Spatie\Fractalistic\Fractal
|
||||
{
|
||||
return $this->fractal->collection(UserSSHKey::query()->where('user_id', '=', $request->user()->id)->get())
|
||||
->transformWith($this->getTransformer(UserSSHKeyTransformer::class));
|
||||
}
|
||||
|
||||
/**
|
||||
* ?
|
||||
*
|
||||
* @return JsonResponse
|
||||
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
*/
|
||||
public function store(StoreSSHKeyRequest $request): JsonResponse
|
||||
{
|
||||
if ($request->user()->sshKeys->count() >= 5) {
|
||||
throw new DisplayException('You have reached the account limit for number of SSH keys.');
|
||||
}
|
||||
|
||||
$data = array_merge($request->validated(), [
|
||||
'user_id' => $request->user()->id,
|
||||
]);
|
||||
$key = UserSSHKey::query()->create($data);
|
||||
|
||||
return $this->fractal->item($key)
|
||||
->transformWith($this->getTransformer(UserSSHKeyTransformer::class))
|
||||
->respond(JsonResponse::HTTP_CREATED);
|
||||
}
|
||||
|
||||
/**
|
||||
* ?
|
||||
*/
|
||||
public function delete(Request $request, UserSSHKey $sshKey): Response
|
||||
{
|
||||
$sshKey->delete();
|
||||
|
||||
return new Response('', Response::HTTP_NO_CONTENT);
|
||||
}
|
||||
}
|
14
app/Http/Requests/Api/Client/Account/StoreSSHKeyRequest.php
Normal file
14
app/Http/Requests/Api/Client/Account/StoreSSHKeyRequest.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Client\Account;
|
||||
|
||||
use Pterodactyl\Models\UserSSHKey;
|
||||
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
||||
|
||||
class StoreSSHKeyRequest extends ClientApiRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return UserSSHKey::getRules();
|
||||
}
|
||||
}
|
|
@ -45,7 +45,7 @@ class AuditLog extends Model
|
|||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public static $validationRules = [
|
||||
public static array $validationRules = [
|
||||
'uuid' => 'required|uuid',
|
||||
'action' => 'required|string|max:191',
|
||||
'subaction' => 'nullable|string|max:191',
|
||||
|
|
|
@ -64,7 +64,7 @@ class DatabaseHost extends Model
|
|||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $validationRules = [
|
||||
public static array $validationRules = [
|
||||
'name' => 'required|string|max:191',
|
||||
'host' => 'required|string',
|
||||
'port' => 'required|numeric|between:1,65535',
|
||||
|
|
|
@ -70,7 +70,7 @@ class EggVariable extends Model
|
|||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $validationRules = [
|
||||
public static array $validationRules = [
|
||||
'egg_id' => 'exists:eggs,id',
|
||||
'name' => 'required|string|between:1,191',
|
||||
'description' => 'string',
|
||||
|
|
|
@ -40,6 +40,7 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
|
|||
* @property \Pterodactyl\Models\AdminRole $adminRole
|
||||
* @property \Pterodactyl\Models\ApiKey[]|\Illuminate\Database\Eloquent\Collection $apiKeys
|
||||
* @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers
|
||||
* @property \Pterodactyl\Models\UserSSHKey|\Illuminate\Database\Eloquent\Collection $sshKeys
|
||||
* @property \Pterodactyl\Models\RecoveryToken[]|\Illuminate\Database\Eloquent\Collection $recoveryTokens
|
||||
* @property \LaravelWebauthn\Models\WebauthnKey[]|\Illuminate\Database\Eloquent\Collection $webauthnKeys
|
||||
*/
|
||||
|
@ -247,6 +248,11 @@ class User extends Model implements
|
|||
return $this->hasMany(Server::class, 'owner_id');
|
||||
}
|
||||
|
||||
public function sshKeys(): HasMany
|
||||
{
|
||||
return $this->hasMany(UserSSHKey::class);
|
||||
}
|
||||
|
||||
public function recoveryTokens(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecoveryToken::class);
|
||||
|
|
|
@ -2,15 +2,20 @@
|
|||
|
||||
namespace Pterodactyl\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property string $name
|
||||
* @property string $public_key
|
||||
* @property \Carbon\CarbonImmutable $created_at
|
||||
* @property \Pterodactyl\Models\User $user
|
||||
*/
|
||||
class UserSSHKey extends Model
|
||||
{
|
||||
const UPDATED_AT = null;
|
||||
|
||||
protected $table = 'user_ssh_keys';
|
||||
protected bool $immutableDates = true;
|
||||
|
||||
|
@ -23,4 +28,9 @@ class UserSSHKey extends Model
|
|||
'name' => 'required|string',
|
||||
'public_key' => 'required|string',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
|
|
29
app/Transformers/Api/Client/UserSSHKeyTransformer.php
Normal file
29
app/Transformers/Api/Client/UserSSHKeyTransformer.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Transformers\Api\Client;
|
||||
|
||||
use Pterodactyl\Models\UserSSHKey;
|
||||
|
||||
class UserSSHKeyTransformer extends BaseClientTransformer
|
||||
{
|
||||
/**
|
||||
* Return the resource name for the JSONAPI output.
|
||||
*/
|
||||
public function getResourceName(): string
|
||||
{
|
||||
return 'user_ssh_key';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return basic information about the currently logged in user.
|
||||
*/
|
||||
public function transform(UserSSHKey $model): array
|
||||
{
|
||||
return [
|
||||
'id' => $model->id,
|
||||
'name' => $model->name,
|
||||
'public_key' => $model->public_key,
|
||||
'created_at' => $model->created_at->toIso8601String(),
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue