Fix tests

This commit is contained in:
Dane Everitt 2021-01-30 19:12:22 -08:00
parent 2f08456ed9
commit 00da092e45
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
11 changed files with 21 additions and 389 deletions

View file

@ -1,99 +0,0 @@
<?php
namespace Pterodactyl\Http\Controllers\Admin;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Traits\Controllers\PlainJavascriptInjection;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
class StatisticsController extends Controller
{
use PlainJavascriptInjection;
private $allocationRepository;
private $databaseRepository;
private $eggRepository;
private $nodeRepository;
private $serverRepository;
private $userRepository;
public function __construct(
AllocationRepositoryInterface $allocationRepository,
DatabaseRepositoryInterface $databaseRepository,
EggRepositoryInterface $eggRepository,
NodeRepositoryInterface $nodeRepository,
ServerRepositoryInterface $serverRepository,
UserRepositoryInterface $userRepository
) {
$this->allocationRepository = $allocationRepository;
$this->databaseRepository = $databaseRepository;
$this->eggRepository = $eggRepository;
$this->nodeRepository = $nodeRepository;
$this->serverRepository = $serverRepository;
$this->userRepository = $userRepository;
}
public function index()
{
throw new NotFoundHttpException();
$servers = $this->serverRepository->all();
$nodes = $this->nodeRepository->all();
$usersCount = $this->userRepository->count();
$eggsCount = $this->eggRepository->count();
$databasesCount = $this->databaseRepository->count();
$totalAllocations = $this->allocationRepository->count();
$suspendedServersCount = $this->serverRepository->getSuspendedServersCount();
$totalServerRam = 0;
$totalNodeRam = 0;
$totalServerDisk = 0;
$totalNodeDisk = 0;
foreach ($nodes as $node) {
$stats = $this->nodeRepository->getUsageStatsRaw($node);
$totalServerRam += $stats['memory']['value'];
$totalNodeRam += $stats['memory']['max'];
$totalServerDisk += $stats['disk']['value'];
$totalNodeDisk += $stats['disk']['max'];
}
$tokens = [];
foreach ($nodes as $node) {
$tokens[$node->id] = decrypt($node->daemon_token);
}
$this->injectJavascript([
'servers' => $servers,
'suspendedServers' => $suspendedServersCount,
'totalServerRam' => $totalServerRam,
'totalNodeRam' => $totalNodeRam,
'totalServerDisk' => $totalServerDisk,
'totalNodeDisk' => $totalNodeDisk,
'nodes' => $nodes,
'tokens' => $tokens,
]);
return view('admin.statistics', [
'servers' => $servers,
'nodes' => $nodes,
'usersCount' => $usersCount,
'eggsCount' => $eggsCount,
'totalServerRam' => $totalServerRam,
'databasesCount' => $databasesCount,
'totalNodeRam' => $totalNodeRam,
'totalNodeDisk' => $totalNodeDisk,
'totalServerDisk' => $totalServerDisk,
'totalAllocations' => $totalAllocations,
]);
}
}

View file

@ -8,7 +8,7 @@ use Pterodactyl\Models\Server;
use Pterodactyl\Models\AuditLog;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Models\Permission;
use Illuminate\Validation\UnauthorizedException;
use Illuminate\Auth\Access\AuthorizationException;
use Pterodactyl\Services\Backups\DeleteBackupService;
use Pterodactyl\Services\Backups\DownloadLinkService;
use Pterodactyl\Services\Backups\InitiateBackupService;
@ -63,11 +63,12 @@ class BackupController extends ClientApiController
*
* @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation
* @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function index(Request $request, Server $server): array
{
if (!$request->user()->can(Permission::ACTION_BACKUP_READ, $server)) {
throw new UnauthorizedException();
throw new AuthorizationException();
}
$limit = min($request->query('per_page') ?? 20, 50);
@ -109,11 +110,12 @@ class BackupController extends ClientApiController
*
* @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation
* @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function view(Request $request, Server $server, Backup $backup): array
{
if (!$request->user()->can(Permission::ACTION_BACKUP_READ, $server)) {
throw new UnauthorizedException();
throw new AuthorizationException();
}
return $this->fractal->item($backup)
@ -130,7 +132,7 @@ class BackupController extends ClientApiController
public function delete(Request $request, Server $server, Backup $backup): JsonResponse
{
if (!$request->user()->can(Permission::ACTION_BACKUP_DELETE, $server)) {
throw new UnauthorizedException();
throw new AuthorizationException();
}
$server->audit(AuditLog::SERVER__BACKUP_DELETED, function (AuditLog $audit) use ($backup) {
@ -146,11 +148,13 @@ class BackupController extends ClientApiController
* Download the backup for a given server instance. For daemon local files, the file
* will be streamed back through the Panel. For AWS S3 files, a signed URL will be generated
* which the user is redirected to.
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function download(Request $request, Server $server, Backup $backup): JsonResponse
{
if (!$request->user()->can(Permission::ACTION_BACKUP_DOWNLOAD, $server)) {
throw new UnauthorizedException();
throw new AuthorizationException();
}
switch ($backup->disk) {
@ -179,7 +183,7 @@ class BackupController extends ClientApiController
public function restore(Request $request, Server $server, Backup $backup): JsonResponse
{
if (!$request->user()->can(Permission::ACTION_BACKUP_RESTORE, $server)) {
throw new UnauthorizedException();
throw new AuthorizationException();
}
// Cannot restore a backup unless a server is fully installed and not currently