From 89477421e1b530a1911c675cc8ae27687b445b79 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sat, 16 Mar 2024 20:00:31 -0400 Subject: [PATCH] Remove api key repository --- .../Repository/ApiKeyRepositoryInterface.php | 29 --------- app/Http/Controllers/Admin/ApiController.php | 13 ++-- .../Admin/NodeAutoDeployController.php | 10 +-- app/Providers/RepositoryServiceProvider.php | 3 - .../Eloquent/ApiKeyRepository.php | 61 ------------------- app/Services/Api/KeyCreationService.php | 7 +-- 6 files changed, 18 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 851cb99f4..000000000 --- a/app/Contracts/Repository/ApiKeyRepositoryInterface.php +++ /dev/null @@ -1,29 +0,0 @@ -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 +82,10 @@ class ApiController extends Controller */ public function delete(Request $request, string $identifier): Response { - $this->repository->deleteApplicationKey($request->user(), $identifier); + $request->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 6598dab67..6cd184ab3 100644 --- a/app/Http/Controllers/Admin/NodeAutoDeployController.php +++ b/app/Http/Controllers/Admin/NodeAutoDeployController.php @@ -9,7 +9,6 @@ use Illuminate\Http\JsonResponse; use App\Http\Controllers\Controller; use Illuminate\Contracts\Encryption\Encrypter; use App\Services\Api\KeyCreationService; -use App\Repositories\Eloquent\ApiKeyRepository; class NodeAutoDeployController extends Controller { @@ -17,7 +16,6 @@ class NodeAutoDeployController extends Controller * NodeAutoDeployController constructor. */ public function __construct( - private ApiKeyRepository $repository, private Encrypter $encrypter, private KeyCreationService $keyCreationService ) { @@ -31,8 +29,12 @@ class NodeAutoDeployController extends Controller */ public function __invoke(Request $request, Node $node): JsonResponse { - /** @var \App\Models\ApiKey|null $key */ - $key = $this->repository->getApplicationKeys($request->user()) + $keys = $request->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 296b5db28..8ca9eb196 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -5,7 +5,6 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Repositories\Eloquent\EggRepository; use App\Repositories\Eloquent\NodeRepository; -use App\Repositories\Eloquent\ApiKeyRepository; use App\Repositories\Eloquent\SessionRepository; use App\Repositories\Eloquent\SubuserRepository; use App\Repositories\Eloquent\DatabaseRepository; @@ -14,7 +13,6 @@ use App\Contracts\Repository\EggRepositoryInterface; use App\Repositories\Eloquent\EggVariableRepository; use App\Contracts\Repository\NodeRepositoryInterface; use App\Repositories\Eloquent\DatabaseHostRepository; -use App\Contracts\Repository\ApiKeyRepositoryInterface; use App\Contracts\Repository\SessionRepositoryInterface; use App\Contracts\Repository\SubuserRepositoryInterface; use App\Contracts\Repository\DatabaseRepositoryInterface; @@ -30,7 +28,6 @@ class RepositoryServiceProvider extends ServiceProvider public function register(): void { // Eloquent Repositories - $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 f1a1e7654..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 1c88116bc..6daddf9b4 100644 --- a/app/Services/Api/KeyCreationService.php +++ b/app/Services/Api/KeyCreationService.php @@ -4,7 +4,6 @@ namespace App\Services\Api; use App\Models\ApiKey; use Illuminate\Contracts\Encryption\Encrypter; -use App\Contracts\Repository\ApiKeyRepositoryInterface; class KeyCreationService { @@ -13,12 +12,12 @@ class KeyCreationService /** * ApiKeyService constructor. */ - public function __construct(private ApiKeyRepositoryInterface $repository, private Encrypter $encrypter) + public function __construct(private Encrypter $encrypter) { } /** - * Set the type of key that should be created. By default an orphaned key will be + * Set the type of key that should be created. By default, an orphaned key will be * created. These keys cannot be used for anything, and will not render in the UI. */ public function setKeyType(int $type): self @@ -47,6 +46,6 @@ class KeyCreationService $data = array_merge($data, $permissions); } - return $this->repository->create($data, true, true); + return ApiKey::query()->forceCreate($data); } }