Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Client/Servers/SettingsController.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Server;
7
use Illuminate\Http\JsonResponse;
8
use Pterodactyl\Facades\Activity;
9
use Pterodactyl\Repositories\Eloquent\ServerRepository;
10
use Pterodactyl\Services\Servers\ReinstallServerService;
11
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
12
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
13
use Pterodactyl\Http\Requests\Api\Client\Servers\Settings\RenameServerRequest;
14
use Pterodactyl\Http\Requests\Api\Client\Servers\Settings\SetDockerImageRequest;
15
use Pterodactyl\Http\Requests\Api\Client\Servers\Settings\ReinstallServerRequest;
16
17
class SettingsController extends ClientApiController
18
{
19
/**
20
* SettingsController constructor.
21
*/
22
public function __construct(
23
private ServerRepository $repository,
24
private ReinstallServerService $reinstallServerService,
25
) {
26
parent::__construct();
27
}
28
29
/**
30
* Renames a server.
31
*
32
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
33
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
34
*/
35
public function rename(RenameServerRequest $request, Server $server): JsonResponse
36
{
37
$name = $request->input('name');
38
$description = $request->has('description') ? (string) $request->input('description') : $server->description;
39
$this->repository->update($server->id, [
40
'name' => $name,
41
'description' => $description,
42
]);
43
44
if ($server->name !== $name) {
45
Activity::event('server:settings.rename')
46
->property(['old' => $server->name, 'new' => $name])
47
->log();
48
}
49
50
if ($server->description !== $description) {
51
Activity::event('server:settings.description')
52
->property(['old' => $server->description, 'new' => $description])
53
->log();
54
}
55
56
return new JsonResponse([], Response::HTTP_NO_CONTENT);
57
}
58
59
/**
60
* Reinstalls the server on the daemon.
61
*
62
* @throws \Throwable
63
*/
64
public function reinstall(ReinstallServerRequest $request, Server $server): JsonResponse
65
{
66
$this->reinstallServerService->handle($server);
67
68
Activity::event('server:reinstall')->log();
69
70
return new JsonResponse([], Response::HTTP_ACCEPTED);
71
}
72
73
/**
74
* Changes the Docker image in use by the server.
75
*
76
* @throws \Throwable
77
*/
78
public function dockerImage(SetDockerImageRequest $request, Server $server): JsonResponse
79
{
80
if (!in_array($server->image, array_values($server->egg->docker_images))) {
81
throw new BadRequestHttpException('This server\'s Docker image has been manually set by an administrator and cannot be updated.');
82
}
83
84
$original = $server->image;
85
$server->forceFill(['image' => $request->input('docker_image')])->saveOrFail();
86
87
if ($original !== $server->image) {
88
Activity::event('server:startup.image')
89
->property(['old' => $original, 'new' => $request->input('docker_image')])
90
->log();
91
}
92
93
return new JsonResponse([], Response::HTTP_NO_CONTENT);
94
}
95
}
96
97