diff --git a/CHANGELOG.md b/CHANGELOG.md index 31c1cef28..6d9126443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,20 @@ This file is a running track of new features and fixes to each version of the pa This project follows [Semantic Versioning](http://semver.org) guidelines. +## v0.7.5 (Derelict Dermodactylus) +### Fixed +* Fixes application API keys being created as a client API key. +* Search term is now passed through when using paginated result sets. +* Reduces the number of SQL queries executed when rendering the server listing to increase performance. +* Fixes exceptions being thrown for non-existent subuser permissions. +* Fixes exception caused when trying to revoke admin privileges from a user account due to a bad endpoint. + +### Changed +* Databases are now properly paginated when viewing a database host. +* No more loading daemon keys for every server model being loaded, some of us value our databases. +* Changed behavior of the subuser middleware to add a daemon access key if one is missing from the database for some reason. +* Server short-codes are now based on the UUID as they were in previous versions of Pterodactyl. + ## v0.7.4-h1 (Derelict Dermodactylus) ### Fixed * Being able to create servers is kind of a core aspect of the software, pushing releases late at night is not a great idea. diff --git a/README.md b/README.md index f6931c048..135f4d4ef 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,9 @@ Some of our core supported games include: * Teamspeak * Mumble * Team Fortress 2 -* Counter Strike: GO -* Garrys Mod -* Ark: Survival Evolved +* Counter Strike: Global Offensive +* Garry's Mod +* ARK: Survival Evolved In addition to our standard nest of supported games, our community is constantly pushing the limits of this software and there are plenty more games available provided by the community. Some of these games include: diff --git a/app/Contracts/Repository/DatabaseHostRepositoryInterface.php b/app/Contracts/Repository/DatabaseHostRepositoryInterface.php index dfd29e9ab..a924d85a9 100644 --- a/app/Contracts/Repository/DatabaseHostRepositoryInterface.php +++ b/app/Contracts/Repository/DatabaseHostRepositoryInterface.php @@ -3,7 +3,6 @@ namespace Pterodactyl\Contracts\Repository; use Illuminate\Support\Collection; -use Pterodactyl\Models\DatabaseHost; interface DatabaseHostRepositoryInterface extends RepositoryInterface { @@ -14,15 +13,4 @@ interface DatabaseHostRepositoryInterface extends RepositoryInterface * @return \Illuminate\Support\Collection */ public function getWithViewDetails(): Collection; - - /** - * Return a database host with the databases and associated servers - * that are attached to said databases. - * - * @param int $id - * @return \Pterodactyl\Models\DatabaseHost - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function getWithServers(int $id): DatabaseHost; } diff --git a/app/Contracts/Repository/DatabaseRepositoryInterface.php b/app/Contracts/Repository/DatabaseRepositoryInterface.php index 7fc3bf045..f5d531513 100644 --- a/app/Contracts/Repository/DatabaseRepositoryInterface.php +++ b/app/Contracts/Repository/DatabaseRepositoryInterface.php @@ -1,16 +1,10 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Contracts\Repository; use Pterodactyl\Models\Database; use Illuminate\Support\Collection; +use Illuminate\Contracts\Pagination\LengthAwarePaginator; interface DatabaseRepositoryInterface extends RepositoryInterface { @@ -39,6 +33,15 @@ interface DatabaseRepositoryInterface extends RepositoryInterface */ public function getDatabasesForServer(int $server): Collection; + /** + * Return all of the databases for a given host with the server relationship loaded. + * + * @param int $host + * @param int $count + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator + */ + public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePaginator; + /** * Create a new database if it does not already exist on the host with * the provided details. diff --git a/app/Contracts/Repository/ServerRepositoryInterface.php b/app/Contracts/Repository/ServerRepositoryInterface.php index 983cf7e6e..dc677fba0 100644 --- a/app/Contracts/Repository/ServerRepositoryInterface.php +++ b/app/Contracts/Repository/ServerRepositoryInterface.php @@ -136,4 +136,13 @@ interface ServerRepositoryInterface extends RepositoryInterface, SearchableInter * @return int */ public function getServersForPowerActionCount(array $servers = [], array $nodes = []): int; + + /** + * Check if a given UUID and UUID-Short string are unique to a server. + * + * @param string $uuid + * @param string $short + * @return bool + */ + public function isUniqueUuidCombo(string $uuid, string $short): bool; } diff --git a/app/Http/Controllers/Admin/DatabaseController.php b/app/Http/Controllers/Admin/DatabaseController.php index d03f7050e..affb0c80e 100644 --- a/app/Http/Controllers/Admin/DatabaseController.php +++ b/app/Http/Controllers/Admin/DatabaseController.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Http\Controllers\Admin; @@ -19,6 +12,7 @@ use Pterodactyl\Services\Databases\Hosts\HostUpdateService; use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest; use Pterodactyl\Services\Databases\Hosts\HostCreationService; use Pterodactyl\Services\Databases\Hosts\HostDeletionService; +use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; @@ -34,6 +28,11 @@ class DatabaseController extends Controller */ private $creationService; + /** + * @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface + */ + private $databaseRepository; + /** * @var \Pterodactyl\Services\Databases\Hosts\HostDeletionService */ @@ -59,6 +58,7 @@ class DatabaseController extends Controller * * @param \Prologue\Alerts\AlertsMessageBag $alert * @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository + * @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository * @param \Pterodactyl\Services\Databases\Hosts\HostCreationService $creationService * @param \Pterodactyl\Services\Databases\Hosts\HostDeletionService $deletionService * @param \Pterodactyl\Services\Databases\Hosts\HostUpdateService $updateService @@ -67,6 +67,7 @@ class DatabaseController extends Controller public function __construct( AlertsMessageBag $alert, DatabaseHostRepositoryInterface $repository, + DatabaseRepositoryInterface $databaseRepository, HostCreationService $creationService, HostDeletionService $deletionService, HostUpdateService $updateService, @@ -74,6 +75,7 @@ class DatabaseController extends Controller ) { $this->alert = $alert; $this->creationService = $creationService; + $this->databaseRepository = $databaseRepository; $this->deletionService = $deletionService; $this->repository = $repository; $this->locationRepository = $locationRepository; @@ -105,7 +107,8 @@ class DatabaseController extends Controller { return view('admin.databases.view', [ 'locations' => $this->locationRepository->getAllWithNodes(), - 'host' => $this->repository->getWithServers($host), + 'host' => $this->repository->find($host), + 'databases' => $this->databaseRepository->getDatabasesForHost($host), ]); } diff --git a/app/Http/Middleware/Server/AuthenticateAsSubuser.php b/app/Http/Middleware/Server/AuthenticateAsSubuser.php index 8f8a158ca..1a185bb6e 100644 --- a/app/Http/Middleware/Server/AuthenticateAsSubuser.php +++ b/app/Http/Middleware/Server/AuthenticateAsSubuser.php @@ -11,7 +11,6 @@ namespace Pterodactyl\Http\Middleware\Server; use Closure; use Illuminate\Http\Request; -use Illuminate\Contracts\Session\Session; use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService; use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; @@ -23,21 +22,14 @@ class AuthenticateAsSubuser */ private $keyProviderService; - /** - * @var \Illuminate\Contracts\Session\Session - */ - private $session; - /** * SubuserAccessAuthenticate constructor. * * @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService - * @param \Illuminate\Contracts\Session\Session $session */ - public function __construct(DaemonKeyProviderService $keyProviderService, Session $session) + public function __construct(DaemonKeyProviderService $keyProviderService) { $this->keyProviderService = $keyProviderService; - $this->session = $session; } /** @@ -60,7 +52,6 @@ class AuthenticateAsSubuser throw new AccessDeniedHttpException('This account does not have permission to access this server.'); } - $this->session->now('server_data.token', $token); $request->attributes->set('server_token', $token); return $next($request); diff --git a/app/Models/Server.php b/app/Models/Server.php index d5c6b3a8d..5d42cf86b 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -35,13 +35,6 @@ class Server extends Model implements CleansAttributes, ValidableContract */ protected $dates = [self::CREATED_AT, self::UPDATED_AT, 'deleted_at']; - /** - * Always eager load these relationships on the model. - * - * @var array - */ - protected $with = ['key']; - /** * Fields that are not mass assignable. * diff --git a/app/Repositories/Daemon/ServerRepository.php b/app/Repositories/Daemon/ServerRepository.php index 3af381d1c..cf5a5a69a 100644 --- a/app/Repositories/Daemon/ServerRepository.php +++ b/app/Repositories/Daemon/ServerRepository.php @@ -115,8 +115,8 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa public function revokeAccessKey($key): ResponseInterface { if (is_array($key)) { - return $this->getHttpClient()->request('POST', 'keys', [ - 'json' => $key, + return $this->getHttpClient()->request('POST', 'keys/batch-delete', [ + 'json' => ['keys' => $key], ]); } diff --git a/app/Repositories/Eloquent/DatabaseHostRepository.php b/app/Repositories/Eloquent/DatabaseHostRepository.php index 6bfb94d70..4ed4d411e 100644 --- a/app/Repositories/Eloquent/DatabaseHostRepository.php +++ b/app/Repositories/Eloquent/DatabaseHostRepository.php @@ -4,8 +4,6 @@ namespace Pterodactyl\Repositories\Eloquent; use Illuminate\Support\Collection; use Pterodactyl\Models\DatabaseHost; -use Illuminate\Database\Eloquent\ModelNotFoundException; -use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; class DatabaseHostRepository extends EloquentRepository implements DatabaseHostRepositoryInterface @@ -30,22 +28,4 @@ class DatabaseHostRepository extends EloquentRepository implements DatabaseHostR { return $this->getBuilder()->withCount('databases')->with('node')->get(); } - - /** - * Return a database host with the databases and associated servers - * that are attached to said databases. - * - * @param int $id - * @return \Pterodactyl\Models\DatabaseHost - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function getWithServers(int $id): DatabaseHost - { - try { - return $this->getBuilder()->with('databases.server')->findOrFail($id, $this->getColumns()); - } catch (ModelNotFoundException $exception) { - throw new RecordNotFoundException; - } - } } diff --git a/app/Repositories/Eloquent/DatabaseRepository.php b/app/Repositories/Eloquent/DatabaseRepository.php index d24dd177e..8a8c93fd8 100644 --- a/app/Repositories/Eloquent/DatabaseRepository.php +++ b/app/Repositories/Eloquent/DatabaseRepository.php @@ -6,6 +6,7 @@ use Pterodactyl\Models\Database; use Illuminate\Support\Collection; use Illuminate\Foundation\Application; use Illuminate\Database\DatabaseManager; +use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException; @@ -78,6 +79,20 @@ class DatabaseRepository extends EloquentRepository implements DatabaseRepositor return $this->getBuilder()->where('server_id', $server)->get($this->getColumns()); } + /** + * Return all of the databases for a given host with the server relationship loaded. + * + * @param int $host + * @param int $count + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator + */ + public function getDatabasesForHost(int $host, int $count = 25): LengthAwarePaginator + { + return $this->getBuilder()->with('server') + ->where('database_host_id', $host) + ->paginate($count, $this->getColumns()); + } + /** * Create a new database if it does not already exist on the host with * the provided details. diff --git a/app/Repositories/Eloquent/ServerRepository.php b/app/Repositories/Eloquent/ServerRepository.php index 5a53d33f0..2fc5f878b 100644 --- a/app/Repositories/Eloquent/ServerRepository.php +++ b/app/Repositories/Eloquent/ServerRepository.php @@ -216,7 +216,7 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt */ public function filterUserAccessServers(User $user, int $level, bool $paginate = true) { - $instance = $this->getBuilder()->select($this->getColumns())->with(['user']); + $instance = $this->getBuilder()->select($this->getColumns())->with(['user', 'node', 'allocation']); // If access level is set to owner, only display servers // that the user owns. @@ -303,6 +303,18 @@ class ServerRepository extends EloquentRepository implements ServerRepositoryInt return $this->getServersForPowerAction($servers, $nodes, true); } + /** + * Check if a given UUID and UUID-Short string are unique to a server. + * + * @param string $uuid + * @param string $short + * @return bool + */ + public function isUniqueUuidCombo(string $uuid, string $short): bool + { + return ! $this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuidShort', '=', $short)->exists(); + } + /** * Return an array of server IDs that a given user can access based * on owner and subuser permissions. diff --git a/app/Services/DaemonKeys/DaemonKeyProviderService.php b/app/Services/DaemonKeys/DaemonKeyProviderService.php index ab19329fa..a386d286e 100644 --- a/app/Services/DaemonKeys/DaemonKeyProviderService.php +++ b/app/Services/DaemonKeys/DaemonKeyProviderService.php @@ -28,6 +28,7 @@ use Carbon\Carbon; use Pterodactyl\Models\User; use Pterodactyl\Models\Server; use Pterodactyl\Exceptions\Repository\RecordNotFoundException; +use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface; class DaemonKeyProviderService @@ -47,21 +48,29 @@ class DaemonKeyProviderService */ private $repository; + /** + * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface + */ + private $subuserRepository; + /** * GetDaemonKeyService constructor. * * @param \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService $keyCreationService * @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $repository * @param \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService $keyUpdateService + * @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $subuserRepository */ public function __construct( DaemonKeyCreationService $keyCreationService, DaemonKeyRepositoryInterface $repository, - DaemonKeyUpdateService $keyUpdateService + DaemonKeyUpdateService $keyUpdateService, + SubuserRepositoryInterface $subuserRepository ) { $this->keyCreationService = $keyCreationService; $this->keyUpdateService = $keyUpdateService; $this->repository = $repository; + $this->subuserRepository = $subuserRepository; } /** @@ -89,10 +98,18 @@ class DaemonKeyProviderService return $this->keyCreationService->handle($server->id, $user->id); } - // If they aren't the admin or owner of the server, they shouldn't get access. - // Subusers should always have an entry created when they are, so if there is - // no record, it should fail. - throw $exception; + // Check if user is a subuser for this server. Ideally they should always have + // a record associated with them in the database, but we should still handle + // that potentiality here. + // + // If no subuser is found, a RecordNotFoundException will be thrown, thus handling + // the parent error as well. + $subuser = $this->subuserRepository->findFirstWhere([ + ['user_id', '=', $user->id], + ['server_id', '=', $server->id], + ]); + + return $this->keyCreationService->handle($subuser->server_id, $subuser->user_id); } if (! $updateIfExpired || Carbon::now()->diffInSeconds($key->expires_at, false) > 0) { diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index 36144241f..4b4c4ef89 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -210,10 +210,12 @@ class ServerCreationService */ private function createModel(array $data): Server { + $uuid = $this->generateUniqueUuidCombo(); + return $this->repository->create([ 'external_id' => array_get($data, 'external_id'), - 'uuid' => Uuid::uuid4()->toString(), - 'uuidShort' => str_random(8), + 'uuid' => $uuid, + 'uuidShort' => substr($uuid, 0, 8), 'node_id' => array_get($data, 'node_id'), 'name' => array_get($data, 'name'), 'description' => array_get($data, 'description') ?? '', @@ -289,4 +291,20 @@ class ServerCreationService return $allocation->node_id; } + + /** + * Create a unique UUID and UUID-Short combo for a server. + * + * @return string + */ + private function generateUniqueUuidCombo(): string + { + $uuid = Uuid::uuid4()->toString(); + + if (! $this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) { + return $this->generateUniqueUuidCombo(); + } + + return $uuid; + } } diff --git a/app/Transformers/Daemon/ApiKeyTransformer.php b/app/Transformers/Daemon/ApiKeyTransformer.php index 54aa37d6f..7f1d7427b 100644 --- a/app/Transformers/Daemon/ApiKeyTransformer.php +++ b/app/Transformers/Daemon/ApiKeyTransformer.php @@ -1,26 +1,4 @@ . - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ namespace Pterodactyl\Transformers\Daemon; @@ -83,8 +61,8 @@ class ApiKeyTransformer extends TransformerAbstract $daemonPermissions = ['s:console']; foreach ($permissions as $permission) { - if (! is_null($mappings[$permission])) { - $daemonPermissions[] = $mappings[$permission]; + if (! is_null(array_get($mappings, $permission))) { + $daemonPermissions[] = array_get($mappings, $permission); } } diff --git a/config/app.php b/config/app.php index 74c1b0266..fe5ed34ca 100644 --- a/config/app.php +++ b/config/app.php @@ -9,7 +9,7 @@ return [ | change this value if you are not maintaining your own internal versions. */ - 'version' => '0.7.4.1', + 'version' => '0.7.5', /* |-------------------------------------------------------------------------- diff --git a/resources/themes/pterodactyl/admin/api/new.blade.php b/resources/themes/pterodactyl/admin/api/new.blade.php index b5db6a154..b5876ee8c 100644 --- a/resources/themes/pterodactyl/admin/api/new.blade.php +++ b/resources/themes/pterodactyl/admin/api/new.blade.php @@ -15,7 +15,7 @@ @section('content')
-
+
diff --git a/resources/themes/pterodactyl/admin/databases/view.blade.php b/resources/themes/pterodactyl/admin/databases/view.blade.php index 41a2d7901..655b833fc 100644 --- a/resources/themes/pterodactyl/admin/databases/view.blade.php +++ b/resources/themes/pterodactyl/admin/databases/view.blade.php @@ -101,14 +101,14 @@ Connections From - @foreach($host->databases as $database) + @foreach($databases as $database) - {{ $database->server->name }} + {{ $database->getRelation('server')->name }} {{ $database->database }} {{ $database->username }} {{ $database->remote }} - + @@ -116,6 +116,11 @@ @endforeach
+ @if($databases->hasPages()) + + @endif
diff --git a/resources/themes/pterodactyl/admin/nodes/index.blade.php b/resources/themes/pterodactyl/admin/nodes/index.blade.php index a96d32240..b4f936fb6 100644 --- a/resources/themes/pterodactyl/admin/nodes/index.blade.php +++ b/resources/themes/pterodactyl/admin/nodes/index.blade.php @@ -70,7 +70,7 @@ @if($nodes->hasPages()) @endif diff --git a/resources/themes/pterodactyl/admin/packs/index.blade.php b/resources/themes/pterodactyl/admin/packs/index.blade.php index 270bea96b..dd74f17ed 100644 --- a/resources/themes/pterodactyl/admin/packs/index.blade.php +++ b/resources/themes/pterodactyl/admin/packs/index.blade.php @@ -61,7 +61,7 @@ @if ($packs->hasPages()) @endif diff --git a/resources/themes/pterodactyl/admin/servers/index.blade.php b/resources/themes/pterodactyl/admin/servers/index.blade.php index 24fe4d81f..117a4de70 100644 --- a/resources/themes/pterodactyl/admin/servers/index.blade.php +++ b/resources/themes/pterodactyl/admin/servers/index.blade.php @@ -76,7 +76,7 @@ @if($servers->hasPages()) @endif diff --git a/resources/themes/pterodactyl/admin/users/index.blade.php b/resources/themes/pterodactyl/admin/users/index.blade.php index dd95f222b..2de490d53 100644 --- a/resources/themes/pterodactyl/admin/users/index.blade.php +++ b/resources/themes/pterodactyl/admin/users/index.blade.php @@ -75,7 +75,7 @@ @if($users->hasPages()) @endif diff --git a/resources/themes/pterodactyl/base/index.blade.php b/resources/themes/pterodactyl/base/index.blade.php index 3b5cd62f7..95cce6128 100644 --- a/resources/themes/pterodactyl/base/index.blade.php +++ b/resources/themes/pterodactyl/base/index.blade.php @@ -51,8 +51,8 @@ description)) rowspan="2" @endif>{{ $server->uuidShort }} {{ $server->name }} - {{ $server->node->name }} - {{ $server->allocation->alias }}:{{ $server->allocation->port }} + {{ $server->getRelation('node')->name }} + {{ $server->getRelation('allocation')->alias }}:{{ $server->getRelation('allocation')->port }} -- / {{ $server->memory === 0 ? '∞' : $server->memory }} MB -- % @@ -79,7 +79,7 @@ @if($servers->hasPages()) @endif diff --git a/tests/Unit/Http/Controllers/Admin/DatabaseControllerTest.php b/tests/Unit/Http/Controllers/Admin/DatabaseControllerTest.php index 108198bde..c3735b927 100644 --- a/tests/Unit/Http/Controllers/Admin/DatabaseControllerTest.php +++ b/tests/Unit/Http/Controllers/Admin/DatabaseControllerTest.php @@ -13,11 +13,13 @@ use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\DatabaseHost; use Prologue\Alerts\AlertsMessageBag; +use Illuminate\Pagination\LengthAwarePaginator; use Tests\Assertions\ControllerAssertionsTrait; use Pterodactyl\Http\Controllers\Admin\DatabaseController; use Pterodactyl\Services\Databases\Hosts\HostUpdateService; use Pterodactyl\Services\Databases\Hosts\HostCreationService; use Pterodactyl\Services\Databases\Hosts\HostDeletionService; +use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; @@ -35,6 +37,11 @@ class DatabaseControllerTest extends TestCase */ private $creationService; + /** + * @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock + */ + private $databaseRepository; + /** * @var \Pterodactyl\Services\Databases\Hosts\HostDeletionService|\Mockery\Mock */ @@ -64,6 +71,7 @@ class DatabaseControllerTest extends TestCase $this->alert = m::mock(AlertsMessageBag::class); $this->creationService = m::mock(HostCreationService::class); + $this->databaseRepository = m::mock(DatabaseRepositoryInterface::class); $this->deletionService = m::mock(HostDeletionService::class); $this->locationRepository = m::mock(LocationRepositoryInterface::class); $this->repository = m::mock(DatabaseHostRepositoryInterface::class); @@ -94,9 +102,14 @@ class DatabaseControllerTest extends TestCase public function testViewController() { $model = factory(DatabaseHost::class)->make(); + $paginator = new LengthAwarePaginator([], 1, 1); $this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn(collect(['getAllWithNodes'])); - $this->repository->shouldReceive('getWithServers')->with(1)->once()->andReturn($model); + $this->repository->shouldReceive('find')->with(1)->once()->andReturn($model); + $this->databaseRepository->shouldReceive('getDatabasesForHost') + ->once() + ->with(1) + ->andReturn($paginator); $response = $this->getController()->view(1); @@ -104,8 +117,10 @@ class DatabaseControllerTest extends TestCase $this->assertViewNameEquals('admin.databases.view', $response); $this->assertViewHasKey('locations', $response); $this->assertViewHasKey('host', $response); + $this->assertViewHasKey('databases', $response); $this->assertViewKeyEquals('locations', collect(['getAllWithNodes']), $response); $this->assertViewKeyEquals('host', $model, $response); + $this->assertViewKeyEquals('databases', $paginator, $response); } /** @@ -118,6 +133,7 @@ class DatabaseControllerTest extends TestCase return new DatabaseController( $this->alert, $this->repository, + $this->databaseRepository, $this->creationService, $this->deletionService, $this->updateService, diff --git a/tests/Unit/Http/Middleware/Server/AuthenticateAsSubuserTest.php b/tests/Unit/Http/Middleware/Server/AuthenticateAsSubuserTest.php index 8ee1a0e4c..04965facc 100644 --- a/tests/Unit/Http/Middleware/Server/AuthenticateAsSubuserTest.php +++ b/tests/Unit/Http/Middleware/Server/AuthenticateAsSubuserTest.php @@ -4,7 +4,6 @@ namespace Tests\Unit\Http\Middleware\Server; use Mockery as m; use Pterodactyl\Models\Server; -use Illuminate\Contracts\Session\Session; use Tests\Unit\Http\Middleware\MiddlewareTestCase; use Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser; use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService; @@ -17,11 +16,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase */ private $keyProviderService; - /** - * @var \Illuminate\Contracts\Session\Session|\Mockery\Mock - */ - private $session; - /** * Setup tests. */ @@ -30,7 +24,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase parent::setUp(); $this->keyProviderService = m::mock(DaemonKeyProviderService::class); - $this->session = m::mock(Session::class); } /** @@ -43,7 +36,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase $this->setRequestAttribute('server', $model); $this->keyProviderService->shouldReceive('handle')->with($model, $user)->once()->andReturn('abc123'); - $this->session->shouldReceive('now')->with('server_data.token', 'abc123')->once()->andReturnNull(); $this->getMiddleware()->handle($this->request, $this->getClosureAssertions()); $this->assertRequestHasAttribute('server_token'); @@ -74,6 +66,6 @@ class AuthenticateAsSubuserTest extends MiddlewareTestCase */ public function getMiddleware(): AuthenticateAsSubuser { - return new AuthenticateAsSubuser($this->keyProviderService, $this->session); + return new AuthenticateAsSubuser($this->keyProviderService); } } diff --git a/tests/Unit/Services/DaemonKeys/DaemonKeyProviderServiceTest.php b/tests/Unit/Services/DaemonKeys/DaemonKeyProviderServiceTest.php index 87d5f506b..81d1b3a09 100644 --- a/tests/Unit/Services/DaemonKeys/DaemonKeyProviderServiceTest.php +++ b/tests/Unit/Services/DaemonKeys/DaemonKeyProviderServiceTest.php @@ -14,11 +14,13 @@ use Carbon\Carbon; use Tests\TestCase; use Pterodactyl\Models\User; use Pterodactyl\Models\Server; +use Pterodactyl\Models\Subuser; use Pterodactyl\Models\DaemonKey; use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService; use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService; use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService; use Pterodactyl\Exceptions\Repository\RecordNotFoundException; +use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface; class DaemonKeyProviderServiceTest extends TestCase @@ -38,6 +40,11 @@ class DaemonKeyProviderServiceTest extends TestCase */ private $repository; + /** + * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock + */ + private $subuserRepository; + /** * Setup tests. */ @@ -49,6 +56,7 @@ class DaemonKeyProviderServiceTest extends TestCase $this->keyCreationService = m::mock(DaemonKeyCreationService::class); $this->keyUpdateService = m::mock(DaemonKeyUpdateService::class); $this->repository = m::mock(DaemonKeyRepositoryInterface::class); + $this->subuserRepository = m::mock(SubuserRepositoryInterface::class); } /** @@ -154,6 +162,33 @@ class DaemonKeyProviderServiceTest extends TestCase $this->assertEquals($key->secret, $response); } + /** + * Test that a missing key is created for a subuser. + */ + public function testMissingKeyIsCreatedForSubuser() + { + $user = factory(User::class)->make(['root_admin' => 0]); + $server = factory(Server::class)->make(); + $key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]); + $subuser = factory(Subuser::class)->make(['user_id' => $user->id, 'server_id' => $server->id]); + + $this->repository->shouldReceive('findFirstWhere')->with([ + ['user_id', '=', $user->id], + ['server_id', '=', $server->id], + ])->once()->andThrow(new RecordNotFoundException); + + $this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([ + ['user_id', '=', $user->id], + ['server_id', '=', $server->id], + ])->andReturn($subuser); + + $this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret); + + $response = $this->getService()->handle($server, $user, false); + $this->assertNotEmpty($response); + $this->assertEquals($key->secret, $response); + } + /** * Test that an exception is thrown if the user should not get a key. * @@ -169,6 +204,11 @@ class DaemonKeyProviderServiceTest extends TestCase ['server_id', '=', $server->id], ])->once()->andThrow(new RecordNotFoundException); + $this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([ + ['user_id', '=', $user->id], + ['server_id', '=', $server->id], + ])->andThrow(new RecordNotFoundException); + $this->getService()->handle($server, $user, false); } @@ -179,6 +219,11 @@ class DaemonKeyProviderServiceTest extends TestCase */ private function getService(): DaemonKeyProviderService { - return new DaemonKeyProviderService($this->keyCreationService, $this->repository, $this->keyUpdateService); + return new DaemonKeyProviderService( + $this->keyCreationService, + $this->repository, + $this->keyUpdateService, + $this->subuserRepository + ); } } diff --git a/tests/Unit/Services/Servers/ServerCreationServiceTest.php b/tests/Unit/Services/Servers/ServerCreationServiceTest.php index 309508dbf..92fbec127 100644 --- a/tests/Unit/Services/Servers/ServerCreationServiceTest.php +++ b/tests/Unit/Services/Servers/ServerCreationServiceTest.php @@ -116,8 +116,14 @@ class ServerCreationServiceTest extends TestCase ]); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('isUniqueUuidCombo') + ->once() + ->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8)) + ->andReturn(true); + $this->repository->shouldReceive('create')->with(m::subset([ 'uuid' => $this->getKnownUuid(), + 'uuidShort' => substr($this->getKnownUuid(), 0, 8), 'node_id' => $model->node_id, 'allocation_id' => $model->allocation_id, 'owner_id' => $model->owner_id, @@ -164,8 +170,14 @@ class ServerCreationServiceTest extends TestCase $this->eggRepository->shouldReceive('setColumns->find')->once()->with($model->egg_id)->andReturn($eggModel); $this->validatorService->shouldReceive('setUserLevel->handle')->once()->andReturn(collect([])); - $this->repository->shouldReceive('create')->once()->with(m::subset([ + $this->repository->shouldReceive('isUniqueUuidCombo') + ->once() + ->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8)) + ->andReturn(true); + + $this->repository->shouldReceive('create')->with(m::subset([ 'uuid' => $this->getKnownUuid(), + 'uuidShort' => substr($this->getKnownUuid(), 0, 8), 'node_id' => $model->node_id, 'allocation_id' => $model->allocation_id, 'nest_id' => $model->nest_id, @@ -211,8 +223,14 @@ class ServerCreationServiceTest extends TestCase $this->allocationSelectionService->shouldReceive('handle')->once()->withNoArgs()->andReturn($allocationModel); $this->validatorService->shouldReceive('setUserLevel->handle')->once()->andReturn(collect([])); - $this->repository->shouldReceive('create')->once()->with(m::subset([ + $this->repository->shouldReceive('isUniqueUuidCombo') + ->once() + ->with($this->getKnownUuid(), substr($this->getKnownUuid(), 0, 8)) + ->andReturn(true); + + $this->repository->shouldReceive('create')->with(m::subset([ 'uuid' => $this->getKnownUuid(), + 'uuidShort' => substr($this->getKnownUuid(), 0, 8), 'node_id' => $model->node_id, 'allocation_id' => $model->allocation_id, 'nest_id' => $model->nest_id, @@ -244,6 +262,7 @@ class ServerCreationServiceTest extends TestCase ]); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('isUniqueUuidCombo')->once()->andReturn(true); $this->repository->shouldReceive('create')->once()->andReturn($model); $this->allocationRepository->shouldReceive('assignAllocationsToServer')->once()->andReturn(1); $this->validatorService->shouldReceive('setUserLevel')->once()->andReturnSelf();