misc_pterodactyl-panel/app/Http/Controllers/Api/Client/ApiKeyController.php

72 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Api\Client;
use Pterodactyl\Models\ApiKey;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
use Pterodactyl\Transformers\Api\Client\ApiKeyTransformer;
use Pterodactyl\Http\Requests\Api\Client\Account\StoreApiKeyRequest;
class ApiKeyController extends ClientApiController
{
/**
* Returns all the API keys that exist for the given client.
*/
public function index(ClientApiRequest $request): array
{
return $this->fractal->collection($request->user()->apiKeys)
2022-12-15 00:05:46 +00:00
->transformWith(ApiKeyTransformer::class)
->toArray();
}
/**
* Store a new API key for a user's account.
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function store(StoreApiKeyRequest $request): array
{
if ($request->user()->apiKeys->count() >= 25) {
2021-01-23 20:33:34 +00:00
throw new DisplayException('You have reached the account limit for number of API keys.');
}
$token = $request->user()->createToken(
$request->input('description'),
$request->input('allowed_ips')
);
Activity::event('user:api-key.create')
->subject($token->accessToken)
->property('identifier', $token->accessToken->identifier)
->log();
return $this->fractal->item($token->accessToken)
2022-12-15 00:05:46 +00:00
->transformWith(ApiKeyTransformer::class)
->addMeta(['secret_token' => $token->plainTextToken])
->toArray();
}
/**
* Deletes a given API key.
*/
public function delete(ClientApiRequest $request, string $identifier): JsonResponse
{
2022-05-30 00:39:51 +00:00
/** @var \Pterodactyl\Models\ApiKey $key */
$key = $request->user()->apiKeys()
->where('key_type', ApiKey::TYPE_ACCOUNT)
->where('identifier', $identifier)
2022-05-30 00:39:51 +00:00
->firstOrFail();
Activity::event('user:api-key.delete')
2022-06-18 16:16:54 +00:00
->property('identifier', $key->identifier)
->log();
$key->delete();
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}