Remove api key repository

This commit is contained in:
Lance Pioch 2024-03-16 20:00:31 -04:00
parent ea417cf45e
commit 89477421e1
6 changed files with 18 additions and 105 deletions

View File

@ -1,29 +0,0 @@
<?php
namespace App\Contracts\Repository;
use App\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

@ -12,7 +12,6 @@ use App\Services\Acl\Api\AdminAcl;
use Illuminate\View\Factory as ViewFactory; use Illuminate\View\Factory as ViewFactory;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Services\Api\KeyCreationService; use App\Services\Api\KeyCreationService;
use App\Contracts\Repository\ApiKeyRepositoryInterface;
use App\Http\Requests\Admin\Api\StoreApplicationApiKeyRequest; use App\Http\Requests\Admin\Api\StoreApplicationApiKeyRequest;
class ApiController extends Controller class ApiController extends Controller
@ -22,7 +21,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 +31,12 @@ class ApiController extends Controller
*/ */
public function index(Request $request): View public function index(Request $request): View
{ {
$keys = $request->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 +82,10 @@ 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); $request->user()->apiKeys()
->where('key_type', ApiKey::TYPE_APPLICATION)
->where('identifier', $identifier)
->delete();
return response('', 204); return response('', 204);
} }

View File

@ -9,7 +9,6 @@ use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use App\Services\Api\KeyCreationService; use App\Services\Api\KeyCreationService;
use App\Repositories\Eloquent\ApiKeyRepository;
class NodeAutoDeployController extends Controller class NodeAutoDeployController extends Controller
{ {
@ -17,7 +16,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 +29,12 @@ class NodeAutoDeployController extends Controller
*/ */
public function __invoke(Request $request, Node $node): JsonResponse public function __invoke(Request $request, Node $node): JsonResponse
{ {
/** @var \App\Models\ApiKey|null $key */ $keys = $request->user()->apiKeys()
$key = $this->repository->getApplicationKeys($request->user()) ->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

@ -5,7 +5,6 @@ namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use App\Repositories\Eloquent\EggRepository; use App\Repositories\Eloquent\EggRepository;
use App\Repositories\Eloquent\NodeRepository; use App\Repositories\Eloquent\NodeRepository;
use App\Repositories\Eloquent\ApiKeyRepository;
use App\Repositories\Eloquent\SessionRepository; use App\Repositories\Eloquent\SessionRepository;
use App\Repositories\Eloquent\SubuserRepository; use App\Repositories\Eloquent\SubuserRepository;
use App\Repositories\Eloquent\DatabaseRepository; use App\Repositories\Eloquent\DatabaseRepository;
@ -14,7 +13,6 @@ use App\Contracts\Repository\EggRepositoryInterface;
use App\Repositories\Eloquent\EggVariableRepository; use App\Repositories\Eloquent\EggVariableRepository;
use App\Contracts\Repository\NodeRepositoryInterface; use App\Contracts\Repository\NodeRepositoryInterface;
use App\Repositories\Eloquent\DatabaseHostRepository; use App\Repositories\Eloquent\DatabaseHostRepository;
use App\Contracts\Repository\ApiKeyRepositoryInterface;
use App\Contracts\Repository\SessionRepositoryInterface; use App\Contracts\Repository\SessionRepositoryInterface;
use App\Contracts\Repository\SubuserRepositoryInterface; use App\Contracts\Repository\SubuserRepositoryInterface;
use App\Contracts\Repository\DatabaseRepositoryInterface; use App\Contracts\Repository\DatabaseRepositoryInterface;
@ -30,7 +28,6 @@ class RepositoryServiceProvider extends ServiceProvider
public function register(): void public function register(): void
{ {
// Eloquent Repositories // Eloquent Repositories
$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 App\Repositories\Eloquent;
use App\Models\User;
use App\Models\ApiKey;
use Illuminate\Support\Collection;
use App\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 App\Services\Api;
use App\Models\ApiKey; use App\Models\ApiKey;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use App\Contracts\Repository\ApiKeyRepositoryInterface;
class KeyCreationService class KeyCreationService
{ {
@ -13,12 +12,12 @@ class KeyCreationService
/** /**
* ApiKeyService constructor. * 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. * created. These keys cannot be used for anything, and will not render in the UI.
*/ */
public function setKeyType(int $type): self public function setKeyType(int $type): self
@ -47,6 +46,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);
} }
} }