Path: blob/1.0-develop/app/Http/Controllers/Admin/ServersController.php
10279 views
<?php12namespace Pterodactyl\Http\Controllers\Admin;34use Illuminate\Http\Request;5use Pterodactyl\Models\User;6use Illuminate\Http\Response;7use Pterodactyl\Models\Mount;8use Pterodactyl\Models\Server;9use Pterodactyl\Models\Database;10use Pterodactyl\Models\MountServer;11use Illuminate\Http\RedirectResponse;12use Prologue\Alerts\AlertsMessageBag;13use Pterodactyl\Exceptions\DisplayException;14use Pterodactyl\Http\Controllers\Controller;15use Illuminate\Validation\ValidationException;16use Pterodactyl\Services\Servers\SuspensionService;17use Pterodactyl\Repositories\Eloquent\MountRepository;18use Pterodactyl\Services\Servers\ServerDeletionService;19use Pterodactyl\Services\Servers\ReinstallServerService;20use Pterodactyl\Exceptions\Model\DataValidationException;21use Pterodactyl\Repositories\Wings\DaemonServerRepository;22use Pterodactyl\Services\Servers\BuildModificationService;23use Pterodactyl\Services\Databases\DatabasePasswordService;24use Pterodactyl\Services\Servers\DetailsModificationService;25use Pterodactyl\Services\Servers\StartupModificationService;26use Pterodactyl\Contracts\Repository\NestRepositoryInterface;27use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;28use Pterodactyl\Services\Databases\DatabaseManagementService;29use Illuminate\Contracts\Config\Repository as ConfigRepository;30use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;31use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;32use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;33use Pterodactyl\Services\Servers\ServerConfigurationStructureService;34use Pterodactyl\Http\Requests\Admin\Servers\Databases\StoreServerDatabaseRequest;3536class ServersController extends Controller37{38/**39* ServersController constructor.40*/41public function __construct(42protected AlertsMessageBag $alert,43protected AllocationRepositoryInterface $allocationRepository,44protected BuildModificationService $buildModificationService,45protected ConfigRepository $config,46protected DaemonServerRepository $daemonServerRepository,47protected DatabaseManagementService $databaseManagementService,48protected DatabasePasswordService $databasePasswordService,49protected DatabaseRepositoryInterface $databaseRepository,50protected DatabaseHostRepository $databaseHostRepository,51protected ServerDeletionService $deletionService,52protected DetailsModificationService $detailsModificationService,53protected ReinstallServerService $reinstallService,54protected ServerRepositoryInterface $repository,55protected MountRepository $mountRepository,56protected NestRepositoryInterface $nestRepository,57protected ServerConfigurationStructureService $serverConfigurationStructureService,58protected StartupModificationService $startupModificationService,59protected SuspensionService $suspensionService,60) {61}6263/**64* Update the details for a server.65*66* @throws DataValidationException67* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException68*/69public function setDetails(Request $request, Server $server): RedirectResponse70{71$this->detailsModificationService->handle($server, $request->only([72'owner_id', 'external_id', 'name', 'description',73]));7475$this->alert->success(trans('admin/server.alerts.details_updated'))->flash();7677return redirect()->route('admin.servers.view.details', $server->id);78}7980/**81* Toggles the installation status for a server.82*83* @throws DisplayException84* @throws DataValidationException85* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException86*/87public function toggleInstall(Server $server): RedirectResponse88{89if ($server->status === Server::STATUS_INSTALL_FAILED) {90throw new DisplayException(trans('admin/server.exceptions.marked_as_failed'));91}9293$this->repository->update($server->id, [94'status' => $server->isInstalled() ? Server::STATUS_INSTALLING : null,95], true, true);9697$this->alert->success(trans('admin/server.alerts.install_toggled'))->flash();9899return redirect()->route('admin.servers.view.manage', $server->id);100}101102/**103* Reinstalls the server with the currently assigned service.104*105* @throws DisplayException106* @throws DataValidationException107* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException108*/109public function reinstallServer(Server $server): RedirectResponse110{111$this->reinstallService->handle($server);112$this->alert->success(trans('admin/server.alerts.server_reinstalled'))->flash();113114return redirect()->route('admin.servers.view.manage', $server->id);115}116117/**118* Manage the suspension status for a server.119*120* @throws DisplayException121* @throws DataValidationException122* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException123*/124public function manageSuspension(Request $request, Server $server): RedirectResponse125{126$this->suspensionService->toggle($server, $request->input('action'));127$this->alert->success(trans('admin/server.alerts.suspension_toggled', [128'status' => $request->input('action') . 'ed',129]))->flash();130131return redirect()->route('admin.servers.view.manage', $server->id);132}133134/**135* Update the build configuration for a server.136*137* @throws DisplayException138* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException139* @throws ValidationException140*/141public function updateBuild(Request $request, Server $server): RedirectResponse142{143try {144$this->buildModificationService->handle($server, $request->only([145'allocation_id', 'add_allocations', 'remove_allocations',146'memory', 'swap', 'io', 'cpu', 'threads', 'disk',147'database_limit', 'allocation_limit', 'backup_limit', 'oom_disabled',148]));149} catch (DataValidationException $exception) {150throw new ValidationException($exception->getValidator());151}152153$this->alert->success(trans('admin/server.alerts.build_updated'))->flash();154155return redirect()->route('admin.servers.view.build', $server->id);156}157158/**159* Start the server deletion process.160*161* @throws DisplayException162* @throws \Throwable163*/164public function delete(Request $request, Server $server): RedirectResponse165{166$this->deletionService->withForce($request->filled('force_delete'))->handle($server);167$this->alert->success(trans('admin/server.alerts.server_deleted'))->flash();168169return redirect()->route('admin.servers');170}171172/**173* Update the startup command as well as variables.174*175* @throws ValidationException176*/177public function saveStartup(Request $request, Server $server): RedirectResponse178{179$data = $request->except('_token');180if (!empty($data['custom_docker_image'])) {181$data['docker_image'] = $data['custom_docker_image'];182unset($data['custom_docker_image']);183}184185try {186$this->startupModificationService187->setUserLevel(User::USER_LEVEL_ADMIN)188->handle($server, $data);189} catch (DataValidationException $exception) {190throw new ValidationException($exception->getValidator());191}192193$this->alert->success(trans('admin/server.alerts.startup_changed'))->flash();194195return redirect()->route('admin.servers.view.startup', $server->id);196}197198/**199* Creates a new database assigned to a specific server.200*201* @throws \Throwable202*/203public function newDatabase(StoreServerDatabaseRequest $request, Server $server): RedirectResponse204{205$this->databaseManagementService->create($server, [206'database' => DatabaseManagementService::generateUniqueDatabaseName($request->input('database'), $server->id),207'remote' => $request->input('remote'),208'database_host_id' => $request->input('database_host_id'),209'max_connections' => $request->input('max_connections'),210]);211212return redirect()->route('admin.servers.view.database', $server->id)->withInput();213}214215/**216* Resets the database password for a specific database on this server.217*218* @throws \Throwable219*/220public function resetDatabasePassword(Request $request, Server $server): Response221{222/** @var Database $database */223$database = $server->databases()->findOrFail($request->input('database'));224225$this->databasePasswordService->handle($database);226227return response('', 204);228}229230/**231* Deletes a database from a server.232*233* @throws \Exception234*/235public function deleteDatabase(Server $server, Database $database): Response236{237$this->databaseManagementService->delete($database);238239return response('', 204);240}241242/**243* Add a mount to a server.244*245* @throws \Throwable246*/247public function addMount(Request $request, Server $server): RedirectResponse248{249$mountServer = (new MountServer())->forceFill([250'mount_id' => $request->input('mount_id'),251'server_id' => $server->id,252]);253254$mountServer->saveOrFail();255256$this->alert->success('Mount was added successfully.')->flash();257258return redirect()->route('admin.servers.view.mounts', $server->id);259}260261/**262* Remove a mount from a server.263*/264public function deleteMount(Server $server, Mount $mount): RedirectResponse265{266MountServer::where('mount_id', $mount->id)->where('server_id', $server->id)->delete();267268$this->alert->success('Mount was removed successfully.')->flash();269270return redirect()->route('admin.servers.view.mounts', $server->id);271}272}273274275