Begin refactoring Tasks to be apart of the Scheduler system
This commit is contained in:
parent
07965d0ce7
commit
2ac90b50f2
28 changed files with 902 additions and 468 deletions
|
@ -24,20 +24,27 @@
|
|||
|
||||
namespace Pterodactyl\Http\Controllers\Server\Tasks;
|
||||
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
use Pterodactyl\Http\Requests\Request;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Services\Tasks\TaskCreationService;
|
||||
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
||||
use Pterodactyl\Traits\Controllers\JavascriptInjection;
|
||||
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
|
||||
use Pterodactyl\Http\Requests\Server\TaskCreationFormRequest;
|
||||
use Pterodactyl\Services\Schedules\ScheduleCreationService;
|
||||
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
||||
use Pterodactyl\Http\Requests\Server\ScheduleCreationFormRequest;
|
||||
|
||||
class TaskManagementController extends Controller
|
||||
{
|
||||
use JavascriptInjection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Tasks\TaskCreationService
|
||||
* @var \Prologue\Alerts\AlertsMessageBag
|
||||
*/
|
||||
protected $alert;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Schedules\ScheduleCreationService
|
||||
*/
|
||||
protected $creationService;
|
||||
|
||||
|
@ -47,7 +54,7 @@ class TaskManagementController extends Controller
|
|||
protected $hashids;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface
|
||||
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
|
@ -59,17 +66,20 @@ class TaskManagementController extends Controller
|
|||
/**
|
||||
* TaskManagementController constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
|
||||
* @param \Illuminate\Contracts\Session\Session $session
|
||||
* @param \Pterodactyl\Services\Tasks\TaskCreationService $creationService
|
||||
* @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $repository
|
||||
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
||||
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
|
||||
* @param \Illuminate\Contracts\Session\Session $session
|
||||
* @param \Pterodactyl\Services\Schedules\ScheduleCreationService $creationService
|
||||
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
|
||||
*/
|
||||
public function __construct(
|
||||
AlertsMessageBag $alert,
|
||||
HashidsInterface $hashids,
|
||||
Session $session,
|
||||
TaskCreationService $creationService,
|
||||
TaskRepositoryInterface $repository
|
||||
ScheduleCreationService $creationService,
|
||||
ScheduleRepositoryInterface $repository
|
||||
) {
|
||||
$this->alert = $alert;
|
||||
$this->creationService = $creationService;
|
||||
$this->hashids = $hashids;
|
||||
$this->repository = $repository;
|
||||
|
@ -86,11 +96,11 @@ class TaskManagementController extends Controller
|
|||
public function index()
|
||||
{
|
||||
$server = $this->session->get('server_data.model');
|
||||
$this->authorize('list-tasks', $server);
|
||||
$this->authorize('list-schedules', $server);
|
||||
$this->injectJavascript();
|
||||
|
||||
return view('server.tasks.index', [
|
||||
'tasks' => $this->repository->getParentTasksWithChainCount($server->id),
|
||||
'schedules' => $this->repository->getServerSchedules($server->id),
|
||||
'actions' => [
|
||||
'command' => trans('server.tasks.actions.command'),
|
||||
'power' => trans('server.tasks.actions.power'),
|
||||
|
@ -108,64 +118,95 @@ class TaskManagementController extends Controller
|
|||
public function create()
|
||||
{
|
||||
$server = $this->session->get('server_data.model');
|
||||
$this->authorize('create-task', $server);
|
||||
$this->authorize('create-schedule', $server);
|
||||
$this->injectJavascript();
|
||||
|
||||
return view('server.tasks.new');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Pterodactyl\Http\Requests\Server\TaskCreationFormRequest $request
|
||||
*
|
||||
* @param \Pterodactyl\Http\Requests\Server\ScheduleCreationFormRequest $request
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
* @throws \Pterodactyl\Exceptions\Service\Schedule\Task\TaskIntervalTooLongException
|
||||
*/
|
||||
public function store(TaskCreationFormRequest $request)
|
||||
public function store(ScheduleCreationFormRequest $request)
|
||||
{
|
||||
$server = $this->session->get('server_data.model');
|
||||
$this->authorize('create-task', $server);
|
||||
$this->authorize('create-schedule', $server);
|
||||
|
||||
$task = $this->creationService->handle($server, $request->normalize(), $request->getChainedTasks());
|
||||
$schedule = $this->creationService->handle($server, $request->normalize(), $request->getTasks());
|
||||
$this->alert->success(trans('server.tasks.task_created'))->flash();
|
||||
|
||||
return redirect()->route('server.tasks.view', [
|
||||
'server' => $server->uuidShort,
|
||||
'task' => $task->id,
|
||||
'task' => $schedule->hashid,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a view to modify task settings.
|
||||
* Return a view to modify a schedule.
|
||||
*
|
||||
* @param string $uuid
|
||||
* @param string $task
|
||||
* @param \Pterodactyl\Http\Requests\Request $request
|
||||
* @return \Illuminate\View\View
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function view($uuid, $task)
|
||||
public function view(Request $request)
|
||||
{
|
||||
$server = $this->session->get('server_data.model');
|
||||
$this->authorize('edit-task', $server);
|
||||
$task = $this->repository->getTaskForServer($this->hashids->decodeFirst($task, 0), $server->id);
|
||||
$schedule = $request->attributes->get('schedule');
|
||||
$this->authorize('view-schedule', $server);
|
||||
|
||||
$this->injectJavascript([
|
||||
'chained' => $task->chained->map(function ($chain) {
|
||||
return collect($chain->toArray())->only('action', 'chain_delay', 'data')->all();
|
||||
'tasks' => $schedule->tasks->map(function ($schedule) {
|
||||
return collect($schedule->toArray())->only('action', 'time_offset', 'payload')->all();
|
||||
}),
|
||||
]);
|
||||
|
||||
return view('server.tasks.view', ['task' => $task]);
|
||||
return view('server.tasks.view', ['schedule' => $schedule]);
|
||||
}
|
||||
|
||||
public function update(TaskCreationFormRequest $request, $uuid, $task)
|
||||
/**
|
||||
* Update a specific parent task on the system.
|
||||
*
|
||||
* @param \Pterodactyl\Http\Requests\Server\ScheduleCreationFormRequest $request
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function update(ScheduleCreationFormRequest $request)
|
||||
{
|
||||
$server = $this->session->get('server_data.model');
|
||||
$this->authorize('edit-task', $server);
|
||||
$task = $this->repository->getTaskForServer($this->hashids->decodeFirst($task, 0), $server->id);
|
||||
$schedule = $request->attributes->get('schedule');
|
||||
$this->authorize('edit-schedule', $server);
|
||||
|
||||
// $this->updateService->handle($task, $request->normalize(), $request->getChainedTasks());
|
||||
$this->alert->success(trans('server.tasks.task_updated'))->flash();
|
||||
|
||||
return redirect()->route('server.tasks.view', [
|
||||
'server' => $server->uuidShort,
|
||||
'task' => $schedule->hashid,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a parent task from the Panel.
|
||||
*
|
||||
* @param \Pterodactyl\Http\Requests\Request $request
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$server = $this->session->get('server_data.model');
|
||||
$schedule = $request->attributes->get('schedule');
|
||||
$this->authorize('delete-schedule', $server);
|
||||
|
||||
$this->repository->delete($task->id);
|
||||
|
||||
return response('', 204);
|
||||
}
|
||||
}
|
||||
|
|
91
app/Http/Middleware/Server/ScheduleAccess.php
Normal file
91
app/Http/Middleware/Server/ScheduleAccess.php
Normal file
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Http\Middleware\Server;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
||||
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
||||
|
||||
class ScheduleAccess
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Extensions\HashidsInterface
|
||||
*/
|
||||
protected $hashids;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Session\Session
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* TaskAccess constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
|
||||
* @param \Illuminate\Contracts\Session\Session $session
|
||||
* @param \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface $repository
|
||||
*/
|
||||
public function __construct(
|
||||
HashidsInterface $hashids,
|
||||
Session $session,
|
||||
ScheduleRepositoryInterface $repository
|
||||
) {
|
||||
$this->hashids = $hashids;
|
||||
$this->repository = $repository;
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a task is assigned to the active server.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\DisplayException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$server = $this->session->get('server_data.model');
|
||||
|
||||
$scheduleId = $this->hashids->decodeFirst($request->route()->parameter('task'), 0);
|
||||
$schedule = $this->repository->getScheduleWithTasks($scheduleId);
|
||||
|
||||
if ($schedule->server_id !== $server->id) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$request->attributes->set('schedule', $schedule);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
|
@ -26,7 +26,7 @@ namespace Pterodactyl\Http\Requests\Server;
|
|||
|
||||
use Pterodactyl\Http\Requests\FrontendUserFormRequest;
|
||||
|
||||
class TaskCreationFormRequest extends FrontendUserFormRequest
|
||||
class ScheduleCreationFormRequest extends FrontendUserFormRequest
|
||||
{
|
||||
/**
|
||||
* Validation rules to apply to the request.
|
||||
|
@ -37,21 +37,19 @@ class TaskCreationFormRequest extends FrontendUserFormRequest
|
|||
{
|
||||
return [
|
||||
'name' => 'string|max:255',
|
||||
'day_of_week' => 'required|string',
|
||||
'day_of_month' => 'required|string',
|
||||
'hour' => 'required|string',
|
||||
'minute' => 'required|string',
|
||||
'action' => 'required|string|in:power,command',
|
||||
'data' => 'required|string',
|
||||
'chain' => 'sometimes|array|size:4',
|
||||
'chain.time_value' => 'required_with:chain|max:5',
|
||||
'chain.time_interval' => 'required_with:chain|max:5',
|
||||
'chain.action' => 'required_with:chain|max:5',
|
||||
'chain.payload' => 'required_with:chain|max:5',
|
||||
'chain.time_value.*' => 'numeric|between:1,60',
|
||||
'chain.time_interval.*' => 'string|in:s,m',
|
||||
'chain.action.*' => 'string|in:power,command',
|
||||
'chain.payload.*' => 'string',
|
||||
'cron_day_of_week' => 'required|string',
|
||||
'cron_day_of_month' => 'required|string',
|
||||
'cron_hour' => 'required|string',
|
||||
'cron_minute' => 'required|string',
|
||||
'tasks' => 'sometimes|array|size:4',
|
||||
'tasks.time_value' => 'required_with:chain|max:5',
|
||||
'tasks.time_interval' => 'required_with:chain|max:5',
|
||||
'tasks.action' => 'required_with:chain|max:5',
|
||||
'tasks.payload' => 'required_with:chain|max:5',
|
||||
'tasks.time_value.*' => 'numeric|between:1,60',
|
||||
'tasks.time_interval.*' => 'string|in:s,m',
|
||||
'tasks.action.*' => 'string|in:power,command',
|
||||
'tasks.payload.*' => 'string',
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -62,18 +60,18 @@ class TaskCreationFormRequest extends FrontendUserFormRequest
|
|||
*/
|
||||
public function normalize()
|
||||
{
|
||||
return $this->only('name', 'day_of_week', 'day_of_month', 'hour', 'minute', 'action', 'data');
|
||||
return $this->only('name', 'cron_day_of_week', 'cron_day_of_month', 'cron_hour', 'cron_minute');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the chained tasks provided in the request.
|
||||
* Return the tasks provided in the request that are associated with this schedule.
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function getChainedTasks()
|
||||
public function getTasks()
|
||||
{
|
||||
$restructured = [];
|
||||
foreach (array_get($this->all(), 'chain', []) as $key => $values) {
|
||||
foreach (array_get($this->all(), 'tasks', []) as $key => $values) {
|
||||
for ($i = 0; $i < count($values); ++$i) {
|
||||
$restructured[$i][$key] = $values[$i];
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue