|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Pterodactyl\Http\Controllers\Server\Tasks; |
| 26 | + |
| 27 | +use Illuminate\Contracts\Session\Session; |
| 28 | +use Pterodactyl\Http\Controllers\Controller; |
| 29 | +use Pterodactyl\Services\Tasks\TaskCreationService; |
| 30 | +use Pterodactyl\Contracts\Extensions\HashidsInterface; |
| 31 | +use Pterodactyl\Traits\Controllers\JavascriptInjection; |
| 32 | +use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; |
| 33 | +use Pterodactyl\Http\Requests\Server\TaskCreationFormRequest; |
| 34 | + |
| 35 | +class TaskManagementController extends Controller |
| 36 | +{ |
| 37 | + use JavascriptInjection; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var \Pterodactyl\Services\Tasks\TaskCreationService |
| 41 | + */ |
| 42 | + protected $creationService; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var \Pterodactyl\Contracts\Extensions\HashidsInterface |
| 46 | + */ |
| 47 | + protected $hashids; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface |
| 51 | + */ |
| 52 | + protected $repository; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var \Illuminate\Contracts\Session\Session |
| 56 | + */ |
| 57 | + protected $session; |
| 58 | + |
| 59 | + /** |
| 60 | + * TaskManagementController constructor. |
| 61 | + * |
| 62 | + * @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids |
| 63 | + * @param \Illuminate\Contracts\Session\Session $session |
| 64 | + * @param \Pterodactyl\Services\Tasks\TaskCreationService $creationService |
| 65 | + * @param \Pterodactyl\Contracts\Repository\TaskRepositoryInterface $repository |
| 66 | + */ |
| 67 | + public function __construct( |
| 68 | + HashidsInterface $hashids, |
| 69 | + Session $session, |
| 70 | + TaskCreationService $creationService, |
| 71 | + TaskRepositoryInterface $repository |
| 72 | + ) { |
| 73 | + $this->creationService = $creationService; |
| 74 | + $this->hashids = $hashids; |
| 75 | + $this->repository = $repository; |
| 76 | + $this->session = $session; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Display the task page listing. |
| 81 | + * |
| 82 | + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
| 83 | + * |
| 84 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 85 | + */ |
| 86 | + public function index() |
| 87 | + { |
| 88 | + $server = $this->session->get('server_data.model'); |
| 89 | + $this->authorize('list-tasks', $server); |
| 90 | + $this->injectJavascript(); |
| 91 | + |
| 92 | + return view('server.tasks.index', [ |
| 93 | + 'tasks' => $this->repository->getParentTasksWithChainCount($server->id), |
| 94 | + 'actions' => [ |
| 95 | + 'command' => trans('server.tasks.actions.command'), |
| 96 | + 'power' => trans('server.tasks.actions.power'), |
| 97 | + ], |
| 98 | + ]); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Display the task creation page. |
| 103 | + * |
| 104 | + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
| 105 | + * |
| 106 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 107 | + */ |
| 108 | + public function create() |
| 109 | + { |
| 110 | + $server = $this->session->get('server_data.model'); |
| 111 | + $this->authorize('create-task', $server); |
| 112 | + $this->injectJavascript(); |
| 113 | + |
| 114 | + return view('server.tasks.new'); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param \Pterodactyl\Http\Requests\Server\TaskCreationFormRequest $request |
| 119 | + * |
| 120 | + * @return \Illuminate\Http\RedirectResponse |
| 121 | + * |
| 122 | + * @throws \Exception |
| 123 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 124 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 125 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 126 | + */ |
| 127 | + public function store(TaskCreationFormRequest $request) |
| 128 | + { |
| 129 | + $server = $this->session->get('server_data.model'); |
| 130 | + $this->authorize('create-task', $server); |
| 131 | + |
| 132 | + $task = $this->creationService->handle($server, $request->normalize(), $request->getChainedTasks()); |
| 133 | + |
| 134 | + return redirect()->route('server.tasks.view', [ |
| 135 | + 'server' => $server->uuidShort, |
| 136 | + 'task' => $task->id, |
| 137 | + ]); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Return a view to modify task settings. |
| 142 | + * |
| 143 | + * @param string $uuid |
| 144 | + * @param string $task |
| 145 | + * @return \Illuminate\View\View |
| 146 | + * |
| 147 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 148 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 149 | + */ |
| 150 | + public function view($uuid, $task) |
| 151 | + { |
| 152 | + $server = $this->session->get('server_data.model'); |
| 153 | + $this->authorize('edit-task', $server); |
| 154 | + $task = $this->repository->getTaskForServer($this->hashids->decodeFirst($task, 0), $server->id); |
| 155 | + |
| 156 | + $this->injectJavascript([ |
| 157 | + 'chained' => $task->chained->map(function ($chain) { |
| 158 | + return collect($chain->toArray())->only('action', 'chain_delay', 'data')->all(); |
| 159 | + }), |
| 160 | + ]); |
| 161 | + |
| 162 | + return view('server.tasks.view', ['task' => $task]); |
| 163 | + } |
| 164 | + |
| 165 | + public function update(TaskCreationFormRequest $request, $uuid, $task) |
| 166 | + { |
| 167 | + $server = $this->session->get('server_data.model'); |
| 168 | + $this->authorize('edit-task', $server); |
| 169 | + $task = $this->repository->getTaskForServer($this->hashids->decodeFirst($task, 0), $server->id); |
| 170 | + } |
| 171 | +} |
0 commit comments