Skip to content

Commit 0999ec9

Browse files
committed
More logic for deleting databases
1 parent 9be2aa4 commit 0999ec9

File tree

5 files changed

+68
-35
lines changed

5 files changed

+68
-35
lines changed

app/Http/Middleware/Api/Client/AuthenticateClientAccess.php

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Middleware\Api\Client\Server;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Pterodactyl\Models\Server;
8+
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
9+
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
10+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
12+
13+
class AuthenticateServerAccess
14+
{
15+
/**
16+
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
17+
*/
18+
private $repository;
19+
20+
/**
21+
* AuthenticateServerAccess constructor.
22+
*
23+
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
24+
*/
25+
public function __construct(ServerRepositoryInterface $repository)
26+
{
27+
$this->repository = $repository;
28+
}
29+
30+
/**
31+
* Authenticate that this server exists and is not suspended or marked as installing.
32+
*
33+
* @param \Illuminate\Http\Request $request
34+
* @param \Closure $next
35+
* @return mixed
36+
*/
37+
public function handle(Request $request, Closure $next)
38+
{
39+
$server = $request->route()->parameter('server');
40+
41+
if (! $server instanceof Server) {
42+
throw new NotFoundHttpException;
43+
}
44+
45+
if ($server->suspended) {
46+
throw new AccessDeniedHttpException('Cannot access a server that is marked as being suspended.');
47+
}
48+
49+
if (! $server->isInstalled()) {
50+
throw new ConflictHttpException('Server has not completed the installation process.');
51+
}
52+
53+
$request->attributes->set('server', $server);
54+
55+
return $next($request);
56+
}
57+
}

app/Http/Requests/Api/Client/Servers/Databases/DeleteDatabaseRequest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ public function permission(): string
1818
}
1919

2020
/**
21-
* Determine if the provided database even belongs to this server instance.
22-
*
2321
* @return bool
2422
*/
2523
public function resourceExists(): bool
2624
{
27-
$server = $this->getModel(Server::class);
28-
$database = $this->getModel(Database::class);
29-
30-
return $database->server_id === $server->id;
25+
return $this->getModel(Server::class)->id === $this->getModel(Database::class)->server_id;
3126
}
3227
}

app/Models/Server.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ public function getTableColumns()
143143
return Schema::getColumnListing($this->getTable());
144144
}
145145

146+
/**
147+
* @return bool
148+
*/
149+
public function isInstalled(): bool
150+
{
151+
return $this->installed === 1;
152+
}
153+
146154
/**
147155
* Gets the user who owns the server.
148156
*

routes/api-client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Pterodactyl\Http\Middleware\Api\Client\AuthenticateClientAccess;
3+
use Pterodactyl\Http\Middleware\Api\Client\Server\AuthenticateServerAccess;
44

55
/*
66
|--------------------------------------------------------------------------
@@ -27,7 +27,7 @@
2727
| Endpoint: /api/client/servers/{server}
2828
|
2929
*/
30-
Route::group(['prefix' => '/servers/{server}', 'middleware' => [AuthenticateClientAccess::class]], function () {
30+
Route::group(['prefix' => '/servers/{server}', 'middleware' => [AuthenticateServerAccess::class]], function () {
3131
Route::get('/', 'Servers\ServerController@index')->name('api.client.servers.view');
3232
Route::get('/utilization', 'Servers\ResourceUtilizationController@index')
3333
->name('api.client.servers.resources');

0 commit comments

Comments
 (0)