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/ScheduleTaskController.php
14055 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4
5
use Pterodactyl\Models\Task;
6
use Illuminate\Http\Response;
7
use Pterodactyl\Models\Server;
8
use Pterodactyl\Models\Schedule;
9
use Illuminate\Http\JsonResponse;
10
use Pterodactyl\Facades\Activity;
11
use Pterodactyl\Models\Permission;
12
use Illuminate\Database\ConnectionInterface;
13
use Pterodactyl\Repositories\Eloquent\TaskRepository;
14
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
15
use Pterodactyl\Transformers\Api\Client\TaskTransformer;
16
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
17
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
18
use Pterodactyl\Exceptions\Service\ServiceLimitExceededException;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\StoreTaskRequest;
21
22
class ScheduleTaskController extends ClientApiController
23
{
24
/**
25
* ScheduleTaskController constructor.
26
*/
27
public function __construct(
28
private ConnectionInterface $connection,
29
private TaskRepository $repository,
30
) {
31
parent::__construct();
32
}
33
34
/**
35
* Create a new task for a given schedule and store it in the database.
36
*
37
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
38
* @throws ServiceLimitExceededException
39
*/
40
public function store(StoreTaskRequest $request, Server $server, Schedule $schedule): array
41
{
42
$limit = config('pterodactyl.client_features.schedules.per_schedule_task_limit', 10);
43
if ($schedule->tasks()->count() >= $limit) {
44
throw new ServiceLimitExceededException("Schedules may not have more than $limit tasks associated with them. Creating this task would put this schedule over the limit.");
45
}
46
47
if ($server->backup_limit === 0 && $request->action === 'backup') {
48
throw new HttpForbiddenException("A backup task cannot be created when the server's backup limit is set to 0.");
49
}
50
51
/** @var Task|null $lastTask */
52
$lastTask = $schedule->tasks()->orderByDesc('sequence_id')->first();
53
54
/** @var Task $task */
55
$task = $this->connection->transaction(function () use ($request, $schedule, $lastTask) {
56
$sequenceId = ($lastTask->sequence_id ?? 0) + 1;
57
$requestSequenceId = $request->integer('sequence_id', $sequenceId);
58
59
// Ensure that the sequence id is at least 1.
60
if ($requestSequenceId < 1) {
61
$requestSequenceId = 1;
62
}
63
64
// If the sequence id from the request is greater than or equal to the next available
65
// sequence id, we don't need to do anything special. Otherwise, we need to update
66
// the sequence id of all tasks that are greater than or equal to the request sequence
67
// id to be one greater than the current value.
68
if ($requestSequenceId < $sequenceId) {
69
$schedule->tasks()
70
->where('sequence_id', '>=', $requestSequenceId)
71
->increment('sequence_id');
72
$sequenceId = $requestSequenceId;
73
}
74
75
return $this->repository->create([
76
'schedule_id' => $schedule->id,
77
'sequence_id' => $sequenceId,
78
'action' => $request->input('action'),
79
'payload' => $request->input('payload') ?? '',
80
'time_offset' => $request->input('time_offset'),
81
'continue_on_failure' => $request->boolean('continue_on_failure'),
82
]);
83
});
84
85
Activity::event('server:task.create')
86
->subject($schedule, $task)
87
->property(['name' => $schedule->name, 'action' => $task->action, 'payload' => $task->payload])
88
->log();
89
90
return $this->fractal->item($task)
91
->transformWith($this->getTransformer(TaskTransformer::class))
92
->toArray();
93
}
94
95
/**
96
* Updates a given task for a server.
97
*
98
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
99
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
100
*/
101
public function update(StoreTaskRequest $request, Server $server, Schedule $schedule, Task $task): array
102
{
103
if ($schedule->id !== $task->schedule_id || $server->id !== $schedule->server_id) {
104
throw new NotFoundHttpException();
105
}
106
107
if ($server->backup_limit === 0 && $request->action === 'backup') {
108
throw new HttpForbiddenException("A backup task cannot be created when the server's backup limit is set to 0.");
109
}
110
111
$this->connection->transaction(function () use ($request, $schedule, $task) {
112
$sequenceId = $request->integer('sequence_id', $task->sequence_id);
113
// Ensure that the sequence id is at least 1.
114
if ($sequenceId < 1) {
115
$sequenceId = 1;
116
}
117
118
// Shift all other tasks in the schedule up or down to make room for the new task.
119
if ($sequenceId < $task->sequence_id) {
120
$schedule->tasks()
121
->where('sequence_id', '>=', $sequenceId)
122
->where('sequence_id', '<', $task->sequence_id)
123
->increment('sequence_id');
124
} elseif ($sequenceId > $task->sequence_id) {
125
$schedule->tasks()
126
->where('sequence_id', '>', $task->sequence_id)
127
->where('sequence_id', '<=', $sequenceId)
128
->decrement('sequence_id');
129
}
130
131
$this->repository->update($task->id, [
132
'sequence_id' => $sequenceId,
133
'action' => $request->input('action'),
134
'payload' => $request->input('payload') ?? '',
135
'time_offset' => $request->input('time_offset'),
136
'continue_on_failure' => $request->boolean('continue_on_failure'),
137
]);
138
});
139
140
Activity::event('server:task.update')
141
->subject($schedule, $task)
142
->property(['name' => $schedule->name, 'action' => $task->action, 'payload' => $task->payload])
143
->log();
144
145
return $this->fractal->item($task->refresh())
146
->transformWith($this->getTransformer(TaskTransformer::class))
147
->toArray();
148
}
149
150
/**
151
* Delete a given task for a schedule. If there are subsequent tasks stored in the database
152
* for this schedule their sequence IDs are decremented properly.
153
*
154
* @throws \Exception
155
*/
156
public function delete(ClientApiRequest $request, Server $server, Schedule $schedule, Task $task): JsonResponse
157
{
158
if ($task->schedule_id !== $schedule->id || $schedule->server_id !== $server->id) {
159
throw new NotFoundHttpException();
160
}
161
162
if (!$request->user()->can(Permission::ACTION_SCHEDULE_UPDATE, $server)) {
163
throw new HttpForbiddenException('You do not have permission to perform this action.');
164
}
165
166
$schedule->tasks()
167
->where('sequence_id', '>', $task->sequence_id)
168
->decrement('sequence_id');
169
$task->delete();
170
171
Activity::event('server:task.delete')->subject($schedule, $task)->property('name', $schedule->name)->log();
172
173
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
174
}
175
}
176
177