Skip to content

Commit 6ef62c7

Browse files
committed
Merge branch 'feature/api-v1' into develop
2 parents 655d248 + c599112 commit 6ef62c7

File tree

207 files changed

+7078
-3225
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+7078
-3225
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Pterodactyl\Console\Commands\Migration;
4+
5+
use Pterodactyl\Models\ApiKey;
6+
use Illuminate\Console\Command;
7+
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
8+
9+
class CleanOrphanedApiKeysCommand extends Command
10+
{
11+
/**
12+
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
13+
*/
14+
private $repository;
15+
16+
/**
17+
* @var string
18+
*/
19+
protected $signature = 'p:migration:clean-orphaned-keys';
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $description = 'Cleans API keys from the database that are not assigned a specific role.';
25+
26+
/**
27+
* CleanOrphanedApiKeysCommand constructor.
28+
*
29+
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
30+
*/
31+
public function __construct(ApiKeyRepositoryInterface $repository)
32+
{
33+
parent::__construct();
34+
35+
$this->repository = $repository;
36+
}
37+
38+
/**
39+
* Delete all orphaned API keys from the database when upgrading from 0.6 to 0.7.
40+
*
41+
* @return null|void
42+
*/
43+
public function handle()
44+
{
45+
$count = $this->repository->findCountWhere([['key_type', '=', ApiKey::TYPE_NONE]]);
46+
$continue = $this->confirm(
47+
'This action will remove ' . $count . ' keys from the database. Are you sure you wish to continue?', false
48+
);
49+
50+
if (! $continue) {
51+
return null;
52+
}
53+
54+
$this->info('Deleting keys...');
55+
$this->repository->deleteWhere([['key_type', '=', ApiKey::TYPE_NONE]]);
56+
$this->info('Keys were successfully deleted.');
57+
}
58+
}

app/Contracts/Repository/AllocationRepositoryInterface.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Contracts\Repository;
44

55
use Illuminate\Support\Collection;
6+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
67

78
interface AllocationRepositoryInterface extends RepositoryInterface
89
{
@@ -23,11 +24,60 @@ public function assignAllocationsToServer(int $server = null, array $ids): int;
2324
*/
2425
public function getAllocationsForNode(int $node): Collection;
2526

27+
/**
28+
* Return all of the allocations for a node in a paginated format.
29+
*
30+
* @param int $node
31+
* @param int $perPage
32+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
33+
*/
34+
public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator;
35+
2636
/**
2737
* Return all of the unique IPs that exist for a given node.
2838
*
2939
* @param int $node
3040
* @return \Illuminate\Support\Collection
3141
*/
3242
public function getUniqueAllocationIpsForNode(int $node): Collection;
43+
44+
/**
45+
* Return all of the allocations that exist for a node that are not currently
46+
* allocated.
47+
*
48+
* @param int $node
49+
* @return array
50+
*/
51+
public function getUnassignedAllocationIds(int $node): array;
52+
53+
/**
54+
* Get an array of all allocations that are currently assigned to a given server.
55+
*
56+
* @param int $server
57+
* @return array
58+
*/
59+
public function getAssignedAllocationIds(int $server): array;
60+
61+
/**
62+
* Return a concated result set of node ips that already have at least one
63+
* server assigned to that IP. This allows for filtering out sets for
64+
* dedicated allocation IPs.
65+
*
66+
* If an array of nodes is passed the results will be limited to allocations
67+
* in those nodes.
68+
*
69+
* @param array $nodes
70+
* @return array
71+
*/
72+
public function getDiscardableDedicatedAllocations(array $nodes = []): array;
73+
74+
/**
75+
* Return a single allocation from those meeting the requirements.
76+
*
77+
* @param array $nodes
78+
* @param array $ports
79+
* @param bool $dedicated
80+
* @return \Pterodactyl\Models\Allocation|null
81+
*/
82+
public function getRandomAllocation(array $nodes, array $ports, bool $dedicated = false);
3383
}
Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Contracts\Repository;
114

12-
use Pterodactyl\Models\APIKey;
5+
use Pterodactyl\Models\User;
6+
use Illuminate\Support\Collection;
137

148
interface ApiKeyRepositoryInterface extends RepositoryInterface
159
{
1610
/**
17-
* Load permissions for a key onto the model.
11+
* Get all of the account API keys that exist for a specific user.
1812
*
19-
* @param \Pterodactyl\Models\APIKey $model
20-
* @param bool $refresh
21-
* @return \Pterodactyl\Models\APIKey
13+
* @param \Pterodactyl\Models\User $user
14+
* @return \Illuminate\Support\Collection
2215
*/
23-
public function loadPermissions(APIKey $model, bool $refresh = false): APIKey;
16+
public function getAccountKeys(User $user): Collection;
17+
18+
/**
19+
* Get all of the application API keys that exist for a specific user.
20+
*
21+
* @param \Pterodactyl\Models\User $user
22+
* @return \Illuminate\Support\Collection
23+
*/
24+
public function getApplicationKeys(User $user): Collection;
25+
26+
/**
27+
* Delete an account API key from the panel for a specific user.
28+
*
29+
* @param \Pterodactyl\Models\User $user
30+
* @param string $identifier
31+
* @return int
32+
*/
33+
public function deleteAccountKey(User $user, string $identifier): int;
34+
35+
/**
36+
* Delete an application API key from the panel for a specific user.
37+
*
38+
* @param \Pterodactyl\Models\User $user
39+
* @param string $identifier
40+
* @return int
41+
*/
42+
public function deleteApplicationKey(User $user, string $identifier): int;
2443
}

app/Contracts/Repository/NodeRepositoryInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Pterodactyl\Contracts\Repository;
44

5+
use Generator;
56
use Pterodactyl\Models\Node;
67
use Illuminate\Support\Collection;
78
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
@@ -62,4 +63,15 @@ public function getNodeServers(int $id): Node;
6263
* @return \Illuminate\Support\Collection
6364
*/
6465
public function getNodesForServerCreation(): Collection;
66+
67+
/**
68+
* Return the IDs of all nodes that exist in the provided locations and have the space
69+
* available to support the additional disk and memory provided.
70+
*
71+
* @param array $locations
72+
* @param int $disk
73+
* @param int $memory
74+
* @return \Generator
75+
*/
76+
public function getNodesWithResourceUse(array $locations, int $disk, int $memory): Generator;
6577
}

app/Contracts/Repository/RepositoryInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Contracts\Repository;
44

55
use Illuminate\Support\Collection;
6+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
67

78
interface RepositoryInterface
89
{
@@ -175,6 +176,14 @@ public function updateOrCreate(array $where, array $fields, bool $validate = tru
175176
*/
176177
public function all(): Collection;
177178

179+
/**
180+
* Return a paginated result set using a search term if set on the repository.
181+
*
182+
* @param int $perPage
183+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
184+
*/
185+
public function paginated(int $perPage): LengthAwarePaginator;
186+
178187
/**
179188
* Insert a single or multiple records into the database at once skipping
180189
* validation and mass assignment checking.

app/Exceptions/DisplayException.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
<?php
2-
/**
3-
* Pterodactyl - Panel
4-
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5-
*
6-
* This software is licensed under the terms of the MIT license.
7-
* https://opensource.org/licenses/MIT
8-
*/
92

103
namespace Pterodactyl\Exceptions;
114

125
use Log;
136
use Throwable;
7+
use Illuminate\Http\Response;
8+
use Prologue\Alerts\AlertsMessageBag;
149

1510
class DisplayException extends PterodactylException
1611
{
12+
const LEVEL_DEBUG = 'debug';
13+
const LEVEL_INFO = 'info';
1714
const LEVEL_WARNING = 'warning';
1815
const LEVEL_ERROR = 'error';
1916

@@ -32,13 +29,13 @@ class DisplayException extends PterodactylException
3229
*/
3330
public function __construct($message, Throwable $previous = null, $level = self::LEVEL_ERROR, $code = 0)
3431
{
35-
$this->level = $level;
32+
parent::__construct($message, $code, $previous);
3633

3734
if (! is_null($previous)) {
3835
Log::{$level}($previous);
3936
}
4037

41-
parent::__construct($message, $code, $previous);
38+
$this->level = $level;
4239
}
4340

4441
/**
@@ -48,4 +45,33 @@ public function getErrorLevel()
4845
{
4946
return $this->level;
5047
}
48+
49+
/**
50+
* @return int
51+
*/
52+
public function getStatusCode()
53+
{
54+
return Response::HTTP_BAD_REQUEST;
55+
}
56+
57+
/**
58+
* Render the exception to the user by adding a flashed message to the session
59+
* and then redirecting them back to the page that they came from. If the
60+
* request originated from an API hit, return the error in JSONAPI spec format.
61+
*
62+
* @param \Illuminate\Http\Request $request
63+
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
64+
*/
65+
public function render($request)
66+
{
67+
if ($request->expectsJson()) {
68+
return response()->json(Handler::convertToArray($this, [
69+
'detail' => $this->getMessage(),
70+
]), method_exists($this, 'getStatusCode') ? $this->getStatusCode() : Response::HTTP_BAD_REQUEST);
71+
}
72+
73+
app()->make(AlertsMessageBag::class)->danger($this->getMessage())->flash();
74+
75+
return redirect()->back()->withInput();
76+
}
5177
}

app/Exceptions/DisplayValidationException.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)