Replace api key repository

This commit is contained in:
Lance Pioch 2022-10-23 16:36:35 -04:00
parent 860b2d890b
commit 28e0668b76
6 changed files with 28 additions and 105 deletions

View file

@ -1,29 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\User;
use Illuminate\Support\Collection;
interface ApiKeyRepositoryInterface extends RepositoryInterface
{
/**
* Get all the account API keys that exist for a specific user.
*/
public function getAccountKeys(User $user): Collection;
/**
* Get all the application API keys that exist for a specific user.
*/
public function getApplicationKeys(User $user): Collection;
/**
* Delete an account API key from the panel for a specific user.
*/
public function deleteAccountKey(User $user, string $identifier): int;
/**
* Delete an application API key from the panel for a specific user.
*/
public function deleteApplicationKey(User $user, string $identifier): int;
}

View file

@ -4,6 +4,7 @@ namespace Pterodactyl\Http\Controllers\Admin;
use Illuminate\View\View; use Illuminate\View\View;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Pterodactyl\Models\ApiKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
@ -12,7 +13,6 @@ use Pterodactyl\Services\Acl\Api\AdminAcl;
use Illuminate\View\Factory as ViewFactory; use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Api\KeyCreationService; use Pterodactyl\Services\Api\KeyCreationService;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Api\StoreApplicationApiKeyRequest; use Pterodactyl\Http\Requests\Admin\Api\StoreApplicationApiKeyRequest;
class ApiController extends Controller class ApiController extends Controller
@ -22,7 +22,6 @@ class ApiController extends Controller
*/ */
public function __construct( public function __construct(
private AlertsMessageBag $alert, private AlertsMessageBag $alert,
private ApiKeyRepositoryInterface $repository,
private KeyCreationService $keyCreationService, private KeyCreationService $keyCreationService,
private ViewFactory $view, private ViewFactory $view,
) { ) {
@ -33,8 +32,15 @@ class ApiController extends Controller
*/ */
public function index(Request $request): View public function index(Request $request): View
{ {
/** @var User $user */
$user = $request->user();
$keys = $user->apiKeys()
->where('key_type', ApiKey::TYPE_APPLICATION)
->get();
return $this->view->make('admin.api.index', [ return $this->view->make('admin.api.index', [
'keys' => $this->repository->getApplicationKeys($request->user()), 'keys' => $keys,
]); ]);
} }
@ -80,7 +86,13 @@ class ApiController extends Controller
*/ */
public function delete(Request $request, string $identifier): Response public function delete(Request $request, string $identifier): Response
{ {
$this->repository->deleteApplicationKey($request->user(), $identifier); /** @var User $user */
$user = $request->user();
$user->apiKeys()
->where('key_type', ApiKey::TYPE_APPLICATION)
->where('identifier', $identifier)
->delete();
return response('', 204); return response('', 204);
} }

View file

@ -8,8 +8,8 @@ use Pterodactyl\Models\ApiKey;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Models\User;
use Pterodactyl\Services\Api\KeyCreationService; use Pterodactyl\Services\Api\KeyCreationService;
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
class NodeAutoDeployController extends Controller class NodeAutoDeployController extends Controller
{ {
@ -17,7 +17,6 @@ class NodeAutoDeployController extends Controller
* NodeAutoDeployController constructor. * NodeAutoDeployController constructor.
*/ */
public function __construct( public function __construct(
private ApiKeyRepository $repository,
private Encrypter $encrypter, private Encrypter $encrypter,
private KeyCreationService $keyCreationService private KeyCreationService $keyCreationService
) { ) {
@ -31,8 +30,15 @@ class NodeAutoDeployController extends Controller
*/ */
public function __invoke(Request $request, Node $node): JsonResponse public function __invoke(Request $request, Node $node): JsonResponse
{ {
/** @var \Pterodactyl\Models\ApiKey|null $key */ /** @var User $user */
$key = $this->repository->getApplicationKeys($request->user()) $user = $request->user();
$keys = $user->apiKeys()
->where('key_type', ApiKey::TYPE_APPLICATION)
->get();
/** @var ApiKey|null $key */
$key = $keys
->filter(function (ApiKey $key) { ->filter(function (ApiKey $key) {
foreach ($key->getAttributes() as $permission => $value) { foreach ($key->getAttributes() as $permission => $value) {
if ($permission === 'r_nodes' && $value === 1) { if ($permission === 'r_nodes' && $value === 1) {

View file

@ -8,7 +8,6 @@ use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\TaskRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository;
use Pterodactyl\Repositories\Eloquent\UserRepository; use Pterodactyl\Repositories\Eloquent\UserRepository;
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Eloquent\SessionRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository;
use Pterodactyl\Repositories\Eloquent\SubuserRepository; use Pterodactyl\Repositories\Eloquent\SubuserRepository;
@ -24,7 +23,6 @@ use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface; use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
@ -47,7 +45,6 @@ class RepositoryServiceProvider extends ServiceProvider
{ {
// Eloquent Repositories // Eloquent Repositories
$this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class); $this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class);
$this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class);
$this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class); $this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class);
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class); $this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
$this->app->bind(EggRepositoryInterface::class, EggRepository::class); $this->app->bind(EggRepositoryInterface::class, EggRepository::class);

View file

@ -1,61 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\User;
use Pterodactyl\Models\ApiKey;
use Illuminate\Support\Collection;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class ApiKeyRepository extends EloquentRepository implements ApiKeyRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return ApiKey::class;
}
/**
* Get all the account API keys that exist for a specific user.
*/
public function getAccountKeys(User $user): Collection
{
return $this->getBuilder()->where('user_id', $user->id)
->where('key_type', ApiKey::TYPE_ACCOUNT)
->get($this->getColumns());
}
/**
* Get all the application API keys that exist for a specific user.
*/
public function getApplicationKeys(User $user): Collection
{
return $this->getBuilder()->where('user_id', $user->id)
->where('key_type', ApiKey::TYPE_APPLICATION)
->get($this->getColumns());
}
/**
* Delete an account API key from the panel for a specific user.
*/
public function deleteAccountKey(User $user, string $identifier): int
{
return $this->getBuilder()->where('user_id', $user->id)
->where('key_type', ApiKey::TYPE_ACCOUNT)
->where('identifier', $identifier)
->delete();
}
/**
* Delete an application API key from the panel for a specific user.
*/
public function deleteApplicationKey(User $user, string $identifier): int
{
return $this->getBuilder()->where('user_id', $user->id)
->where('key_type', ApiKey::TYPE_APPLICATION)
->where('identifier', $identifier)
->delete();
}
}

View file

@ -4,7 +4,6 @@ namespace Pterodactyl\Services\Api;
use Pterodactyl\Models\ApiKey; use Pterodactyl\Models\ApiKey;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
class KeyCreationService class KeyCreationService
{ {
@ -13,7 +12,7 @@ class KeyCreationService
/** /**
* ApiKeyService constructor. * ApiKeyService constructor.
*/ */
public function __construct(private ApiKeyRepositoryInterface $repository, private Encrypter $encrypter) public function __construct(private Encrypter $encrypter)
{ {
} }
@ -33,7 +32,6 @@ class KeyCreationService
* This will automatically generate an identifier and an encrypted token that are * This will automatically generate an identifier and an encrypted token that are
* stored in the database. * stored in the database.
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/ */
public function handle(array $data, array $permissions = []): ApiKey public function handle(array $data, array $permissions = []): ApiKey
{ {
@ -47,6 +45,6 @@ class KeyCreationService
$data = array_merge($data, $permissions); $data = array_merge($data, $permissions);
} }
return $this->repository->create($data, true, true); return ApiKey::query()->forceCreate($data);
} }
} }