2020-03-19 04:08:32 +00:00
< ? php
namespace Pterodactyl\Http\Controllers\Api\Client\Servers ;
use Pterodactyl\Models\Task ;
use Illuminate\Http\Response ;
use Pterodactyl\Models\Server ;
use Pterodactyl\Models\Schedule ;
use Illuminate\Http\JsonResponse ;
2022-05-29 23:26:28 +00:00
use Pterodactyl\Facades\Activity ;
2020-03-19 04:08:32 +00:00
use Pterodactyl\Models\Permission ;
use Pterodactyl\Repositories\Eloquent\TaskRepository ;
use Pterodactyl\Exceptions\Http\HttpForbiddenException ;
2020-03-19 05:28:32 +00:00
use Pterodactyl\Transformers\Api\Client\TaskTransformer ;
2020-03-19 04:08:32 +00:00
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest ;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController ;
2020-06-28 21:41:22 +00:00
use Pterodactyl\Exceptions\Service\ServiceLimitExceededException ;
2020-03-19 04:08:32 +00:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException ;
2020-03-19 05:28:32 +00:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Schedules\StoreTaskRequest ;
2020-03-19 04:08:32 +00:00
class ScheduleTaskController extends ClientApiController
{
/**
* ScheduleTaskController constructor .
*/
2022-10-14 16:59:20 +00:00
public function __construct ( private TaskRepository $repository )
2020-03-19 04:08:32 +00:00
{
parent :: __construct ();
}
2020-03-19 05:28:32 +00:00
/**
* Create a new task for a given schedule and store it in the database .
*
* @ throws \Pterodactyl\Exceptions\Model\DataValidationException
2020-06-28 21:41:22 +00:00
* @ throws \Pterodactyl\Exceptions\Service\ServiceLimitExceededException
2020-03-19 05:28:32 +00:00
*/
2022-10-14 16:59:20 +00:00
public function store ( StoreTaskRequest $request , Server $server , Schedule $schedule ) : array
2020-03-19 05:28:32 +00:00
{
2020-06-28 21:41:22 +00:00
$limit = config ( 'pterodactyl.client_features.schedules.per_schedule_task_limit' , 10 );
if ( $schedule -> tasks () -> count () >= $limit ) {
2022-10-14 16:59:20 +00:00
throw new ServiceLimitExceededException ( " Schedules may not have more than $limit tasks associated with them. Creating this task would put this schedule over the limit. " );
2020-03-19 05:28:32 +00:00
}
2021-05-16 16:47:36 +00:00
if ( $server -> backup_limit === 0 && $request -> action === 'backup' ) {
throw new HttpForbiddenException ( " A backup task cannot be created when the server's backup limit is set to 0. " );
}
2020-10-23 03:54:58 +00:00
/** @var \Pterodactyl\Models\Task|null $lastTask */
$lastTask = $schedule -> tasks () -> orderByDesc ( 'sequence_id' ) -> first ();
2020-03-19 05:28:32 +00:00
/** @var \Pterodactyl\Models\Task $task */
$task = $this -> repository -> create ([
'schedule_id' => $schedule -> id ,
'sequence_id' => ( $lastTask -> sequence_id ? ? 0 ) + 1 ,
'action' => $request -> input ( 'action' ),
2020-06-19 04:00:04 +00:00
'payload' => $request -> input ( 'payload' ) ? ? '' ,
2020-03-19 05:28:32 +00:00
'time_offset' => $request -> input ( 'time_offset' ),
2021-05-01 17:44:40 +00:00
'continue_on_failure' => ( bool ) $request -> input ( 'continue_on_failure' ),
2020-03-19 05:28:32 +00:00
]);
2022-05-29 23:26:28 +00:00
Activity :: event ( 'server:task.create' )
-> subject ( $schedule , $task )
-> property ([ 'name' => $schedule -> name , 'action' => $task -> action , 'payload' => $task -> payload ])
-> log ();
2020-03-19 05:28:32 +00:00
return $this -> fractal -> item ( $task )
-> transformWith ( $this -> getTransformer ( TaskTransformer :: class ))
-> toArray ();
}
/**
* Updates a given task for a server .
*
* @ throws \Pterodactyl\Exceptions\Model\DataValidationException
* @ throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
2022-10-14 16:59:20 +00:00
public function update ( StoreTaskRequest $request , Server $server , Schedule $schedule , Task $task ) : array
2020-03-19 05:28:32 +00:00
{
if ( $schedule -> id !== $task -> schedule_id || $server -> id !== $schedule -> server_id ) {
2021-01-23 20:33:34 +00:00
throw new NotFoundHttpException ();
2020-03-19 05:28:32 +00:00
}
2021-05-16 16:47:36 +00:00
if ( $server -> backup_limit === 0 && $request -> action === 'backup' ) {
throw new HttpForbiddenException ( " A backup task cannot be created when the server's backup limit is set to 0. " );
}
2020-03-19 05:28:32 +00:00
$this -> repository -> update ( $task -> id , [
2020-04-11 20:07:40 +00:00
'action' => $request -> input ( 'action' ),
2020-06-19 04:00:04 +00:00
'payload' => $request -> input ( 'payload' ) ? ? '' ,
2020-03-19 05:28:32 +00:00
'time_offset' => $request -> input ( 'time_offset' ),
2021-05-01 17:44:40 +00:00
'continue_on_failure' => ( bool ) $request -> input ( 'continue_on_failure' ),
2020-03-19 05:28:32 +00:00
]);
2022-05-29 23:26:28 +00:00
Activity :: event ( 'server:task.update' )
-> subject ( $schedule , $task )
-> property ([ 'name' => $schedule -> name , 'action' => $task -> action , 'payload' => $task -> payload ])
-> log ();
2020-03-19 05:28:32 +00:00
return $this -> fractal -> item ( $task -> refresh ())
-> transformWith ( $this -> getTransformer ( TaskTransformer :: class ))
-> toArray ();
}
2020-03-19 04:08:32 +00:00
/**
2020-10-23 03:54:58 +00:00
* Delete a given task for a schedule . If there are subsequent tasks stored in the database
* for this schedule their sequence IDs are decremented properly .
2020-03-19 04:08:32 +00:00
*
2020-10-23 03:54:58 +00:00
* @ throws \Exception
2020-03-19 04:08:32 +00:00
*/
2022-10-14 16:59:20 +00:00
public function delete ( ClientApiRequest $request , Server $server , Schedule $schedule , Task $task ) : JsonResponse
2020-03-19 04:08:32 +00:00
{
if ( $task -> schedule_id !== $schedule -> id || $schedule -> server_id !== $server -> id ) {
2021-01-23 20:33:34 +00:00
throw new NotFoundHttpException ();
2020-03-19 04:08:32 +00:00
}
2021-01-23 20:33:34 +00:00
if ( ! $request -> user () -> can ( Permission :: ACTION_SCHEDULE_UPDATE , $server )) {
2020-03-19 04:08:32 +00:00
throw new HttpForbiddenException ( 'You do not have permission to perform this action.' );
}
2020-10-23 03:54:58 +00:00
$schedule -> tasks () -> where ( 'sequence_id' , '>' , $task -> sequence_id ) -> update ([
'sequence_id' => $schedule -> tasks () -> getConnection () -> raw ( '(sequence_id - 1)' ),
]);
$task -> delete ();
2020-03-19 04:08:32 +00:00
2022-05-29 23:26:28 +00:00
Activity :: event ( 'server:task.delete' ) -> subject ( $schedule , $task ) -> property ( 'name' , $schedule -> name ) -> log ();
2020-10-23 03:54:58 +00:00
return new JsonResponse ( null , Response :: HTTP_NO_CONTENT );
2020-03-19 04:08:32 +00:00
}
}