From 28e0668b76f1544593e31672a20b72a52be0f3c5 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 23 Oct 2022 16:36:35 -0400 Subject: [PATCH] Replace api key repository --- .../Repository/ApiKeyRepositoryInterface.php | 29 --------- app/Http/Controllers/Admin/ApiController.php | 20 ++++-- .../Admin/NodeAutoDeployController.php | 14 +++-- app/Providers/RepositoryServiceProvider.php | 3 - .../Eloquent/ApiKeyRepository.php | 61 ------------------- app/Services/Api/KeyCreationService.php | 6 +- 6 files changed, 28 insertions(+), 105 deletions(-) delete mode 100644 app/Contracts/Repository/ApiKeyRepositoryInterface.php delete mode 100644 app/Repositories/Eloquent/ApiKeyRepository.php diff --git a/app/Contracts/Repository/ApiKeyRepositoryInterface.php b/app/Contracts/Repository/ApiKeyRepositoryInterface.php deleted file mode 100644 index bfebbddb6..000000000 --- a/app/Contracts/Repository/ApiKeyRepositoryInterface.php +++ /dev/null @@ -1,29 +0,0 @@ -user(); + + $keys = $user->apiKeys() + ->where('key_type', ApiKey::TYPE_APPLICATION) + ->get(); + 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 { - $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); } diff --git a/app/Http/Controllers/Admin/NodeAutoDeployController.php b/app/Http/Controllers/Admin/NodeAutoDeployController.php index ac0684a9c..3495f9bd3 100644 --- a/app/Http/Controllers/Admin/NodeAutoDeployController.php +++ b/app/Http/Controllers/Admin/NodeAutoDeployController.php @@ -8,8 +8,8 @@ use Pterodactyl\Models\ApiKey; use Illuminate\Http\JsonResponse; use Pterodactyl\Http\Controllers\Controller; use Illuminate\Contracts\Encryption\Encrypter; +use Pterodactyl\Models\User; use Pterodactyl\Services\Api\KeyCreationService; -use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; class NodeAutoDeployController extends Controller { @@ -17,7 +17,6 @@ class NodeAutoDeployController extends Controller * NodeAutoDeployController constructor. */ public function __construct( - private ApiKeyRepository $repository, private Encrypter $encrypter, private KeyCreationService $keyCreationService ) { @@ -31,8 +30,15 @@ class NodeAutoDeployController extends Controller */ public function __invoke(Request $request, Node $node): JsonResponse { - /** @var \Pterodactyl\Models\ApiKey|null $key */ - $key = $this->repository->getApplicationKeys($request->user()) + /** @var User $user */ + $user = $request->user(); + + $keys = $user->apiKeys() + ->where('key_type', ApiKey::TYPE_APPLICATION) + ->get(); + + /** @var ApiKey|null $key */ + $key = $keys ->filter(function (ApiKey $key) { foreach ($key->getAttributes() as $permission => $value) { if ($permission === 'r_nodes' && $value === 1) { diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 8a0434f52..b91f60ace 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -8,7 +8,6 @@ use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository; use Pterodactyl\Repositories\Eloquent\UserRepository; -use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository; use Pterodactyl\Repositories\Eloquent\SubuserRepository; @@ -24,7 +23,6 @@ use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; -use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Contracts\Repository\SessionRepositoryInterface; @@ -47,7 +45,6 @@ class RepositoryServiceProvider extends ServiceProvider { // Eloquent Repositories $this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class); - $this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class); $this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class); $this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class); $this->app->bind(EggRepositoryInterface::class, EggRepository::class); diff --git a/app/Repositories/Eloquent/ApiKeyRepository.php b/app/Repositories/Eloquent/ApiKeyRepository.php deleted file mode 100644 index eb1a362ae..000000000 --- a/app/Repositories/Eloquent/ApiKeyRepository.php +++ /dev/null @@ -1,61 +0,0 @@ -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(); - } -} diff --git a/app/Services/Api/KeyCreationService.php b/app/Services/Api/KeyCreationService.php index f026b9f22..a1aa21fa0 100644 --- a/app/Services/Api/KeyCreationService.php +++ b/app/Services/Api/KeyCreationService.php @@ -4,7 +4,6 @@ namespace Pterodactyl\Services\Api; use Pterodactyl\Models\ApiKey; use Illuminate\Contracts\Encryption\Encrypter; -use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; class KeyCreationService { @@ -13,7 +12,7 @@ class KeyCreationService /** * 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 * stored in the database. * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException */ public function handle(array $data, array $permissions = []): ApiKey { @@ -47,6 +45,6 @@ class KeyCreationService $data = array_merge($data, $permissions); } - return $this->repository->create($data, true, true); + return ApiKey::query()->forceCreate($data); } }