Skip to content

Commit be101e2

Browse files
Made user nullable
1 parent c355df1 commit be101e2

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

app/Contracts/Repository/ApiKeyRepositoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function getAccountKeys(User $user): Collection;
1515
/**
1616
* Get all the application API keys that exist.
1717
*/
18-
public function getApplicationKeys(): Collection;
18+
public function getApplicationKeys(?User $user = null): Collection;
1919

2020
/**
2121
* Delete an account API key from the panel for a specific user.
@@ -25,5 +25,5 @@ public function deleteAccountKey(User $user, string $identifier): int;
2525
/**
2626
* Delete an application API key from the panel.
2727
*/
28-
public function deleteApplicationKey(string $identifier): int;
28+
public function deleteApplicationKey(?User $user = null, string $identifier): int;
2929
}

app/Http/Controllers/Admin/ApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function store(StoreApplicationApiKeyRequest $request): RedirectResponse
7878
*/
7979
public function delete(Request $request, string $identifier): Response
8080
{
81-
$this->repository->deleteApplicationKey($identifier);
81+
$this->repository->deleteApplicationKey(null, $identifier);
8282

8383
return response('', 204);
8484
}

app/Http/Controllers/Admin/NodeAutoDeployController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(
3232
public function __invoke(Request $request, Node $node): JsonResponse
3333
{
3434
/** @var ApiKey|null $key */
35-
$key = $this->repository->getApplicationKeys()
35+
$key = $this->repository->getApplicationKeys($request->user())
3636
->filter(function (ApiKey $key) {
3737
if ($key->user->id != $request->user()->id) return false;
3838
foreach ($key->getAttributes() as $permission => $value) {

app/Repositories/Eloquent/ApiKeyRepository.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,33 @@ public function getAccountKeys(User $user): Collection
2828
}
2929

3030
/**
31-
* Get all the application API keys that exist.
31+
* Get all the application API keys. If a user is provided, filter by that user.
3232
*/
33-
public function getApplicationKeys(): Collection
33+
public function getApplicationKeys(?User $user = null): Collection
3434
{
35-
return $this->getBuilder()
35+
$instance = $this->getBuilder()->where('key_type', ApiKey::TYPE_APPLICATION);
36+
37+
if ($user) {
38+
$instance->where('user_id', $user->id);
39+
}
40+
41+
return $instance->get($this->getColumns());
42+
}
43+
44+
/**
45+
* Delete an application API key. If a user is provided, ensure it belongs to them.
46+
*/
47+
public function deleteApplicationKey(?User $user = null, string $identifier): int
48+
{
49+
$query = $this->getBuilder()
3650
->where('key_type', ApiKey::TYPE_APPLICATION)
37-
->with('user')
38-
->get($this->getColumns());
51+
->where('identifier', $identifier);
52+
53+
if ($user) {
54+
$query->where('user_id', $user->id);
55+
}
56+
57+
return $query->delete();
3958
}
4059

4160
/**

0 commit comments

Comments
 (0)